一个简单的MySQL备份Shell脚本
#!/bin/bash #thisisascriptofmysqlbackup if[!-d/mydata/data1/backup];then mkdir/mydata/data1/backup fi cd/mydata/data1/backup file=$(find.-typef-mtime-7|grep.*all.sql)#查找7天内是否有备份的文件 echo$file if[-z$file];then echo"backupalldatabases..." 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[$?-eq0];then echo"Accomplish,fileis$backupfile-all.sql!" else echo"Failure!!!" fi else#查找倒数第二大的二进制文件,作增量备份 cd.. echo"Alldatabasebackups,nowstartdoingincrementalbackups!" a=1 b=1 forfilein$(ls|grepmysql-bin|grep-vindex);do num=$(echo$file|cut-d.-f2) if[$num-gt$a];then a=$num fi done unsetnumfile num=1 forfilein$(ls|grepmysql-bin|grep-vindex);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[$?-eq0];then echo"Backupcompleted,fileis"$file1-$file2.sql"" fi fi
说明:此脚本每七天做一次完全备份,以后每天做一次增量备份,完全备份使用mysqldump客户端工具,增量备份使用mysqlbinlog备份其二进制日志文件。