创建备份目录
mkdir /data/dbback/mysql
创建备份shell脚本
vim /data/dbback/bkDatabaseName.sh
db_user="root"
db_passwd="root"
db_name="db_test"
# the directory for story your backup file.you shall change this dir
backup_dir="/data/dbback/mysql"
# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y%m%d%H%M%S")"
mysqldump -u$db_user -p$db_passwd $db_name > "$backup_dir/$db_name"_"$time.sql"
添加可执行权限
chmod +x bkDatabaseName.sh
创建定时任务
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查看服务状态
添加计划任务
crontab -e
00 01 * * * /data/dbback/bkDatabaseName.sh(每天凌晨一点备份数据库)
添加删除备份文件的脚本
find /home/gk_hsa/dbback/mysql/data -type f -name "*.sql" -mtime +5 -exec rm -rfv {} \;