Linux备份mysql cron代码
1 前言
Linux定时备份mysql,增加数据冗余性
2 代码
xxxdbBackup.sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # !/bin/sh dd= "$(date +" %Y%m%d%H%M%S ")" # 保存备份个数,备份31天数据 number=31 # 备份保存路径 backup_dir=/var/lib/mysql/backup # 将要备份的数据库 database_name=xxxdb # 如果文件夹不存在则创建 if [ ! -d $backup_dir ]; then mkdir -p $backup_dir; fi # 执行备份命令 /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/conf.d/mysql.cnf xxxdb >$backup_dir/$database_name-$dd.sql # 写创建备份日志 echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt # 找出需要删除的备份 delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1` # 判断现在的备份数量是否大于$number count =`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l` if [ $ count -gt $number ] then # 删除最早生成的备份,只保留number数量的备份 rm $delfile # 写删除文件日志 echo "delete $delfile" >> $backup_dir/log.txt fi |
xxxbacklog.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # !/bin/sh dd = "$(date +" %Y%m%d%H%M%S ")" # 保存备份个数,备份31天数据 number=31 # 备份保存路径 project_dir= /home/sample/pro/demo backup_dir= /home/sample/pro/demo/backup/log # 将要备份的日志 backup_filename=logapi # 如果文件夹不存在则创建 if [ ! -d $backup_dir ]; then mkdir -p $backup_dir; fi # 执行备份命令 cp /home/sample/pro/demo/logapi .log $backup_dir/$backup_filename-$ dd .log #cp /dev/null > $project_dir/logapi.log #echo "logapi.log has been backup and clear now" # 写创建备份日志 echo "create $backup_dir/$backup_filename-$dd.log" >> $backup_dir /record .txt # 找出需要删除的备份 delfile=` ls -l -crt $backup_dir/*.log | awk '{print $9 }' | head -1` # 判断现在的备份数量是否大于$number count=` ls -l -crt $backup_dir/*.log | awk '{print $9 }' | wc -l` if [ $count -gt $number ] then # 删除最早生成的备份,只保留number数量的备份 rm $delfile # 写删除文件日志 echo "delete $delfile" >> $backup_dir /record .txt fi |
backup mysql cron配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | crontab crontab -l # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields ( for 'any' ).# # Notice that tasks will be started based on the cron 's system # daemon' s notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with : # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 0 1 * * * /bin/sh /var/lib/mysql/sh/xxxdbBackup.sh |
3 小结
略
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-03-09 [学习笔记]JS 数组Array push相关问题