一个简单的MySQL备份Shell脚本
#!/bin/bash #this is a script of mysql backup if [ ! -d /mydata/data1/backup ] ;then mkdir /mydata/data1/backup fi cd /mydata/data1/backup file=$(find . -type f -mtime -7 | grep .*all.sql) #查找7天内是否有备份的文件 echo $file if [ -z $file ] ;then echo "backup all databases..." backupfile=$(date +%F-%H-%M-%S) mysqldump -uroot --lock-all-tables --flush-logs --master-data=2 --all-databases > /mydata/data1/backup/"$backupfile-all.sql" if [ $? -eq 0 ] ;then echo "Accomplish,file is $backupfile-all.sql !" else echo " Failure !!! " fi else #查找倒数第二大的二进制文件,作增量备份 cd .. echo "All database backups ,now start doing incremental backups!" a=1 b=1 for file in $(ls |grep mysql-bin |grep -v index);do num=$(echo $file |cut -d. -f2 ) if [ $num -gt $a ];then a=$num fi done unset num file num=1 for file in $(ls |grep mysql-bin |grep -v index);do num=$(echo $file |cut -d. -f2 ) if [ $num -gt $b -a $num -ne $a ] ; then b=$num tmp=$file fi done file1=$(echo $tmp|cut -d. -f1) file2=$(echo $tmp|cut -d. -f2) mysqlbinlog $tmp > /mydata/data1/backup/"$file1-$file2.sql" if [ $? -eq 0 ] ;then echo "Backup completed,file is "$file1-$file2.sql"" fi fi
说明:此脚本每七天做一次完全备份,以后每天做一次增量备份,完全备份使用mysqldump客户端工具,增量备份使用mysqlbinlog备份其二进制日志文件。
相关推荐
huha 2020-10-16
laisean 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
JohnYork 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
wangzhaotongalex 2020-09-22
流年浅滩 2020-10-23
liujianhua 2020-10-22
woaimeinuo 2020-10-21
tufeiax 2020-09-03
laisean 2020-09-01
vvu 2020-09-16
libao 2020-09-16
Yyqingmofeige 2020-08-18
zhushixia 2020-08-17