Regularly back up MySQL database tasks every day, delete the data before the specified days, and retain the data of the specified days;
Requirements:
1. Back up MySQL data at 4:00 every day;
2. In order to save space, delete all backup data for more than 3 months;
3. Delete the backup data for more than 7 days, and keep the backup data for the 10th, 20th and 30th in 3 months;
#Create SHELL file
vim backup_mysql.sh
mysqldump -uroot -p123456 --all-databases > /data/dbdata/mysqlbak/`date +%Y%m%d`.sql
find /data/dbdata/mysqlbak/ -mtime +7 -name '*[1-9].sql' -exec rm -rf {} \;
find /data/dbdata/mysqlbak/ -mtime +92 -name '*.sql' -exec rm -rf {} \;
#Create scheduled task
crontab –e
0 4 * * * /data/dbdata/backup_mysql.sh
The above is the whole content of this article. I hope it will be helpful for you to master shell scripts.