SoFunction
Updated on 2025-03-09

Share mysql timed backup shell script under CentOS

1. Backup statement
 

Copy the codeThe code is as follows:

# /usr/local/mysql/bin/mysqldump -utest -ptest test --socket=/tmp/mysql. > /usr/local/mysql_backup/3306/test-`date +%Y%m%d`. 

Statement description:
 
/usr/local/mysql/bin/mysqldump: under MySql installation directory
-utest: divided into two pieces, one is -u and the other is test; where -u means it is the user name, and test means it is the current user name
-ptest: divided into two pieces, one is -u and the other is test; where -u means it is a password, and test means it is the current password
--socket: Which port to back up? Check which one is in line with /tmp
test: Which database to back up
>: The folder behind is the backup, and named after the database name-current date
 
2. Write the statement into a script
 
3. Edit /etc/crontab

Copy the codeThe code is as follows:

# vim /etc/crontab

4. Add below
 

Copy the codeThe code is as follows:

00 3 * * * root /home/mysql/3306/backup 

Note: backup is the script I just wrote
It means that the backup is performed every day at 3 o'clock
 
5. Restart crontab
 

Copy the codeThe code is as follows:

# /etc///crond restart