Automatically back up shell files of the MySQL database

Source: Internet
Author: User
Tags mysql backup

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.