Go MySQL automatically backs up and saves nearly 15 days of recording scripts

Source: Internet
Author: User

The main reality of this script is to implement backup of MySQL database and keep backup files for the last 15 days in CentOS. Avoid too many useless, stale backup footprints.

#!/bin/bashId="Root" #User namePwd="123123" #Passworddbs="conedu Commlib" #a list of database names, with multiple databases separated by spaces. Backuppath="/root/mysqlbackup" #where to save the backupDay=15#keep backups for the last few days[!-D$backpath] &&mkdir-p$backuppath#determine if the backup directory exists, and create a new directory when it is not saved. Cd$backuppath #go to the backup directory, this sentence can be omitted. You can direct the path to the command line as well. backupname=mysql_$ (Date +%y-%m-%d)#generates a prefix for the name of the backup file without a suffix.  forDbinch $dbs;#DBS is a collection of data names. Traverse all the data.  Domysqldump-U$id-P$pwd-s/var/lib/mysql/mysql.sock$db>$backupname _$db. sql#backs up individual data to. sql files. Put it in the current positionif["$?"=="0"]#$? Gets the return value of the execution of the last shell command. 0 indicates successful execution. The other represents an error. and writes the results to the log. Then Echo$ (Date +%y-%m-%d)"$db mysqldump sucess">>Mysql.logElseEcho$ (Date +%y-%m-%d)"$db MySQL dump failed">>Mysql.log Exit0Fidonetar-czf$backupname. tar.gz *.sql#compress all SQL filesif["$?"=="0"]then Echo$ (Date +%y-%m-%d)"Tar sucess">>Mysql.logElseEcho$ (Date +%y-%m-%d)"Tar failed">>Mysql.log Exit0firm- F*.sql#Delete all the SQL filesdelname=mysql_$ (date-d"$day day ago"+%y-%m-%d). tar.gz#get the name of the backup that is too old to be deleted. Rm- F $delname#Delete the file. 

As long as you add a row to the/etc/crontab, you can automatically back up the

Go MySQL automatically backs up and saves nearly 15 days of recording scripts

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.