MySQL database backup implementation example using XtraBackup

  Tools and methods on a backup MySQL database will be more, we simply introduce our company a platform backup solution. Xtrabackup yes, but only for database storage engine InnoDB database and XtraDB be backed up by a non-blocking percona free open-source database hot backup software, but for MyISAM (such as MySQL) backup will also lock the table during the backup process. More about Xtrabackup please introduce the degree of your mother.

1, the installation Xtrabackup

  Https://www.percona.com tar to the official website to download to install, as part of our server can be connected public network, the service provider and downloaded directly transmitted to the target host

  After installation will generate several tools, which can be used to innobackupex MySQL for backup.

2, Division I-platform database backup strategy

  MySQL internet data currently deployed on two servers A and B, two mutual backup data real-time synchronization, currently the primary library is A. Backup scripts and cron job has deployed on two services, scheduled tasks will be executed in two daily services, but the main scripts strategy only for backup with the library, the main criterion is the MySQL data floating in the host IP on.

  Full backup

# Full backup script
#! / Bin / bash
HOMEDIR = / opt / db_backup

# Judge save directory full backup, the log directory exists, if there is no new
[-d / opt / db_backup / full_bakcup] || mkdir -p / opt / db_backup / full_bakcup
[-d / opt / db_backup / logs] || mkdir -p / opt / dB_backup / logs

# Set backup log
logfile = `DATE" the Y +% m% D% "` _fullbackup.log
Touch $ HOMEDIR / logs / $ logfile

# Determine whether to activate the floating IP on the host
/ sbin / ip addr | grep 192.168.1.10
IF [$ -ne 0?]; The then
        echo "This IS not at The Master DB, NO need to Backup." >> $ HOMEDIR / logs / $ logfile
        Exit. 3
Fi

#检查mysql是否运行正常
/usr/sbin/lsof -i:3306 | grep LISTEN
if [ $? -ne 0 ];then
        echo "MySQL may not working correctly, can not find LISTEN for mysql." >> $HOMEDIR/logs/$logfile
        exit 5
fi

ps -ef | grep /home/mysql/bin/mysqld
if [ $? -ne 0 ];then
        echo "MySQL may not working correctly, can not find pid for mysql." >> $HOMEDIR/logs/$logfile
        exit 7
fi

#执行全量备份
/usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password="abc@123" /opt/DB_backup/full_bakcup/  &>> $HOMEDIR/logs/$logfile
[ $? -eq 0 ] && echo "Full backup successful." >> $HOMEDIR/logs/$logfile

# Incremental backup is transferred to the previous week / home directory temporarily stores
tmp_back_name = "` date -d "-7 days" + "% Y% m% d" `-`date -d" -1 days "+" the Y% m% D% "` "
the tar -zcvf /home/old_DB_backup/$tmp_back_name.tar.gz / opt / db_backup / incremental_backup / >> 201 * & $ HOMEDIR / logs / $ logfile && RM -rf / opt / db_backup / incremental_backup / * & >> $ HOMEDIR / logs / $ logfile && echo " $ tmp_back_name incremental backup transferred to the / home directory to save temporary success." >> $ HOMEDIR / logs / $ logfile

  Incremental Backup

  Also by MySQL incremental backup floating IP host it is not determined that the master database, and then determines whether or not a normal operation state MySQL. The determination by will again depend full backup directory is determined, if the host not been full backup, or the most recent full backup is 7 days ago, no incremental backup, a shift to full backup. Daily incremental backups are using the full amount of Monday's backup base, so although it will more than take up some disk space, but only when recovery needs to be performed twice a recovery action can be.

# Incremental backup script
#! / Bin / bash
# This script for the purpose of MySQL incremental backup once a day

HOMEDIR=/opt/DB_backup

# Judge incremental backups save directory, log directory exists, if there is no new
[-d / opt / db_backup / incremental_backup] || mkdir -p / opt / db_backup / incremental_backup
[-d / opt / db_backup / logs] | | mkdir -p / opt / dB_backup / logs

# Set backup log
logfile = `date" +% Y % m% d "` _incremental_backup.log

touch $HOMEDIR/logs/$logfile

# Determine the host is not the main mysql with the library, if the library is not the main exit to run the script.
/ Sbin / ip addr | grep 192.168.1.10
IF [$ -ne 0?]; The then
        echo "This IS not at The Master DB, Backup need to NO. ">> $ HOMEDIR / logs / $ logfile
        Exit 3
