A simple MySQL backup Shell script _ MySQL

Source: Internet
Author: User
This article mainly introduces a simple MySQL backup Shell script. This article provides code examples directly. For more information, see
#! /Bin/bash # this is a script of mysql backup if [! -D/mydata/data1/backup]; then mkdir/mydata/data1/backupficd/mydata/data1/backupfile = $ (find. -type f-mtime-7 | grep. * all. SQL) # Find the backup file echo $ fileif [-z $ file]; then echo "backup all databases... "backupfile = $ (date + % F-% H-% M-% S) mysqldump-uroot -- lock-all-tables -- flush-logs -- master-data = 2 -- all-databases>/mydata/data1/backup/"$ backupfile-all. SQL" if [$? -Eq 0]; then echo "Accomplish, file is $ backupfile-all. SQL! "Else echo" Failure !!! "Fielse # Find the penultimate binary file for incremental backup cd. echo" All database backups, now start doing incremental backups! "A = 1 B = 1 for file in $ (ls | grep mysql-bin | grep-v index); do num = $ (echo $ file | cut-d. -f2) if [$ num-gt $ a]; then a = $ num fi done unset num file num = 1 for file in $ (ls | grep mysql-bin | grep-v index ); do num = $ (echo $ file | cut-d. -f2) if [$ num-gt $ B-a $ num-ne $ a]; then B = $ num tmp = $ file fi done file1 = $ (echo $ tmp | cut-d. -f1) file2 = $ (echo $ tmp | cut-d. -f2) mysqlbinlog $ tmp>/mydata/data1/ba Ckup/"$ file1-$ file2. SQL" if [$? -Eq 0]; then echo "Backup completed, file is" $ file1-$ file2. SQL "" fifi

Note: This script performs a full backup every seven days, and then performs an incremental backup every day. full backup uses the mysqldump client tool, and incremental backup uses mysqlbinlog to back up its binary log files.

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.