Use shell to automatically back up mysql Databases and shellmysql databases at regular intervals

Source: Internet
Author: User

Use shell to automatically back up mysql Databases and shellmysql databases at regular intervals

#! /Bin/bashid = "root" # username pwd = "123456" # password dbs = "blog.ewsd.cn dangjian.ewsd.cn idc.ewsd.cn shangfa.ewsd.cn" # list of database names, separate multiple databases with spaces. backuppath = "/home/mysqlbackup" # Save the backup location day = 30 # Keep the backup for the last few days [! -D $ backuppath] & mkdir-p $ backuppath # Check whether the backup directory exists. If no backup directory exists, create a new one. Cd $ backuppath # Go To The Backup Directory, which can be omitted. You can directly export the path to the command. Backupname = mysql _ $ (date + % Y-% m-% d) # prefix of the name of the backup file generated without the suffix. For db in $ dbs; # dbs is a set of data names. Traverse all data. Do mysqldump-u $ id-p $ pwd-S/tmp/mysql. sock $ db> $ backupname _ $ db. SQL # Back up a single data file as a. SQL file. Put it in the current location if ["$? "=" 0 "] # $? Returns the execution result of the previous shell command. 0 indicates that the execution is successful. Others indicate errors. And write the results to the log. Then echo $ (date + % Y-% m-% d) "$ db mysqldump sucess"> mysql. log else echo $ (date + % Y-% m-% d) "$ db mysql dump failed"> mysql. log exit 0 fidonetar-czf unzip backupname.tar.gz *. SQL # compress all SQL files if ["$? "=" 0 "] then echo $ (date + % Y-% m-% d)" tar sucess "> mysql. logelse echo $ (date + % Y-% m-% d) "tar failed"> mysql. log exit 0 firm-f *. SQL # Delete All SQL files delname = mysql _ $ (date-d "$ day ago" +%y-%m-%d=.tar.gz # obtain the name of the old backup to be deleted. Rm-f $ delname # delete an object.

Add the preceding script to the crontab of the scheduled task,Note the script execution permission settings.

Related Article

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.