Sunday, February 26, 2012

Script to backup mysql database -- database older than 15 days will be deleted

#!/bin/bash

# Script Function:
# This bash script backups all db dependent on when you set the cron job $
# The db will be saved in whereever you want
# DB backups older than 15 days will be deleted.

#[Set Backup Path]
DUMP_PATH=/backup/dbbackup

#[Changes Directory]
cd $DUMP_PATH

#[Old DB Deletion Script]
find $DUMP_PATH -name "*.tar.gz" -mtime +15 -exec rm -f {} \;

#[Stamps the file name with a date]
TIMESTAMP=`date +%y-%m-%d-%H%M`

#[DB Backup Scripts]

#################################################################################
#TODO: create backup user with limited privileges and use it instead of root user
#################################################################################

# DB Host or IP address of MySQL Server
HOST=localhost
# DB Username
USER="root"
# DB Password
PASSWORD="ROOT'sPASSWORD"

mysqldump -h$HOST -u$USER -p$PASSWORD --all-databases > alldb.sql
tar -czPf $DUMP_PATH/alldb-dump.$TIMESTAMP.tar.gz alldb.sql
rm -f alldb.sql

No comments:

Post a Comment