文件名称:MySQL自动备份
文件大小:21KB
文件格式:DOC
更新时间:2018-06-17 11:01:36
MySQL
MYSQL自动备份脚本 功能说明:1 自动保留前三天的数据备份2 命令参数决定要备份的数据库,可以根据需要灵活设置3 以备份的当前时间来命名打包文件,备份目录下保存的文件就有三个, 如:20061027.tar.gz , 20061029.tar.gz , 20061030.tar.gz代码如下:#! /bin/bash##this is mysql data backup sample script############################################# judge syntax,here must be one syntax at least,or else quit#if [ $# -eq 0 ];then echo "no database choosed,quit" exit 1fi### set some temporary variables#backuppath=/home/mybackup/mysqlpath=/usr/local/mysql/bin/### make directory for keeping backup data#mkdir -p $backuppath####set now time to name backup-data-file namenowtime=`date +"%Y%m%d"`#### keep former data of three days#cd $backuppath#filelist=`ls|wc -w`if [ $filelist -ge 3 ];then for i in `ls` do temstring=`echo $i|cut -d. -f1` temtime=$(($nowtime-$temstring)) if [ $temtime -ge 3 ];then rm -rf $i fi donefi### start backup#for j in $* do ${mysqlpath}mysqldump -uroot -pguoxin2006 --default-character-set=gb2312 -B $j>${backuppath}$j.txt if [ $? -eq 0 ];then echo "now backuping database: $j,please wait ....." else echo "something wrong for dump databse $j,quit!" exit 1 fi done### tar all the datafile backuped just now#cd $backuppathtar -czvf ${nowtime}.tar.gz *.txtif [ $? -eq 0 ];then echo "tar work successfully!"else echo "something wrong with tar work,now quit!"fi### delete txt file if exists#rm -rf ${backuppath}*.txt###################################################