Shell script-----Automatically back up MySQL database

Source: Internet
Author: User

The whole idea of scripting

1. Define the required variables

2. Use the For loop to back up the library that needs to be backed up and name the library-the date of the day. SQL naming and logging of related logs

3. Set up a directory for the date of the day of the backup, move to the backup SQL file to that directory

4. Compress package the directory to save space and remove the directory after package success

5. Backup directory only back up seven days of the database, the eighth day delete the first day of the backup, the directory will only exist for seven consecutive days of backup files

6. Find the. log log file in the backup directory for more than seven days of deletion

#!/bin/bashmysql_bin=/usr/local/tdoa/mysql/bin/mysqldumpuser=rootpasswd=myoa888mysql_back=/mysql-backdate=$ ( DATE +%F) for i in td_oa td_oa_archive  bus crscelldo          $mysql _bin -u$user -p$passwd  $i  >>  $mysql _back/$ I-$date .sql        if [ $? -eq 0 ];then                          echo -e  "$date   $i  back success\n"  >> $ mysql_back/back.log                 else                         echo -e  "$date   $i  back failure\n"  >>  $MYSQl_back/error.log        fidonecd  $mysql _back         if [ ! -d  $date  ];then                 mkdir  $mysql _back/$date                  mv  $mysql _back/*.sql   $mysql _back/$date         else                 mv  $mysql _back/*.sql  $mysql _back/$ date        fitar -czf mysql-$date .tar.gz  $daterm   -r  $mysql _back/$datefind   $mysql _back -name \*.gz -type f -mtime +6  -exec rm {} \;find  $mysql _back -name \*.log -type f -atime  +6 -exec rm&nbsP {} \;


This article is from the "Hanging Sword" blog, please be sure to keep this source http://sublime.blog.51cto.com/8856101/1535990

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.