#!/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備份其二進制日志文件。