mysql备份脚本
PY
重要数据库表备份
#!/bin/env python
import os
a = ['表','表','表']
b = ['表','表','表']
bak_dir = '/home/...'
allbak_dir = '/home/...'
for i in a:
os.system('mysqldump -hX.X.X.X -uroot -ppassword zx '+i+ '>'+bak_dir + i+'.sql')
for ii in b:
os.system('mysqldump -hX.X.X.X -uroot -ppassword wde '+ii+ '>'+bak_dir+ ii+'.sql')
os.system('mysqldump -hX.X.X.X -uroot -ppassword db_namne> '+allbak_dir+'`date +%Y.%m.%d`XXX.sql')
os.system('mysqldump -hX.X.X.X -uroot -ppassword db_namne> '+allbak_dir+' `date +%Y.%m.%d`XXX.sql')
shell
#!/bin/bash
BackupPath=/data/databk
Mysql_bk_dir=$BackupPath/mysql_bk_dir
LogFile=$BackupPath/backuplog
Passwd=password
mkdir -p $BackupPath
mkdir -p $Mysql_bk_dir
for i in $(mysql -p$Passwd -e "show databases\G"|grep Database |awk '{print $2}')
do
NewFile="$Mysql_bk_dir/"$i$(date +%Y%m%d).tar.gz
DumpFile="$Mysql_bk_dir/"$i$(date +%Y%m%d).sql
OldFile="$Mysql_bk_dir/"$i$(date +%Y%m%d --date='5 days ago').tgz
echo "-----------------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
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
cd $Mysql_bk_dir
/usr/local/mysql/bin/mysqldump --opt $i -p$Passwd >$DumpFile
tar -zcvf $NewFile $i$(date +%Y%m%d).sql >>$LogFile 2>&1
echo "[$NewFile]Back Success !" >>$LogFile
rm -rf $DumpFile
fi
sleep 20
done
增量备份
# vim /root/DBFullyBak.sh //添加以下内容
#!/bin/bash
# Program
# use mysqldump to Fully backup mysql data per week!
# History
# 2013-04-27 guo first
# Path
# ....
BakDir=/home/mysql/backup
LogFile=/home/mysql/backup/bak.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
/usr/local/mysql/bin/mysqldump -uroot -p123456 --quick --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFile
/bin/tar czvf $GZDumpFile $DumpFile
/bin/rm $DumpFile
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
cd $BakDir/daily
rm -f *
# cat /root/DBDailyBak.sh //内容为下
#!/bin/bash
# Program
# use cp to backup mysql data everyday!
# History
# 2013-05-02 guo first
# Path
# ....
BakDir=/home/mysql/backup/daily
BinDir=/home/mysql/data
LogFile=/home/mysql/backup/bak.log
BinFile=/home/mysql/data/mysql-bin.index
/usr/local/mysql/bin/mysqladmin -uroot -p123456 flush-logs
#这个是用于产生新的mysql-bin.00000*文件
Counter=`wc -l $BinFile |awk '{print $1}'`
NextNum=0
#这个for循环用于比对$Counter,$NextNum这两个值来确定文件是不是存在或最新的。
for file in `cat $BinFile`
do
base=`basename $file`
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=`expr $NextNum + 1`
if [ $NextNum -eq $Counter ]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去。
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
fi
fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next Bakup succ! >> $LogFile
# crontab -l //内容为下
#每个星期日凌晨3:00执行完全备份脚本
0 3 * * 0 /root/DBFullyBak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /root/DBDailyBak.sh >/dev/null 2>&1
---------------------------------------------------------
恐惧是因为努力的还不够,加油 ~~---------------------------------------------