MySQL Automatic Backup combat

MySQL data backup corporate real. Production shell script

Function 1: xtrabackup make a weekly backup of a backup cycle (+ binary log database backup to local / data / backup). Tip: On Monday some point in time to do a full backup, on Tuesday, three, four, five, six, day incremental backup storage directory "/ data / backup / 2018/52/1 / backup data" directory interpretation "/ data / the first weeks of backup / year / year / day of week / data "52 weeks a year 7 days a week. The reduction can be carried out tests on the new server.

Function 2: xtrabackup manual backup, full backup to the backup data / data / backup / base. Inc1 incremental backup may be performed according to the base directory, then the incremental backup inc2 according inc1 directory. And restore tests on the new server

 

shell script

#! / bin / bash 
# 
# ****************************************** ************************** 
#Author: qiangshu 
#Tel: 18,610,586,896 
#Date: 2019-11-14 
#filename: mysql_backup.sh 
#URL : HTTP: // xxxxxx 
#description: xtrabackup use backup and restore mysql database 
# test environment for CentOS7, yum install mariadb-server5.5, MySQL5.7.27 binary installation was successful                     
#Copyright (C): 2019 All Rights Reserved 
# ** ************************************************** **************** 

storage directory # xtrabackup backup data after the 
base_dir = / data / backup # MySQL data directory, recovery time with 
data_dir

 = / data / MySQL

= SQL_user 'the root' 
sql_host = 'localhost' 
sql_pass = '123456' 
sql_login = "- User SQL_user --host = = $ $ $ sql_pass sql_host --password =" 

# epel installation source, and then install percona-xtrabackup, yum install xtrabackup version of xtrabackup-2.3.6-percona 
# does not support backup mysql5.7, the need to percona official website to download xtrabackup-24-2.4-percona .12 or later version 

# judge script parameters
 Case $ 1  in 
--all | - A)
 # - perform a full backup of all manual
     [-d $ base_dir] || mkdir -p $ base_dir 
    xtrabackup $ sql_login --backup --target- the dir = $ base_dir / Base 
    ;;

 --increment | - I)
 # - incrementd manually perform incremental backups    
    LS = `$ base_dir end_dir | tail -1`     # If only the base, then incremental backups for the base directory data directory
     IF [" $ end_dir "==" base "]; the then 
        xtrabackup $ sql_login --backup - = $ base_dir the dir-target / INC1---incremental the basedir = $ base_dir / Base
     # if the data directory already contains inc directory, then the incremental backup catalog according to the maximum number inc
     elif [[ "$ end_dir" = ~ " inc is "]]; the then 
        NUM =` echo $ end_dir | grep -o [0-9] * ` 
        the let num_add = NUM +. 1 
        xtrabackup $ sql_login --backup --target-base_dir the dir = $ / inc is $ num_add --incremental = $ base_dir -basedir / INC. $ NUM 
    fi 
    ;; --manualrecovery | - m)
 # manually restore the data, to determine whether the directory database is empty, do not quit empty     [ " ` LS $ data_dir` " == ""




] {|| echo the MySQL Data the dir IS Not null !!!; Exit 2 ;} 
    inc_num = ` LS -d $ base_dir / inc is * 2 > / dev / null | WC -l`     # incremental data directly without reduction
     if [ "$ inc_num" -eq 0]; the then 
        xtrabackup --prepare --target-base_dir the dir = $ / Base
     # incremental data, the increment data according to the file number of the folder, circulation is restored
     elif [ "$ inc_num "-ge. 1]; the then 
        xtrabackup --prepare --apply-log-only-the dir = $ --target base_dir / Base 
        for I in SEQ. 1 $` inc_num`; do 
            IF [$ I $ inc_num -eq]; the then
 
                xtrabackup --prepare the dir = $---target base_dir / base --incremental-dir = $ base_dir / inc $ i
                BREAK 
            Fi 
            xtrabackup --prepare --apply-log-only---target base_dir the dir = $ / $ = Base --incremental the dir-base_dir / I inc is $ 
        DONE 
    Fi 
    xtrabackup --copy-Back---target the dir = $ base_dir / Base
     user # modify the data directory of all files named MySQL
     chown - R MySQL: MySQL $ data_dir 
    ;;

 --autobackup | b)
 # can be placed cron scheduled task, the task to perform automatic backup every day, Zhou Yisheng sake of the amount of data, Tuesday to Sunday generate incremental data 
