Use Crontab to automatically back up databases under Linux
Here are the steps to perform a simple database backup using the Add and mysqldump of the crontab Timer task, as follows:
1. Write a script:/serverback/autobackmysql.sh
The contents are as follows:
Method One:
# #使用mysqldump备份数据库erms
/usr/local/mysql/bin/mysqldump-uroot-ppwd erms >>/serverback/mysql_back/erms_$ (date+ "%Y_%m_%d"). sql
# #找到/serverback/mysql_back/The file name starts with Erms_, ends with. sql, and the file was modified by the system 7 days ago, delete it
find/serverback/mysql_back/-mtime +7-name "Erms_*.sql"-exec rm-rf {} \;
Method Two:
/usr/local/mysql/bin/mysqldump-uroot-ppwd dbname > dir/db_ ' Date +%f '. sql
# #保留近一周的备份文件, delete earlier
Find/dir-mtime +7-name "Db_*.sql"-exec rm-rf {} \;
Method Three:
Filename= ' Date +%y%m%d '
/usr/local/mysql/bin/mysqldump-uroot-proot erms >>/serverback/mysql/$filename. sql
Note: A. The mysqldump is best used in absolute path, if the direct use of mysqldump may be backed up into empty files
B. To ensure that the contents of the script are accurate, you can run each command individually, such as the Execute mysqldump command:/usr/local/mysql/bin/mysqldump-uroot-ppwd erms >>/serverback/ mysql_back/erms_$ (date+ "%y_%m_%d"). sql
C.find in the command, at the end; Semicolons cannot be omitted
2. Add a Scheduled task
Crontab–e # #编辑定时任务
Add timed Task content:
* * * * */serverback/autobackmysql.sh # #每天定时15:00:00 Execute script/serverback/autobackmysql.sh
Command:
Crontab–e # #编辑定时任务
Crontab–r Delete all Scheduled Tasks
Crontab–l List all Scheduled tasks
Use Crontab to automatically back up databases under Linux