当前位置:网站首页>[database backup] complete the backup of MySQL database through scheduled tasks
[database backup] complete the backup of MySQL database through scheduled tasks
2022-06-23 05:59:00 【Hehexue programming】
Write it at the front
I've been working on a project recently , Alibaba cloud database is used , But I only rented one , The addition, deletion, modification and query are all performed on this database , Because it will be used by users soon , therefore , I'm worried about deleting the database and running away , So I thought of the idea of backing up the database , But manual backup every time is very troublesome , So I think of scheduled task execution .
Timed task scripts
In fact, the fixed time task uses mysqldump command , Here, three databases are backed up , That is, there are three copies , These three copies are the first backup Second backup The third backup , When there is a fourth backup , It will be deleted the first time . Don't talk much , Go straight up sh Script
#!/bin/bash
# Save the number of backup
number=3
# Backup save path
backup_dir=/root/mysqlbackup
# date
dd=`date +%Y-%m-%d-%H-%M-%S`
# Backup tools
tool=mysqldump
# user name
username=root
# password
password='123456'
#host
host=127.0.0.1
# The database to be backed up
database_name=mydb1
# If the folder does not exist, create
if [ ! -d $backup_dir ];
then
mkdir -p $backup_dir;
fi
# Simple writing mysqldump -u root -p123456 users > /root/mysqlbackup/users-$filename.sql
$tool -u $username -p$password -h$host $database_name > $backup_dir/$database_name-$dd.sql
# Write create backup log
echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt
# Find the backup that needs to be deleted
delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`
# Determine whether the current number of backups is greater than $number
count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`
if [ $count -gt $number ]
then
# Delete the earliest generated backup , Only keep number Number of backups
rm $delfile
# Write delete file log
echo "delete $delfile" >> $backup_dir/log.txt
fi
Timing task
Use here crontab Timing task , direct crontab -e, Enter the following scheduled tasks
0 3 * * * /bin/sh /root/mysqlbackup/mysql_backup_script.sh
Every morning 3 Point to perform mysql_backup_script.sh Script , A more general description of scheduled tasks is given below
* * * * *
- - - - -
| | | | |
| | | | +----- What day of the week (0 - 6) ( Sunday by 0)
| | | +---------- month (1 - 12)
| | +--------------- Day of the month (1 - 31)
| +-------------------- Hours (0 - 23)
+------------------------- minute (0 - 59)
When you have finished writing the scheduled task , Direct execution crontab -l You can see . thus , You can safely delete the database .
边栏推荐
- vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
- PAT 乙等 1011 C语言
- jvm-01.指令重排
- ssm项目搭建
- jvm-01. Instruction rearrangement
- Leetcode topic analysis: factorial training zeroes
- Pat class B 1012 C language
- Pat class B 1017 C language
- 数字藏品火热背后需要强大的技术团队支持 北方技术团队
- Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
猜你喜欢

数字化工厂建设可划分为三个方面

Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration

Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler

runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)

The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming

Use of visdom

Centos7 installation of postgresql8.2.15 and creation of stored procedures

Raspberry pie assert preliminary exercise

ant使用总结(二):相关命令说明

雷达图canvas
随机推荐
True MySQL interview question (24) -- row column exchange
Excel sheet column number for leetcode topic resolution
数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
阿里云 ACK One、ACK 云原生 AI 套件新发布,解决算力时代下场景化需求
The performance of nonstandard sprintf code in different platforms
PAT 乙等 1014 C语言
新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!
MySQL面试真题(二十一)——金融-贷款逾期
Alibaba cloud object storage oss+picgo+typera implements the construction map
PAT 乙等 1013 C语言
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
【数据库备份】通过定时任务完成MySQL数据库的备份
HierarchyViewer工具找不到 HierarchyViewer位置
True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
Leetcode topic analysis: factorial training zeroes
Pat class B 1011 C language
ant使用总结(一):使用ant自动打包apk
Raspberry pie assert preliminary exercise
jvm-03.jvm内存模型