Linux use regular tasks automatically back up the database and delete the file n days ago

First, in the actual production environment encountered need to back up the database, or database server crashes prevent data corruption, can not save the situation, a situation we can deploy MySQL master database to back up the primary active preparation, the machine is not enough resources we deploy MySQL when the primary master mutual support, can not be taken to such a database manually scheduled backups cumbersome operation, we can use the Linux crontab scheduled tasks to automated backup database

1, let's look at our needs, for example, now requires name every night 1:30 to automatically backup mysql MySQL database for the library to a specified directory, such as files in the root directory mysql_backup folder, the backup command that follows

/ usr / bin / -Uusername -Ppassword the mysqldump MySQL> / the root / mysql_backup / MySQL _ $ (DATE the Y% m%% + D_% H% M% S) .sql
"the Username" is a user of our database

"Password" is the password for this user

Database "mysql" as we need to back up in a database

Because we look at the database backup every night, so after backing up our backup time to time to name the backup file is the "mysql _ $ (date +% Y% m% d_% H% M% S) .sql", for example, I 30 January 2019 13:30 full backup of the database is the "mysql_20190130_013000.sql".

2, our needs are in every night to back up a bit, then generates a file every day, a long time hard disk will be filled, and before long there is not much data to retain meaning, then we can back up every day At the same time delete the backup data prior to a period of time, for example, we need to remove the back up to 30 days before

the Find / root / mysql_backup / -mtime +30 The -type f | xargs RM -f
"/ root / mysql_backup /" for our backup files saved directory

"-Mtime +30" is set for 30 days before

"-Type f" indicates that the file type is looking for

This line of action is to complete the command: Find / root / under mysql_backup / directory file 30 days prior to and deleted.

3, delete the backup with the command we have written, then we can let the system automatically to perform both tasks daily by crontab

Create a task script mysql_autobackup.sh, we have just written two commands to file and to the highest authority

! # / bin / the bash
/ usr / bin / -Uusername -Ppassword the mysqldump MySQL> / the root / mysql_backup / MySQL _ $ (DATE the Y% m%% + D_% H% M% S) .sql
Find / the root / mysql_backup / - sdadmin name '. * SQL *' -mtime +30 The -type f | xargs RM -f
crontab -e written plan tasks and save

30 01 * * * /root/mysql_autobackup.sh
represents 13:00 every day 30 branch execution mysql_autobackup.sh script in the root directory, that is, we write the above backup script with the delete operation, so that you can complete the system to automatically back up the database every day and It will automatically go to find more than 30 days of backup and delete

Two, crontab format

1, we just write an example of scheduled tasks

30 01 * * * /root/mysql_autobackup.sh
after this reduction format is the following

* * * * * *
First column of the "*" is from 1 to 59 minutes

The second column, "*" is from 0 to 23,0 hours Representative 12:00 AM

The third column of "*" for the day from 1 to 31

The fourth column of "*" for the month from 1 to 12

Fifth column "*" for the week from 0 to 6,0 for Sunday

The sixth column of the "*" command to run

Together is the following format

Command timeshare day Monday to run
2, give some examples

30 21 * * * reboot
above example indicates night 21:30 restart the server.
45 4 1,10,22 * * reboot
the example above indicates the monthly number of 1,10,22 4:45 reboot the server
10 1 * * 6,0 reboot
the above examples represent every Saturday, Sunday 1:10 reboot the server
0,30 18-23 * * * reboot
above example represents restart the server every 30 minutes between 18:00 and 23:00 each day.
0 23-7 / 1 * * * reboot
between 23:00 to 7 am, every hour restart the server
 
----------------
Disclaimer: This article is CSDN blogger "coffee so strong." original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/ywd1992/article/details/81219091

Guess you like

Origin www.cnblogs.com/zst062102/p/11683550.html