保留每天備份的數(shù)據(jù)是件不太現(xiàn)實的事,做好的做法是保留前三天的
備份數(shù)據(jù)。把備份的數(shù)據(jù)打包并壓縮,文件名以系統(tǒng)時間命名,打包后的
備份文件放在一個特定的文件夾下面,實際上,只要是以時間命名備份文件,
ls 命令后,文件將自動按時間排序的,這樣就可以方便的刪除三天以前的備份文件,于是
就保證了服務器上每天都保留著最近三天的數(shù)據(jù)庫備份
#/usr/bin/dbbackup
#! /bin/bash
dbsum=$#
if [ "${dbsum}" -eq 0 ];then
echo "Error:no database chosed"
exit 1
fi
mkdir -p /backup/
backdir=/backup/
touch /var/log/dbbackup.log
datetime=`date +"%Y%m%d"`
filesum=`ls ${backdir} | wc -l`
if [ "${filesum}" -ge 3 ];then
cd ${backdir}
rm -rf `ls | head -1`
fi
cd /usr/local/mysql/bin
for i in $*;do
echo "backing up for database $i starting ..."
mysqldump -uroot -ppassword -e --default-character-set=utf8 $i > ${backdir}$i.sql
echo "backing up for database $i completed"
done
echo "tar and gzip the backed file now ..."
cd ${backdir}
tar -c *.sql | gzip >./${datetime}.tar.gz
rm -rf *.sql
echo "all success ! you can find the backed file in ${backdir} suffixed by .tar.gz"
exit 0
編輯crontab,每晚11:30備份數(shù)據(jù)
crontab -e 加入如下內(nèi)容:
30 23 * * * /usr/bin/dbbackup dbname1 dbname2 > /var/log/dbbackup.log 2>&1
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請
點擊舉報。