MySQL自动备份脚本+Linux计划任务

通过参数指定备份数据库
保留最近7天备份文件

vi /data/script/mysql-backup-day.sh

#!/bin/sh

if [ $# != 1 ] ; then
echo "dbname error"
exit 1;
fi

# db="testdb"
db=$1
user="root"
password="root"
backup_dir="/data/backup/mysql-day/"
mysqldump="/usr/local/mysql/bin/mysqldump"

date=`date -d yesterday +%Y%m%d`
backup_file=${backup_dir}${db}"."${date}".sql"
$mysqldump -u$user -p$password $db -lF > $backup_file

# delete 7 day ago
date=`date -d '7 day ago' +%Y%m%d`
backup_file=${backup_dir}${db}"."${date}".sql"
rm -f $backup_file

chmod u+x /data/script/mysql-backup-day.sh
mkdir -p /data/backup/mysql-day
crontab -e

0 1 * * * /data/script/mysql-backup-day.sh testdb

 

发表评论