8/21/2010

Backup databases by scripts on CentOS

-B1: Create backup_mysql.sh content:

*************************

# -------------------------------------------------------------------------

MyUSER="root" # USERNAME
MyPASS="Dj4uLgQd" # PASSWORD
MyHOST="localhost" # Hostname

# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/backup_mysql"

# Main directory where backup will be stored
MBD="$DEST/mysql"

# Get hostname
HOST="$(hostname)"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE="mysql_db_backup"
# Store list of databases
DBS="sodepvn_mlmobi sodepvn_sim" # danh sach database can backup

# DO NOT BACKUP these databases
# IGGY="test"

[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi

if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi
done

*********************************

-B2: Restart crontab
service crond restart
- B3: Create Crontab content:
#crontab -e
0 15 * * 1,4 sh /mnt/data/linux/code/backupbookmarks.sh
- B4: Restart crontab

No comments:

Post a Comment