1. Check the disk space and select the backup path
df -h
The home space is very sufficient.
2. Create a new backup directory
cd /home
mkdir backup
cd backup
3. Create a backup shell script.
The created file XXX can be named the name of the database.
vi backupXXX.sh
Edit the following content in the sh file (full export): Export the backup file as a SQL file
mysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql
If exported as a compressed file, edit it to the following content:
mysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz
4. Disk space is limited after all. You can delete previously backed up files and keep the most recent backup.
#删除7天之前的备份
find /home/backup -name "eco*.sql.gz" -type f -mtime +7 -exec rm -rf {
} \; > /dev/null 2>&l
#删除5分钟之前的备份
find /home/backup -name "eco*.sql.gz" -type f -mmin +5 -exec rm -rf {
} \; > /dev/null 2>&l
5. Save and exit
先Esc键,:wq保存退出编辑
6. Add executable permissions
chmod u+x backupXXX.sh
7. Execute the script
./backupXXX.sh
8. A warning appears: Many materials say that the backup will fail when this warning appears, but there is no failure. The file backup is successful.
9. Add a scheduled task
and execute the command
crontab -e
If there is no error prompt, it means that crontab has been installed. If an error is reported, install it by yourself.
Edit scheduled tasks
*/1 * * * * /home/backup/backupXXX.sh
Execute the shell script every minute and back up the files
10. You can go to the backup directory to check whether there are new backup files generated.
You can also check the task log.
tail -f /var/log/cron
Final configuration:
mysqldump -uroot -pong!8 ec | gzip > /home/backup/ec_$(date +%Y%m%d_%H%M%S).sql.gz
find /home/backup -name "ec*.sql.gz" -type f -mtime +30 -exec rm -rf {
} \; > /dev/null 2>&1