Automatic mysql database backup in CentOS

Source: Internet
Author: User

Implementing automatic data backup for mysql Databases in CentOS is a very important data and often needs to be backed up. manual backup is troublesome every time. This script backs up the database and retains the backup files of the last 15 days in CentOS. Avoid occupying too much useless backup space.

#! /Bin/bash id = "root" # username pwd = "123123" # password dbs = "conedu commlib" # list of database names. Separate multiple databases with spaces. Backuppath = "/root/mysqlbackup" # Save the backup location day = 15 # Keep the backup for the last few days [! -D $ backpath] & mkdir-p $ backuppath # Check whether the backup directory exists. If the backup directory is not saved, create a new directory. 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/var/lib/mysql. sock $ db> $ backupname _ $ db. SQL # Back up a single data. 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 fi done tar-czf unzip backupname.tar.gz *. SQL # compress all SQL files if ["$? "=" 0 "] then echo $ (date + % Y-% m-% d)" tar sucess "> mysql. log else echo $ (date + % Y-% m-% d) "tar failed"> mysql. log exit 0 fi rm-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.

 

You only need to add a row to/etc/crontab, and then you can automatically back up the row at the specified time.

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.