mysql增量备份脚本
#!/bin/bash
dateDIR=$(date -d "yesterday" +"%y-%m-%d")
mkdir -p /data/backup/mysql/binlog/$dateDIR
##刷新缓存中的日志
mysqladmin -uroot -p flush-logs
#7天前的时间
TIME=$(date "-d 7 day ago" +"%Y-%m-%d %H:%M:%S")
#24小时前的时间
StartTime=$(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")
#删除7天前的2进制文件
mysql -uroot -p -e "purge master logs before '${TIME}';"
#针对每个数据库
for db in $(mysql -uroot -p -e "show databases" | grep -e "zabbixdatabases" -e"serverdatabases")
do
#针对所有的日志文件
for log in $(cat /var/lib/mysql/mysql-bin.index)
do
#备份每个数据库24小时之前到现在时间的所有日志,写入到相应文件中
mysqlbinlog -uroot -p -d $db --start-datetime="$StartTime" ${log} > /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}.sql
done
#tar jcvf /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}.tar.bz2 /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR} 2&>/dev/null
#rm -rf /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}
done
#删除20天以前的增量备份文件和目录
find /data/backup/mysql/binlog/* -mtime +19 -type d -exec rm -rf {} \;
mysql全备份脚本
1 #!/bin/bash
2
3 date=`date +%Y-%m-%d`
4
5 bakdir="/home/mysqlbackup"
6 mvdir="/home/mysqlbackup/mv"
7 logFile="/home/mysqlbackup/log/mysqlbak.log"
8 user="root"
9 pass="passwd"
10 filename="database-$date.sql"
11 filenamee="databases-$date.sql"
12
13 if [ ! -d $bakdir ];then mkdir -pv $bakdir;fi
14
15 cd $bakdir
16 mv $bakdir/$filename.tgz $mvdir/
17 mv $bakdir/$filenamee $mvdir/
18 rm -f $bakdir/$filename
19
20 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B dabases -u$user -p$pass > $filename
21 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B databases -u$user -p$pass >
$filenamee 22
23 echo "开始打包备份文件"
24 echo "$filename $filenamee 数据库备份成功,备份文件在192.168.0.10:/root/home/mysqlbackup目录下;" > $logFile
25 /bin/tar czvf $filename.tgz $filename
26
27 /bin/find $mvdir -type f -ctime +3 -exec rm -fr {} \;
28 #/bin/tar czvf $filenamee.tgz $filename
29 scp $bakdir/$filename.tgz 192.168.0.10:/root/home/mysqlbackup
30 scp $bakdir/$filenamee 192.168.0.10:/root/home/mysqlbackup
31
32 mv $bakdir/$filename.tgz $mvdir/
33 mv $bakdir/$filenamee $mvdir/
34 rm -f $bakdir/$filename
35
36 ssh 192.168.0.10 '/bin/find /root/home/mysqlbackup -type f -atime +3 -exec rm -fr {} \;'
37 #只保留最近四天的数据
~
或者:
2 # Program
3 # # use mysqldump to Fully backup mysql data per week!
4 # # History
5 # # 2015-04-02 jin first
6 # # path
7 # # ....
8 # 备份文集的目录,日志目录,时间,用户,密码
9 BakDir="/root/djback/backup"
10 LogFile="/root/djback/backup/bak.log"
11 Date=`date +%Y%m%d`
12 Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
13 user="root"
14 pass="passwd"
15 #如果文件不存在就创建
16 if [ ! -d $BakDir ];then mkdir -pv $BakDir;fi
17 cd $BakDir
18 #备份文件名字和压塑后的问件名
19 DumpFile=data-$Date.sql
20 GZDumpFile=data-$Date.sql.tgz
21 DumpFilee=databa-$Date.sql
22 GZDumpFilee=databa-$Date.sql.tgz
23 #备份qiv库
24 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --master-data=2 --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B data -u$user -p
$pass > $BakDir/$DumpFile 25 #备份qiv_secretcode库
26 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --master-data=2 --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B databa
e -u$user -p$pass > $BakDir/$DumpFilee 27 sed -n "22p" $BakDir/$DumpFilee | awk -F'=|,|;' '{print $2,$4}' >$BakDir/postion
28 # 压缩备份文件
29 /bin/tar czvf $GZDumpFile $DumpFile
30
31 /bin/tar czvf $GZDumpFilee $DumpFilee
32 # 删除以前的备份文件
33 /bin/rm $DumpFile
34 Last=`date +"%Y年%m月%d日 %H:%M:%S"`
35 echo "开始:$Begin 结束:$Last $GZDumpFile succ
36 sleep 2
37 echo "(备份成功 备份的目录为/root/djback/backup下)"
38 cd $BakDir/daily
39 rm -f *
40 echo "开始上传文件到192.168.0.10的/root/home/mysqlbackup/下"
41 scp /root/djback/$DumpFile 192.168.0.10:/root/home/mysqlbackup/
42 scp /root/djback/$DumpFilee 192.168.0.10:/root/home/mysqlbackup/
43 echo "上传成功!"
转载于:https://blog.51cto.com/kongshuai/1713270