Realization Method of Timing Backup of MySQL Database
- 2021-11-24 03:12:34
- OfStack
1. Create an shell script
vim backupdb.sh
Create the script as follows:
#!/bin/sh
db_user="root"
db_passwd="123456"
db_name="userdb"
name="$(date +"%Y%m%d%H%M%S")"
/usr/bin/mysqldump -u$db_user -p$db_passwd $db_name >>/home/backup/$name.sql
Description:
/usr/bin/mysqldump : mysql Under the database installation directory mysqldump Backup tool path
dbname Name of database to be backed up
/home/backup/$name.sql Backup file output location can be set according to the situation
2. Add execute permissions to the shell script
chmod +x backupdb.sh
3. Add timed tasks to scripts
crontab -e
Input on 1 Row naming for editing timing task , Finally, add the following
00 01 * * * /bin/sh /usr/local/mysql/backupdb.sh
The above timed task means every morning 1 Point will perform automatic backup feet , Go on MySQL Timed backup of database .
Description of the crontab file:
In the crontab file created by users, each line represents a timing task, and each field in each line represents a setting. Its format is divided into 6 fields in each line, the first 5 sections are time setting fields, and the sixth section is the command field to be executed.
The format is as follows: minute hour day month week command
Parameter description:
minute: Represents minutes and can be any integer from 0 to 59.
hour: Represents an hour and can be any integer from 0 to 23.
day: Represents a date and can be any integer from 1 to 31.
month: Represents the month and can be any integer from 1 to 12.
week: Represents the day of the week and can be any integer from 0 to 7, where 0 or 7 represents Sunday.
command: To execute the command, can be Linux system commands, can also be written by their own script files.
Summarize