一、查看 MySQL 是否安装了 crontab
yum -y install vixie-cron
yum -y install crontabs
二、crontab 常用命令
# 查看进程是否正在进行
ps aux | grep crond
# 查看运行状态
systemctl status crond 或者 service crond status
# 启动服务
systemctl start crond 或者 service crond status
# 关闭服务
systemctl stop crond 或者 service crond status
# 重启服务
systemctl restart crond 或者 service crond restart
# 重新载入配置
systemctl reload crond
# 加入开机启动
systemctl enable crond
# 查看定时任务
crontab -l
# 编辑
crontab -e
三、编写 dump_mysql.sh 脚本
方式一:简单版
# 设置mysql的登录用户名和密码(根据实际情况填写)
mysql_user="root"
mysql_password="123456"
mysql_host="101.133.170.222"
mysql_port="3306"
mysql_charset="utf8"
# 备份文件存放地址(根据实际情况填写)
backup_location=/data/mysql/bakup
# 是否删除过期数据
expire_backup_delete="ON"
expire_days=7
backup_time=`date +%Y%m%d%H%M`
backup_dir=$backup_location
welcome_msg="Welcome to use MySQL backup tools!"
# 备份指定数据库中数据(此处假设数据库是ruoyi )mysql容器里面找到mysqldump我的路径是/usr/bin/mysqldump
docker exec mysql /usr/bin/mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B ruoyi > $backup_dir/mysql-$backup_time.sql
# 删除过期数据
if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then
`find $backup_location/ -type f -mtime +$expire_days | xargs rm -rf`
echo "Expired backup data delete complete!"
fi
方式二:复杂版
mysql_user="root" #MySQL备份用户
mysql_password="123456" #MySQL备份用户的密码
mysql_host="101.133.170.222"
mysql_port="3306"
mysql_charset="utf8" #MySQL编码0 2 * * * docker exec -i mysql sh /root/mysql8.0.20/mysqlbfplus/run.sh
backup_db_arr=("ruoyi") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
backup_location=/data/mysql/bakup #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=7 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效
backup_time=`date +%Y%m%d%H%M` #定义备份详细时间
backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
#backup_day_ago=`date -d 'expire_days days ago' +%Y-%m-%d` #备份之前的日期
backup_dir=$backup_location/$backup_Ymd #备份文件夹全路径
welcome_msg="Welcome to use MySQL backup tools!" #欢迎语
mysql_ps=`ps -ef |grep mysql |wc -l`
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
echo "ERROR:MySQL is not running! backup stop!"
exit
else
echo $welcome_msg
echo "MySQL connect ok! Please wait......"
fi
# 判断有没有定义备份的数据库,如果有定义则开始备份,否则退出备份
if [ "$backup_db_arr" != "" ];then
#dbnames=$(cut -d ',' -f1-5 $backup_database)
#echo "arr is (${backup_db_arr[@]})"
for dbname in ${backup_db_arr[@]}
do
echo "database $dbname backup start..."
`mkdir -p $backup_dir`
docker exec mysql /usr/bin/mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz
flag=`echo $?`
if [ $flag == "0" ];then
echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz"
else
echo "database $dbname backup fail!"
fi
done
else
echo "ERROR:No database to backup! backup stop"
exit
fi
# 如果开启了删除过期备份,则进行删除操作
if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then
#`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
`find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
echo "Expired backup data delete complete!"
fi
方式三:docker定时备份,根据备份数量删除
#用户名
mysql_user="root"
#密码
mysql_password="root"
mysql_host="101.133.170.222"
#端口号
mysql_port="3306"
#将要备份的数据库
database_name="ruoyi"
#编码格式
mysql_charset="utf8"
#保存备份文件最多个数
count=3
#备份保存路径
backup_path=/data/mysql/bakup
#日期
date_time=`date +%Y-%m-%d-%H-%M`
#如果文件夹不存在则创建
if [ ! -d $backup_path ];
then
mkdir -p $backup_path;
fi
#查看MySQL是否在运行
mysql_ps=`ps -ef |grep mysql |wc -l`
echo "查看MySQL是否在运行:$mysql_ps" >> $backup_path/dump.log
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
echo "ERROR:MySQL is not running! backup stop!" >> $backup_path/dump.log
exit
else
echo "Welcome to use MySQL backup tools!" >> $backup_path/dump.log
echo "MySQL connect ok! Please wait......" >> $backup_path/dump.log
fi
#开始备份
echo "开始备份..." >> $backup_path/dump.log
docker exec h3_mysql /usr/bin/mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $database_name --default-character-set=$mysql_charset > $backup_path/$database_name-$date_time.sql
#开始压缩
cd $backup_path
tar -zcvf $database_name-$date_time.tar.gz $database_name-$date_time.sql
#更新备份日志
echo "压缩原文件 create $backup_path/$database_name-$date_time.tar.gz" >> $backup_path/dump.log
#删除源文件
rm -rf $backup_path/$database_name-$date_time.sql
echo "压缩后删除原文件 delete $backup_path/$database_name-$date_time.sql" >> $backup_path/dump.log
#找出需要删除的备份
delfile=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | head -1`
#判断现在的备份数量是否大于阈值
number=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | wc -l`
if [ $number -gt $count ]
then
#删除最早生成的备份,只保留count数量的备份
rm $delfile
#更新删除文件日志
echo "$date 删除 $count 前的文件 delete $delfile" >> $backup_path/dump.log
fi
方式四:不是 docker 安装备份
#用户名
mysql_user="root"
#密码
mysql_password="root"
mysql_host="101.133.170.222"
#端口号
mysql_port="3306"
#将要备份的数据库
database_name="ruoyi"
#编码格式
mysql_charset="utf8"
#保存备份文件最多个数
count=3
#备份保存路径
backup_path=/data/mysql/bakup
#日期
date_time=`date +%Y-%m-%d-%H-%M`
#如果文件夹不存在则创建
if [ ! -d $backup_path ];
then
mkdir -p $backup_path;
fi
#查看MySQL是否在运行
mysql_ps=`ps -ef |grep mysql |wc -l`
echo "$date_time 查看MySQL是否在运行:$mysql_ps" >> $backup_path/dump.log
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
echo "$date_time ERROR:MySQL is not running! backup stop!" >> $backup_path/dump.log
exit
else
echo "$date_time Welcome to use MySQL backup tools!" >> $backup_path/dump.log
echo "$date_time MySQL connect ok! Please wait......" >> $backup_path/dump.log
fi
#开始备份
echo "$date_time 开始备份...$backup_path" >> $backup_path/dump.log
docker exec h3_mysql /usr/bin/mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $database_name --default-character-set=$mysql_charset > $backup_path/$database_name-$date_time.sql
echo "$date_time 备份完成...$backup_path" >> $backup_path/dump.log
#开始压缩
cd $backup_path
tar -zcvf $database_name-$date_time.tar.gz $database_name-$date_time.sql
#更新备份日志
echo "$date_time 压缩原文件 create $backup_path/$database_name-$date_time.tar.gz" >> $backup_path/dump.log
#删除源文件
rm -rf $backup_path/$database_name-$date_time.sql
echo "$date_time 压缩后删除原文件 delete $backup_path/$database_name-$date_time.sql" >> $backup_path/dump.log
#找出需要删除的备份
delfile=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | head -1`
#判断现在的备份数量是否大于阈值
number=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | wc -l`
if [ $number -gt $count ]
then
#删除最早生成的备份,只保留count数量的备份
rm $delfile
#更新删除文件日志
echo "$date_time 删除 $count 前的文件 delete $delfile" >> $backup_path/dump.log
echo "$date_time 备份成功" >> $backup_path/dump.log
fi
四、配置定时任务
时间格式:
# 常用时间格式
每五分钟执行 */5 * * * *
每小时执行 0 * * * *
每天执行 0 0 * * *
每周执行 0 0 * * 0
每月执行 0 0 1 * *
每年执行 0 0 1 1 *
# crontab 文件的格式
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
minute: 区间为 0 – 59
hour: 区间为0 – 23
day-of-month: 区间为0 – 31
month: 区间为1 – 12. 1 是1月. 12是12月.
Day-of-week: 区间为0 – 7. 周日可以是0或7.
常用时间:
# 执行 crontab -e 命令,写入以下命令保存,每 5 分钟执行一次
*/5 * * * * sh /data/mysql/dump_mysql.sh
# 每天凌晨 5 点执行
0 5 * * * sh /data/mysql/dump_mysql.sh
© 版权声明
THE END
暂无评论内容