fi

#检查mysql是否运行正常
/usr/sbin/lsof -i:3306 | grep LISTEN
if [ $? -ne 0 ];then
        echo "MySQL may not working correctly, can not find LISTEN for mysql." >> $HOMEDIR/logs/$logfile
        exit 5
fi

ps -ef | grep /home/mysql/bin/mysqld
if [ $? -ne 0 ];then
        echo "MySQL may not working correctly, can not find pid for mysql." >>$HOMEDIR/logs/$logfile
        exit 7
fi


# Confirm the total amount of incremental backup is dependent current backup directory
the basedir -lrt LS = `/ opt / db_backup / full_bakcup | tail -1 | awk '{}. 9 Print $'`
# spirit full backup is determined if the host is not performed, executing full backup, incremental backup canceled
IF [ "$ basedir" == ""]; the then
    echo. "This has A full backup DB doesnt the before, the execute CAN not with the incremental backup" >> $ HOMEDIR / logs / $ logfile
    echo. "to the Execute the Begin Full Backup" >> $ HOMEDIR / logs / $ logfile
    SH /opt/DB_backup/shell/full_backup.sh
    IF [$ -eq 0?]; the then
        echo "at The First Full Backup Server IS ON the this successful , with the incremental Backup by Will the begin at The AT Tomorrow. ">> $ HOMEDIR / logs / $ logfile
        Exit
    fi
    Exit 77
fi

# Confirm full backup of the host that exists is earlier than 7 days, if the total amount is not within 7 days of backup is performed full backup.
Tl = `DATE +"% Y-% M-% D "`
T2 = `echo" $ the basedir "| -F_ awk '{}. 1 Print $'`
Day1% S = -d + `DATE" Tl $ "`
Day2% S = -d + `DATE" T2 $ "`
Day3 = $ ((($ Day1- Day2 $) / 86400))
IF [$ Day3 -gt 7]; the then
    echo. "Full Backup at The lastest IS 7 Days ago Member, the begin to the Execute Full Backup" >> $ HOMEDIR / logs / $ logfile
    SH / opt / db_backup / shell / full_backup.sh
    IF [$ -eq 0?]; the then
        echo "Full Backup Exec successful, PLS the Check Full Backup logs." >> $ HOMEDIR / logs / $ logfile
        Exit
    fi
    Exit 99
fi

# Full backup of the directory printed to the log
echo "The basedir is $ basedir" >> $ HOMEDIR / logs / $ logfile

#执行增量备份
/usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password="abc@123" --incremental --incremental-basedir=/opt/DB_backup/full_bakcup/$basedir /opt/DB_backup/incremental_backup >> $HOMEDIR/logs/$logfile

[ $? -eq 0 ] && echo "Incremental backup successful." >> $HOMEDIR/logs/$logfile

3, recovery plan

  Data recovery operation is relatively simple

  The full amount of recovery

# To prepare, by the action of --apply-log data file is to make the data files in a consistent state through the transaction and transaction synchronization has been submitted rollback uncommitted
innobackupex --apply-log / opt / DB_backup / full_bakcup / 2019 -05-09_10-45-32

# Restore operation needs to be emptied of all data source library and delete the original data files in the data directory
mysql> drop database xxx;

rm -rf ib_logfile0 ib_logfile1 ibdata1

# Restore command
innobackupex --defaults-file = / etc / mysql / my.cnf --copy-back / opt / DB_backup / full_bakcup / 2019-05-09_10-45-32

  Incremental recovery

# If only part of the data is a lossy data, after determining the scope of lossy data, can be restored directly to the incremental
innobackupex --apply-log / opt / DB_backup / full_bakcup / 2019-05-09_10-45-32 --incremental -dir = / home / dB_backup / incremental_backup / 2019-05-11_03-00-00

# If all the data is corrupted, perform the following operations
# 1, the total amount of the first reducing
innobackupex --defaults-file = / etc / mysql / my.cnf --copy-back / opt / DB_backup / full_bakcup / 2019-05-09_10-45 -32
# 2, the incremental reduction in
innobackupex --apply-log / opt / dB_backup / full_bakcup / 2019-05-09_10-45-32 --incremental-dir = / home / dB_backup / incremental_backup / 2019-05-11_03- 00-00

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159946.htm