网上搜集并测试整理
#!/bin/bash #This is a ShellScript For Auto DB Backup #Powered by aspbiz Changed by Jeen#2004-09 Changed 2014-12#Setting #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置DBName="test"DBUser="root"DBPasswd="root"BackupPath=/root/backup/mysqlLogFile=/root/backup/mysql/bak_$DBName.log#Setting End NewFile=$BackupPath/"$DBName"_$(date +%y%m%d).tar.gz DumpFile="$DBName"_$(date +%y%m%d).sqlOldFile=$BackupPath/"$DBName"_$(date +%y%m%d --date='7 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 echo "--------------------------" >> $LogFile if [ -f $NewFile ] ; then echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile else cd $BackupPath if [ -z $DBPasswd ] ;then mysqldump -u $DBUser --opt $DBName > $DumpFile else mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile fi tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -rf $DumpFile fiecho "-------------------------------------------" >> $LogFile