Cron Bash AutoBackup Database

Cron Bash AutoBackup Database

备份策略:

  1. 每天定时备份数据库一次

  2. 备份数据只保留最近三天

Cron 任务编写

10 10 * * * /home/backup_mysql > /tmp/cronjob.txt 2>&1

Bash脚本

#!/bin/bash
# server mysql backup

# backup init
dir=/home/
user=root
pwd=
server=admin
database=
date=`date +%Y_%m_%d`
filename="$server"_"$date".sql

#Script for exec
cd $dir
mysqldump -u $user -p$pwd --skip-lock-tables $database > "$filename"
find $dir -type f -mtime +3 -delete;

注意事项

  1. 编写Cron时,注意Server的时区, 用date检查下

Reference

  1. crontab.guru

  2. Mysqldump with Modern MySQL

标签: none

添加新评论