根据要求写下MySQL备份脚本,要求如下:对指定数据库进行日备份并且将备份文件保存一周,对一周以外文件自动删除。
1、创建shell脚本
[root@server18 mysql]$ mkdir -p /home/mysql/DB_BAK/day_bak/
DB_NAME='tdc_ntt_debug'
USER='mysql'
PWD='mysql'
TIME=$(date +%Y%m%d)
DEL_TIME=$(date --date="7 days ago" +%Y%m%d)
DB_BAK_NAME=tdc_debug${TIME}.sql
DB_DEL_NAME=tdc_debug${DEL_TIME}.sql
BACKUP_PATH='/home/mysql/DB_BAK/day_bak/'
echo $BACKUP_PATH$DB_BAK_NAME
if test -f $BACKUP_PATH$DB_DEL_NAME
then
echo "Delete history dump file"
rm -f $BACKUP_PATH$DB_DEL_NAME
fi
mysqldump -u $USER -p$PWD $DB_NAME > $BACKUP_PATH$DB_BAK_NAME
2.修改计划任务/etc/crontab
[root@server18 ~]# crontab -e
30 21 * * * root /home/mysql/DB_BAK/day_bak/day_bak.sh
每天晚上21:30进行MySQL备份,将备份文件保留7天。