1. Under the server, create the following 3 folders: Mkdir/bak,mkdir/bak/bakmysql,mkdir/bak/bakmysqlold. Build a shell script under the/bak/bakmysql folder: touch/bak/bakmysql/backup.sh. Authorization to File: chmod 755/bak/bakmysql/backup.sh.
2. Edit Shell script: vi/bak/bakmysql/backup.sh
#!/bin/shcd /bak/bakmysqlecho "You are in Bakmysql directory "mv bakmysql* /bak/bakmysqloldecho " old databases are Moved to bakmysqlold folder "now=$ (date +"%d-%m-%y ") file=bakmysql-$Now. sqlmysqldump - Uroot -p ' password ' db_bbs > $Fileecho "your database backup Successfully completed "sevendays=$ (date -d -7day +"%d-%m-%y ") if [ -f /bak/bakmysqlold/bakmysql-$SevenDays. sql ]thenrm -rf /bak/bakmysqlold/bakmysql-$ sevendays.sqlecho "you have delete 7days ago bak file " elseecho " 7days ago bak file not exist "Fi
3.shell script: mysqldump-uroot-p 'password' Db_bbs> $File The red font in this sentence, please change to your own MySQL database password and name. The script first moves the backup file under the/bak/bakmysql folder Bakmysql to the beginning of the file name to/bak/bakmysqlold, generates the most recent backup file, and then judges/bak/ Bakmysqlold folder seven days ago whether the file exists, the existence is deleted, and finally ended.
This is the blogger's server: 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/7A/3E/wKiom1al7aywsIeBAACYSJ9aC6U422.jpg " Title= "Qq20160125173941.jpg" alt= "Wkiom1al7aywsiebaacysj9ac6u422.jpg"/>
4. Set the shell script to execute automatically every day: Vi/etc/crontab add such a line at the end (refer to the note in/etc/crontab for your own reference): 0 0 * * * root/bak/bakmysql/backup.sh. Restart Crontab:/sbin/service crond restart. If you do not set the crontab to boot, you can set it to boot from: Chkconfig–level crond on
This article is from the "Zhanglunc" blog, make sure to keep this source http://zhanglunc.blog.51cto.com/11144997/1738402
Automatically backs up MySQL database every day under Linux (CentOS)