--dump完全备份
vi /tmp/mysql_full_bak.sh
#!/bin/sh
scriptsDir='pwd'
mysqlDir='/usr/local/mysql'
user=root
userPWD=root123
dataBackupDir=/tmp/mysqlbackup
eMailFile=$dataBackupDir/email.txt
[email protected]
logFile=$dataBackupDir/mysqlbackup.log
#DATE='date -I'
DATE=`date -I`
echo "" > $eMailFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile
cd $dataBackupDir
dumpFile=mysql_$DATE.sql
GZDumpFile=mysql_$DATE.sql.tar.gz
#bakup
$mysqlDir/bin/mysqldump -u$user -p$userPWD \
--opt --default-character-set=utf8 --extended-insert=false \
--triggers -R --hex-blob --all-databases \
--flush-logs --delete-master-logs \
-x  > $dumpFile
#tar
if [[ $? == 0 ]]; then
tar czf $GZDumpFile $dumpFile >> eMailFile 2>&1
echo "BackupFileName:$GZDumpFile" >> $eMailFile
echo "DataBase Backup Success!" >> $eMailFile
rm -rf $dumpFile
#delete previous daily backup files
cd $dataBackupDir/daily
rm -f *
# Delete old backup files(mtime>2).
#$scriptsDir/rmBackup.sh
#Move Backup Files To Backup Server.
#适合Linux(MySQL服务器)到Linux(备份服务器)
#$scriptsDir/rsyncBackup.sh
#if (( !$? )); then
#echo "Move Backup Files To Backup Server Success!" >> $eMailFile
# else
# echo "Move Backup Files To Backup Server Fail!" >> $eMailFile
# fi
#else
#echo "DataBase Backup Fail!" >> $emailFile
fi
#write log
echo "--------------------------------------------------------" >> $logFile
cat $eMailFile >> $logFile
#send imail
cat $eMailFile | mail -s "MySQL Backup" $eMail

增量备份
vi /tmp/mysql_daily_bak.sh
#!/bin/sh
scriptsDir='pwd'
mysqlDir='/usr/local/mysql'
dataDir=$mysqlDir/var
user=root
userPWD=root123
dataBackupDir=/tmp/mysqlbackup
dailyBackupDir=$dataBackupDir/daily
eMailFile=$dataBackupDir/email.txt
[email protected]
logFile=$dataBackupDir/mysqlbackup.log
DATE=`date -I`
HOSTNAME=`uname -n`
echo "" > $eMailFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile
#刷新日志
$mysqlDir/bin/mysqladmin -u$user -p$userPWD flush-logs
cd $dataDir
filelist=`cat mysql-bin.index`
iCounter=0
for file in $filelist
do
iCounter=`expr $iCounter + 1`
done
nextNum=0
iFile=0
for file in  $filelist
do
   binLogName=`basename $file`
nextNum=`expr $nextNum + 1`
if [[ $nextNum == $iCounter ]]; then
echo "skip lastest!" > /dev/null
else
dest=$dailyBackupDir/$binLogName
#跳过已备份的二进制日志文件
if [[ -e $dest ]]; then
echo "Skip exist $binLogName!" > /dev/null
else
# 备份日志文件到备份目录
cp $binLogName $dailyBackupDir
 if [[ $? == 0 ]]; then
iFile=`expr $iFile + 1`
 echo "$binLogName Backup Success!" >> $eMailFile
fi
fi
fi
done
if [[ $iFile == 0 ]];then
echo "No Binlog Backup!" >> $eMailFile
else
echo "Backup $iFile File(s)." >> $eMailFile
echo "Backup MySQL Binlog OK!" >> $eMailFile
fi

删除old文件
vi /tmp/rmBackup.sh
#!/bin/sh
# Name:rmBackup.sh
# PS:Delete old Backup.
# 定义备份目录
dataBackupDir=/tmp/mysqlbackup
# 删除mtime>2的日志备份文件
find $dataBackupDir -name "mysql_*.gz" -type f -mtime +2 -exec rm {} \; > /dev/null 2>&1


同步备份到备份服务器
vi /tmp/rsyncBackup.sh
#!/bin/sh
# Name:rsyncBackup.sh
#定义数据库备份目录
dataBackupDir=/tmp/mysqlbackup/
# 定义备份服务器上存放备份数据的目录
backupServerDir=/root/mysqlbackup/
# 定义备份服务器
backupServer=172.16.107.133
# 同步备份文件到备份服务器
rsync -a --delete $dataBackupDir -e ssh $backupServer:$backupServerDir > /dev/null 2>&1

恢复服务器
全部恢复
mysqldump --user=root -p --all-databases > /backup/mysql.sql
DB恢复
/usr/local/mysql/bin/mysql -uroot -pUserPWD db_name < db_name.sql

增量恢复
1.对于任何可适用的更新日志,将它们作为 mysql 的输入
% ls -t -r -1 HOSTNAME-bin* | xargs mysqlbinlog | mysql -uUser –pUserPWD
2.一般恢复
/usr/local/mysql/bin/mysqlbinlog mysql-bin.000052 | mysql -uUser -pUserPWD
定制任务
#crontab –e

10 4 * * 1-6 /tmp/mysql_daily_bak.sh   #礼拜一到礼拜六运行每天备份脚本

10 4 * * 0 /tmp/mysql_full_bak.sh   #礼拜天执行全备份的脚本