1. Edit the shell file used for backup.
#! /Bin/bash <br/> # MySQL backup scripts. </P> <p> ################################# ####################################### <br/> #1 backup database <br/> ############################# ######################################## ### <br/> username = root <br/> # Password = <br/> backup_dir1 =/var/lib/MySQL/dbbackup/<br/> backup_dir2 =/var /lib/MySQL/db_mysql_backup/<br/> backup_db1 = mydbname <br/> backup_file1 = $ backup_dir1/backup _ $ backup_db1 'date' + % Y % m % d _ % H % m % s ''. SQL <br/> backup_file2 = $ backup_dir2/backup _ $ backup_db1 'date' + % Y % m % d _ % H % m % s ''. SQL <br/> # Start backup. <br/> # mysqldump-U $ username-p $ password-B $ backup_db1 -- add-drop-table-r> $ backup_file1 <br/> # mysqldump-U $ username- p $ password-B $ backup_db1 -- add-drop-table-r> $ backup_file2 <br/> mysqldump-U $ username-B $ backup_db1 -- add-drop-table-r> $ backup_file1 <br/> mysqldump-U $ username-B $ backup_db1 -- add-drop-table-r> $ backup_file2 <br/> echo 'date' + % Y % m % d % H: % m: % s backupfile ''$ backup_file1 <br/> echo 'date' + % Y % m % d % H: % m: % s backupfile ''$ backup_file2 <br/> # compress backup file. <br/> gzip $ backup_file1 <br/> gzip $ backup_file2 <br/> echo 'date' + % Y % m % d % H: % m: % s GZIP file ''$ backup_file1 <br/> echo 'date' + % Y % m % d % H: % m: % s GZIP file ''$ backup_file2 <br/> ############################ ######################################## #### <br/> #2 delete overdue MySQL backup file. <br/> #################################### ####################################< br/> # Get the date part. <br/> arr1 = 'ls $ backup_dir1 | cut-d' _ '-f2' <br/> # Get 30 days ago. <br/> cur_date = 'date + '% Y % m % d'-30 day'' <br/> # Delete the overdue file. <br/> for cur_filename in $ arr1 <br/> DO <br/> result = 'echo $ cur_filename | tr-d "[A-Za-Z]" '<br /> If [$ result = ". "] <br/> then <br/> continue <br/> fi <br/> If [" $ result "-Le" $ cur_date "] <br/> then <br/> RM-RF 'echo "$ {backup_dir1} backup _ $ {cur_filename} _ *. SQL .gz" '<br/> echo 'date' + % Y % m % d % h: % m: % s Rm-RF ''$ {backup_dir1} backup _ $ {cur_filename} _ *. SQL .gz </P> <p> RM-RF 'echo" $ {backup_dir2} backup _ $ {cur_filename} _ *. SQL .gz "'<br/> echo 'date' + % Y % m % d % H: % m: % s Rm-RF ''$ {backup_dir2} backup _ $ {cur_filename} _ *. SQL .gz <br/> fi <br/> done <br/>
2. Edit the Scheduled File and perform the backup at 03:10 every day.
# Vi/etc/crontab
Add
# Backup MySQL database
10 03 *** Su-root-c "sh/var/lib/MySQL/dbbackup/backupdb. Sh>/var/lib/MySQL/dbbackup/backupdblog. log"
3. Restart crond.
#/Etc/rc. d/init. d/crond restart