mysql scheduled backup in linux Environment

In my blog website, Nacos is used as the configuration and registration center and associated with the database. Since the data, data and website settings are stored in the database, I always want to make a backup. So I found a relatively simple scheduled backup mode. Of course, it is based on linux.

Method: use the timer in linux system to execute shell script regularly and export the database.

It should be noted here that the newly created cron task will not be executed immediately. It will take at least 2 minutes. Of course, you can restart cron to execute it immediately.

First, create your own shell script for database export. I also use it written by others

Original author link: Mushroom blog: http://www.moguit.cn

1. Set a directory bak as the storage point of backup files and export the data of the corresponding database

Where - uroot -p'admin 'is equivalent to - u root -p admin, i.e. user name and password

If mysqldump is not in / usr/local/bin, command not found will appear

At this time, there are two options: 1. Establish a soft link from the actual path to the file with the same name under bin, and 2. Write the full path of mysqldump.

The following is the script for backing up the database: provider: Mushroom blog: http://www.moguit.cn

#!/bin/sh

bseDir="/bak";

cd "$baseDir";

echo "Start backing up database";

#Start backup mogu_blog database

echo `mysqldump  -uroot -p'admin' --default-character-set=utf8  mogu_blog > mogu_blog_$(date +%Y%m%d).sql`;

#Start backup mogu_picture database

echo `mysqldump  -uroot -p'w1313375' --default-character-set=utf8  mogu_picture > mogu_picture_$(date +%Y%m%d).sql`;

echo "Packaging backup files in progress"

echo `tar zcvf mogu_blog_$(date +%Y%m%d).tar.gz  mogu_blog_$(date +%Y%m%d).sql mogu_picture_$(date +%Y%m%d).sql `;

echo `rm -rf mogu_blog_$(date +%Y%m%d).sql`;

echo `rm -rf mogu_picture_$(date +%Y%m%d).sql`;

echo "Backup data complete";

oldDate=`date --date='8 day ago' +%Y%m%d`;

#Delete backup of current date -8

echo `rm -rf mogu_blog_$oldDate*`;

echo "delete $oldDate Your backup was successful"

2. Install crontab

explain:

crontab is used to allow users to execute programs at fixed times or intervals. In other words, it is similar to the user's schedule.

- u user refers to setting the schedule of the specified user. This premise is that you must have its permission (for example, root) to specify the schedule of others. If - u user is not used, it means setting your own schedule.

1,Install first crond

yum install crontabs

2,crond Start command

systemctl start crond

3,close

systemctl stop crond

4,Open self starting

systemctl enable crond

5,View time tasks

crontab -l

6,Set task

crontab -e    (The generated task is in/var/spool/cron/Under, the file name is the login user name)

3. It is mainly necessary to specify the time when setting tasks.

It is worth noting that when specifying the crond task, such as sh startup SH because crond does not introduce variables, you can either import them yourself or write the full path} to / bin / sh startup Otherwise, the timer executes, but it has no effect.

The timer time format is: f1 f2 f3 f4 f5 program
  • Where f1 is the minute, f2 is the hour, f3 is the day of the month, f4 is the month, and f5 is the day of the week. Program represents the program to be executed.
  • When f1 is * it means that the program should be executed every minute, when f2 is * it means that the program should be executed every hour, and so on
  • When f1 is a-b, it means to execute from minute a to minute b; when f2 is a-b, it means to execute from hour a to hour b, and so on
  • When f1 is * / N, it means to execute every n minute intervals, f2 is * / N, it means to execute every n hour intervals, and so on
  • When f1 is a, b, c When means a, b, c Minutes to execute, f2 is a, b, c Time means a, b, c It takes hours to execute, and so on
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- Day of the week (0 - 7) (Sunday is 0)
|    |    |    +---------- month (1 - 12) 
|    |    +--------------- What day of the month (1 - 31)
|    +-------------------- hour (0 - 23)

Some examples:

0 */2 * * * /sbin/service httpd restart  It means restart every two hours apache 

50 7 * * * /sbin/service sshd start  It means open at 7:50 every day ssh service 

50 22 * * * /sbin/service sshd stop  It means close at 22:50 every day ssh service 

0 0 1,15 * * fsck /home  Check on the 1st and 15th of each month/home disk 

1 * * * * /home/bruce/backup  The first minute of every hour /home/bruce/backup This file 

00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;  Every Monday to Friday at 3 o'clock, in the catalogue/home In, find the file named*.xxx And delete the file 4 days ago.

30 6 */10 * * ls  It means that it is executed at 6:30 on the 1st, 11th, 21st and 31st of each month ls command

4. My execution and troubleshooting process:

According to the tutorial, I set it up. In order to test, I ran a database backup once a minute. As a result, the machine burst, cpu and memory. So I kept kill ing the process and stop the timer.

Log: / var/log/cron

Here are some of my implementation results:

The 3:15 running timer is set:

I modified the sh file and changed the timer time to 3:15. You can see that my script was executed at 3:15.

Finally, you can see that the database backup was created successfully

Keywords: Linux Database MySQL

Added by henka on Sat, 18 Dec 2021 03:11:53 +0200