Shell script for backing up MySQL to FTP with automatic cleanup function

Source: Internet
Author: User
In the past, the company used a Shell script to automatically back up MySQL to FTP on the Internet, which has always been used well. But the problem is that as the number of backup files increases, it is inconvenient to regularly clean up the old backup files on FTP. In fact, MySQL backup does not need to be retained for too long. It would be nice to have the function of automatically clearing old backup files. However, search online

In the past, the company used a Shell script to automatically back up MySQL to FTP on the Internet, which has always been used well. But the problem is that as the number of backup files increases, it is inconvenient to regularly clean up the old backup files on FTP. In fact, MySQL backup does not need to be retained for too long. It would be nice to have the function of automatically clearing old backup files. However, search online

In the past, the company used a Shell script to automatically back up MySQL to FTP on the Internet, which has always been used well. But the problem is that as the number of backup files increases, it is inconvenient to regularly clean up the old backup files on FTP.

In fact, MySQL backup does not need to be retained for too long. It would be nice to have the function of automatically clearing old backup files. However, I found this script on the Internet, but it may not meet my needs. Therefore, you can only manually change the previous Shell script.

First, we need to determine the backup retention time. I think one month of backup retention is enough. Because the backup will not be restored after one month. (If you have manual backup, automatic backup cannot replace manual backup ). So our idea is coming out: When uploading a backup file named on the current day, we will delete the backup of the same date before the next month, you can ensure that the backup of the last month is retained on the server.

Then we need to know a command in Linux:Date. This is a command to view the time and date information of the current server. Of course, here we need to know the parameters of this command. After all, we need to get the date part.

Enter the following command to view the "Year-month-day" input, similar ":

Date + % Y-% m-% d

So what if we want to get the date one month ago? Use the following command to get the result:

Date-d last-month + % Y-% m-% d

How is it? Simple. In actual backup, because the previous script uses the m-d-Y format, that is, the month-day-year format, so I made some adjustments. However, the following Shell script generates a file name based on "year-month-day.

Therefore, the entire backup Shell script is provided. If you need a friend, you only need to enter the Chinese description to use it on your Linux server. Usage: copy the file to a directory, grant the execution permission, and set the daily running time in cron. (For details about this part, refer to relevant instructions here)

The Shell script for backing up MySQL to FTP with the automatic cleanup function is as follows:

#! /Bin/bashMyUSER = "db_username" # database username, we recommend that you use the root account or the username MyPASS = "db_password" # Database Password MyHOST = "localhost" # Database Server # Linux bin paths, change this if it can't be autodetected via which commandMYSQL = "$ (which mysql)" MYSQLDUMP = "$ (which mysqldump)" CHOWN = "$ (which chown) "CHMOD =" $ (which chmod) "GZIP =" $ (which gzip) "# database backup path DEST ="/home/backup "# Main directory where backup will be storedMBD =" $ DEST/mysql "# G Et hostnameHOST = "$ (hostname)" # Get data in dd-mm-yyyy formatNOW = "$ (date + % d-% m-% Y) "OLD =" $ (date-d last-month + % d-% m-% Y) "# File to store current backup fileFILE =" "# Store list of databasesDBS =" "# do not backup these databasesIGGY =" test "rm-rf $ MBD [! -D $ MBD] & mkdir-p $ MBD |: # Only root can access it! $ CHOWN 0.0-R $ DEST $ CHMOD 0600 $ DEST # Get all database list firstDBS = "$ ($ MYSQL-u $ MyUSER-h $ MyHOST-p $ MyPASS-Bse 'Show databases ') "cat> ftpcc. sh <
 
  
> Ftpcc. sh # do all inone job in pipe, # connect to mysql using mysqldump for select mysql database # and pipe it out to gz file in backup dir :) $ MYSQLDUMP-u $ MyUSER-h $ MyHOST-p $ MyPASS $ db | $ GZIP-9> $ FILE fidonecat> ftpcc. sh <
  
   

After such a Shell script is run, you can obtain an independent backup file for each MySQL database and compress it in gz format to save space.

Original article address: the Shell script for backing up MySQL to FTP with the automatic cleaning function. Thank you for sharing it with the original author.

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.