MySQL 定期备份脚本
使用mysqldump备份#!/bin/bash #This is a ShellScript. For Auto DB Backup #Powered by alex@wg927.com #2014/2/25 #Setting #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式 #默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy #默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz DBName=(mysql wm_ztcj) #DBUser=root #DBPasswd='xxx' BackupPath=/data/backup/ LogFile=/data/backup/backup.log #DBPath=/data/mysqldata/ BackupMethod=mysqldump #BackupMethod=mysqlhotcopy #BackupMethod=tar #Setting End NewFile="$BackupPath"db$(date +%y%m%d).tar.gz DumpFile="$BackupPath"db$(date +%y%m%d) OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tar.gz echo "-------------------------------------------" >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile echo "--------------------------" >> $LogFile #Delete Old File if [ -f $OldFile ] then rm -f $OldFile >> $LogFile 2>&1 echo "[$OldFile]Delete Old File Success!" >> $LogFile else echo "[$OldFile]No Old Backup File!" >> $LogFile fi if [ -f $NewFile ] then echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile else case $BackupMethod in mysqldump) # if [ -z $DBPasswd ] # then # /usr/local/mysql/bin/mysqldump -u $DBUser --opt --all-databases > $DumpFile # else /usr/local/mysql/bin/mysqldump -uroot -h127.0.0.1 --single-transaction --default-character-set=utf8 -R -E --hex-blob --all-databases --master-data=1 --opt > $DumpFile # /usr/local/mysql/bin/mysqldump -u $DBUser -p$DBPasswd --default-character-set=utf8 --triggers -R -E --hex-blob wm_ztcj --master-data=1 --opt > $DumpFile # fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -rf $DumpFile ;; mysqlhotcopy) rm -rf $DumpFile mkdir $DumpFile if [ -z $DBPasswd ] then mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1 else mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1 fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -rf $DumpFile ;; *) /etc/init.d/mysqld stop >/dev/null 2>&1 tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1 /etc/init.d/mysqld start >/dev/null 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile ;; esac fi echo "-------------------------------------------" >> $LogFile echo $(date "+%F %T") >> $LogFile
来自:http://my.oschina.net/u/1440383/blog/313803