网上搜集并测试整理
#!/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/mysql LogFile=/root/backup/mysql/bak_$DBName.log #Setting End NewFile=$BackupPath/"$DBName"_$(date +%y%m%d).tar.gz DumpFile="$DBName"_$(date +%y%m%d).sql OldFile=$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 fi echo "-------------------------------------------" >> $LogFile
本文出自 “微所欲为——微生活” 博客,请务必保留此出处http://vsfor.blog.51cto.com/4165449/1586531
原文地址:http://vsfor.blog.51cto.com/4165449/1586531