自动备份数据库

技术分享 Story 83浏览 0评论
摘要

自动备份数据库

自动备份数据库
#!/bin/sh

BACKUP_BASE_DIR="/data/backup/database"
max_old_backup=12

clean_old_backup()
{
count=0
for name in `ls $BACKUP_BASE_DIR|grep -E '[0-9]{6,6}\-[0-9]{6,6}' | sort -r`
do
count=$((count+1))
if [ $count -gt $max_old_backup ] ; then
echo -n "Cleaning old backup: $name... "
if [ "$testmode" = 1 ] ; then
echo -n "rm -fr $BACKUP_BASE_DIR/$name"
else
rm -fr $BACKUP_BASE_DIR/$name
fi
echo " Done."
else
echo "Keeping old backup: $name"
fi
done
}

clean_old_backup
tar -zcf /data/backup/database/test.`date '+%Y%m%d-%H%M%S'`.tar.gz /var/db/mysql/test/

ee /etc/crontab
0 */4 * * * /root/mysql.sh >> /data/backup/backlog/tar.log

每四小时执行一次备份.只保留12个数据库备份

转载请注明:成长的对话 » 自动备份数据库