# data directory format / data / backup / 2019/45 /1 represents the 2019 week 45 Monday,
     year = `DATE +% Y ' 
    week_number` DATE = +% U` 
    date_of_week `DATE = +% u`
    = $ base_dir full_dir / year $ / $ week_number 
    mkdir -p base_dir $ / year $ / $ week_number 
    IF [$ date_of_week -eq 1]; the then
        Week # 1 full backups
         xtrabackup $ sql_login --backup --target- dir = $ full_dir / $ date_of_week
     the else 
        # 2-7 weeks , against the previous day's data do incremental backups 
        the let Yesterday = $ date_of_week- 1 

        one day before # If the directory does not exist, there is no proof of the backup data
         [base_dir -d $ / year $ / $ week_number / $ Yesterday] {|| echo Yesterday backup data not exist !!!; Exit. 1;} 
        xtrabackup $ sql_login --backup --target = $ full_dir -dir / $ date_of_week --incremental-full_dir the basedir = $ / $ Yesterday 
    Fi 
    ;;
 
--autorecovery | - R & lt)
# Automatic backup and recovery methods, such as using the format:. / 45. 3 Backup --autorecovery 2019 returns to 2019 45 weeks 3 days of data 
    year = $ 2
    week_number = $ . 3 
    date_of_week = $ . 4 
    full_dir = base_dir $ / year $ / $ week_number     # date if the directory does not exist, an error exit
    

    [full_dir -d $ / $ date_of_week] {|| echo Wrong INPUT DATE !!!; Exit . 3 ;}
    # date if the directory is empty, exit error     [ " ` full_dir LS $ / $ date_of_week` " == " " ] && { echo the Dir Data NO !!!; exit . 4 ;}    # num represented several weeks to recover 
    num = $ date_of_week
     # If the number is 1, the only full backup 1 week, according to a full backup and recovery
     if [$ num -eq 1]; then
 

    
 
        --prepare the dir = --target-xtrabackup $ full_dir / 1 
    Fi 

    # If the number> = 2, there is a full backup of the periphery, and a plurality of backup, respectively, each incremental reduction 
    if [$ num -ge 2] ; the then 
        xtrabackup --prepare --apply-log-only---target full_dir the dir = $ /. 1 
        for I in SEQ `$ 2 num`; do 
            IF [$ I $ -eq NUM]; the then 
                xtrabackup --prepare - = $ full_dir the dir--target / --incremental. 1-full_dir the dir = $ / $ I 
                BREAK 
            Fi 
            xtrabackup --prepare --apply-log-only---target full_dir the dir = $ / $. 1 = the dir---incremental full_dir / $ I 
        DONE 
    Fi 
 
    xtrabackup --copy-Back---target $ full_dir the dir = /. 1
    chown - R & lt MySQL: MySQL data_dir $ 
    ;; 

--help | -H) 
    CAT << the EOF
This script by xtrabackup program to back up mysqld, you need to install percona-xtrabackup-24-2.4.12 or later 

parameter options: 
--all or -a: full backup, full backup default path is / the Data / Backup / Base 

- -increment or -i: incremental backup, incremental backup must be performed in the case of full backup, incremental backup for the first time / data / backup / inc1, the second time / data / backup / inc2, to such push 

--manualrecovery or -m: full recovery data backup and incremental backup, full backup advance the placed / data / backup / base directory, increment 1 / data / backup / inc1, so 

--autobackup or -b: in accordance with the first few weeks, a few weeks of backup data format. The storage path / data / backup / 2019/45 / {1,2,3}, determines if it is a full backup on Monday, Tuesday to Sunday, the day before incremental backup for backup. We can put the script into crontab task to perform once a day. For example: 0 30 * * * / bin / bash script name. 

--autorecoveryh or -r: --autobackup backup data reduction, reducing the time required to enter in the first few weeks of the week. For example: script.sh --autorecovery 2019 45 2, meaning that data back to the 2019 Week 45 Week 2 of 

--help or -h: View help 

EOF 
    ;; 
 
*)
  echo "use --help" 

esac

 

Guess you like

Origin www.cnblogs.com/ysuwangqiang/p/11864914.html