這是我寫的一個自動備份MYSQL的 腳本, 能備份所有MYSQL 中的所有數據庫。
#!/bin/bash
umask 177
MUSER="userid"
MPASS="password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
PARAMS="--hex-blob --skip-extended-insert --password=$MPASS"
DATE="$(date +%F)"
BACKUPDIR="/home/${MUSER}/BACKUP/$DATE/"
RMDATE="$(date -d "1 week ago" +%F)"
RMDIR="/home/${MUSER}/BACKUP/$RMDATE"
BACKUPLOG="/home/${MUSER}/BACKUP/backup.log"
if [ ! -f $BACKUPLOG ] ; then
touch $BACKUPLOG
chmod 600 $BACKUPLOG
fi
if [ -d $RMDIR ]; then
rm -rf $RMDIR
echo "Backup DELETED from" $RMDATE >> $BACKUPLOG
fi
if [ ! -d $BACKUPDIR ]; then
mkdir -m 700 -p $BACKUPDIR
fi
echo "Backup BEGINNING" $DATE >> $BACKUPLOG
DBS="$($MYSQL -h $MHOST -u $MUSER -p$MPASS -Bse 'show databases')"
for db in $DBS
do
$MYSQLDUMP $PARAMS $db > $BACKUPDIR$db.sql && bzip2 --best $BACKUPDIR$db.sql
done
echo "Backup COMPLETED" $DATE >> $BACKUPLOG
另外可以結合CRONJOB定時備份
作者 eimhee