1. connect to the server through the shell
Ssh-l root-p Port IP
After entering the password, enter the server
2, Create a folder where database backup data resides
mkdir/mysql/mysqldata_backup
3. Create and edit files
Vi/usr/sbin/bakmysql
The contents of the file are as follows:
fn = ' Date +%y%m%d '
Tar zcvf/mysql/mysqldata_backup/mysql$fn.tar.gz/mysql/data
Or
Mysqldump-uroot-ppassword/mysql/data/yourdatabase >/mysql/mysqldata_backup/mysql$fn.sql
Find $fn-name "mysql*.tar.gz"-type f-mtime +7-exec fm{}; >/dev/null 2>&1
Or
Find $fn-name "Mysql*.sql"-type f-mtime +7-exec fm{}; >/dev/null 2>&1
description:
/mysql/mysqldata_backup /Save the path for the backup data, msql$fn.tar.gz the name of the backup data according to the date number,/mysql/data the data path for the server database, yourdatabase the name of the database you want to back up ;
Note:
The First command is not a single quotation mark, but a symbol above the TAB key, and a space is required before and after date;
The second command has two methods, the first direct backup and compression of the database data source files, the second is to use the MySQL command mysqldump export database yourdatabase SQL file;
The third sentence is to delete the backup file 7 days ago, the file name corresponds to the second sentence.
4, Modify file Bakmysql property
chmod +x/usr/sbin/bakmysql
5, modify/etc/crontab
vi/etc/crontab
01 3 * * * root/usr/sbin/bakmysql
description:
01 3 is a daily 3:01 execution of the bakmysql file;
6, There are times when rebooting is not required, if the server has Crond service under the/etc/rc.d/init.d/path, you can choose to restart Crond
/etc/rc.d/init.d/crond restart
Regular backup and deletion of MySQL database under Linux