当前位置:网站首页>[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 .

原网站

版权声明
本文为[Hehexue programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230408224807.html