此脚本自动备份mysql数据库到本地磁盘,并自动将备份拷贝到其他机器和自动清理过期备份,写日志,完成后发邮件给管理员。程序中使用了ssh自动登陆。
主程序:bakcup_mysqldb.sh
#author: bill
#date: 2012-03-27
####################################################
DATE=`date +"%Y%m%d%H%M%S"`
RunPath=/root/backup
MysqlDump=/usr/local/mysql/bin/mysqldump
BakPath=/home/backup
SourceHost=192.168.0.6
AllDir=$BakPath/$SourceHost/mysqldb
SourceDir=$AllDir/$DATE
DestHost=192.168.0.8
RemoteBakPath=/home/backup
DataType=mysqldb
RemoteDestDir=$RemoteBakPath/$SourceHost/$DataType
mkdir -p $AllDir/$DATE
BakDir=$AllDir/$DATE
mkdir -p $AllDir/log
LogFile=$AllDir/log/$DATE.log
RetainDay=30
##################################################
echo "Backup mysqldb start at $(date +"%Y-%m-%d %H:%M:%S")" > $LogFile
echo "--------------------------------------------------" >> $LogFile
$MysqlDump --all-databases | gzip > $AllDir/$DATE/mysqldb_all.sql.gz
if [ $? -eq 0 ];then
echo "backup allmysqldb done into mysqldb_all.sql.gz">>$LogFile
else
echo "backup allmysqldb fail into mysqldb_all.sql.gz">>$LogFile
fi
echo "--------------------------------------------------" >> $LogFile
echo "Backup mysqldb end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo " " >> $LogFile
#############################################################
#delete old backup file
echo "Delete old backup start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------" >> $LogFile
AllBakDir=`find $BakPath -type d`
for eachdir in $AllBakDir
do
dirname=${eachdir##*/}
dirdate=`echo ${dirname:0:8}`
if [ `echo $dirdate | grep '[0-9]\{8\}'` ] && [ $dirdate -le $(date --date="$RetainDay days ago" +"%Y%m%d") ]
then
rm -rf $eachdir /dev/null
if [ $? -eq 0 ];then
echo " [$eachdir],Delete old bakup directory success!" >> $LogFile
else
echo " [$eachdir],Delete old bakup directory fail!" >> $LogFile
fi
fi
done
echo "--------------------------------------------------" >> $LogFile
echo "Delete old backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo " " >> $LogFile
#################################################################
#delete old logfile
echo "Delete old log file start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------" >> $LogFile
cd ${AllDir}/log
ExistLogFile=`ls ${AllDir}/log`
for EachLogFile in $ExistLogFile
do
FileDate=`echo ${EachLogFile:0:8}`
if [ `echo $FileDate | grep '[0-9]\{8\}'` ] && [ $FileDate -le $(date --date="$RetainDay days ago" +"%Y%m%d") ]
then
rm -rf $AllDir/log/$EachLogFile > /dev/null
if [ $? -eq 0 ];then
echo "[$AllDir/log/$EachLogFile],Delete old log file success!" >> $LogFile
else
echo "[$AllDir/log/$EachLogFile],Delete old log file fail!" >> $LogFile
fi
fi
#echo " " >> $LogFile
done
echo "--------------------------------------------------" >> $LogFile
echo "Delete old log file end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo " " >> $LogFile
###############################################################
echo "Back file to remote host [$DestHost] start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------" >> $LogFile
cd $RunPath
#call make remote host dir
./mkdir_remote.sh $SourceHost $DestHost $DATE $RemoteBakPath $DataType
scp -P 33 -rp -i /root/.ssh/id_rsa_$SourceHost $SourceDir $DestHost:$RemoteDestDir
if [ $? = 0 ];then
echo "Backup $SourceDir to $DestHost Success!" >> $LogFile
else
echo "Backup $SourceDir to $DestHost Failed!" >> $LogFile
fi
echo "--------------------------------------------------" >> $LogFile
echo "Back file to remote host [$DestHost] end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
mail -s "Backup mysqldb log @ $SourceHost" [email protected] < $LogFile
exit 0
子程序: mkdir_remote.sh
#!/usr/bin/expect
set timeout 2
set SourceHost [lindex $argv 0]
set DestHost [lindex $argv 1]
set DATE [lindex $argv 2]
set RemoteBakPath [lindex $argv 3]
set DataType [lindex $argv 4]
spawn ssh -p33 -i /root/.ssh/id_rsa_$SourceHost $DestHost
expect eof
send "mkdir -p $RemoteBakPath/$SourceHost/$DataType/$DATE\n"
expect eof
exit