admin avatar

Implementation method of regular backup of MySQL database

🕗 by admin

  1. Create a shell script

vi dbbackup.sh

The content of the creation script is as follows:

Bash:
1
2
3
4
5
6
7
#!/bin/sh
 db_user="root"
 db_passwd="demopasswd"
 db_name="demodb"
 name="$(date +"%Y%m%d%H%M%S")"
 /usr/bin/mysqldump -u$db_user -p$db_passwd $db_name >>/data/backup/$name.sql

/usr/bin/mysqldump: //The path of the mysqldump backup tool in the mysql database installation directory

demodb: // The name of the database to be backed up

/data/backup/$name.sql: //The output location of the backup file can be set according to the situation

  1. Add execute permission to shell script

chmod +x dbbackup.sh

  1. Add a scheduled task to the script

crontab -e

Enter the name of the previous line to edit the scheduled task, and finally add the following content

00 01 * * * /bin/sh /usr/local/mysql/dbbackup.sh

At 1 am every day, an automatic backup script will be executed to perform regular backups of the MySQL database.

restart task process

service cron restart

Description of the crontab file:

In the crontab file created by the user, each line represents a scheduled task, and each field in each line represents a setting. Its format is divided into six fields per line. The first five paragraphs are time setting fields, and the sixth paragraph is the time setting field. segment is the command field to execute.

The format is as follows: minute hour day month week command

Parameter Description:

1
2
3
4
5
6
7
minute: Indicates the minute, which can be any integer from 0 to 59.
hour: Indicates the hour, which can be any integer from 0 to 23.
day: Indicates the date, which can be any integer from 1 to 31.
month: Indicates the month, which can be any integer from 1 to 12.
week: Indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
command: The command to be executed, which can be a Linux system command or a script program written by yourself.

💘 相关文章

写一条评论