Practice: INNOBACKUPEX for mysql 5.6 Automatic Restoration script-v2

Source: Internet
Author: User

The script is updated again and shared!

 

 

#! /Bin/sh
#
# Usage:
#./Restore. sh/full path of your backup file
# Ocpyang@126.com

INNOBACKUPEX = innobackupex
INNOBACKUPEX_PATH =/usr/bin/$ INNOBACKUPEX
TMP_LOG = "/var/log/restore. $. log"
MY_CNF =/usr/local/mysql/my. cnf
BACKUP_DIR =/backup # Your backup home directory
FULLBACKUP_DIR = $ BACKUP_DIR/full # full Database Backup Directory
INCRBACKUP_DIR = $ BACKUP_DIR/incre# directory of Incremental Backup
MEMORY = 4096 M # MEMORY limit used for restoration
ERRORLOG = 'grep-I "^ log-error" $ MY_CNF | cut-d =-f 2'
MYSQLD_SAFE =/usr/local/mysql/bin/mysqld_safe
MYSQL_PORT = 3306


######################################## #####################################

# Display Error

######################################## #####################################

Error ()
{
Echo "$1" 1> & 2
Exit 1
}

######################################## #####################################

# Check innobackupex error output

######################################## #####################################

Check_innobackupex_fail ()
{
If [-z "'Tail-2 $ TMP_LOG | grep' completed OK! ''"]; Then
Echo "$ INNOBACKUPEX command execution failed:"; echo
Echo "---------- $ error output of INNOBACKUPEX ----------"
Cat $ TMP_LOG
# Keep a Detailed backup log
Logfiledate = restore. 'date when policyymm1_d1_hsf-m'.txt
Cat $ TMP_LOG>/backup/$ logfiledate
Rm-f $ TMP_LOG
Exit 1
Fi
}

 

 

# Option Detection
If [! -X $ INNOBACKUPEX_PATH]; then
Error "$ INNOBACKUPEX_PATH does not exist in the specified path. Check whether the link is installed or correct ."
Fi

If [! -D $ BACKUP_DIR]; then
Error "the backup directory $ BACKUP_DIR does not exist ."
Fi

If [$ #! = 1]; then
Error "Usage: $0 indicates the absolute path of the restored directory"
Fi

If [! -D $1]; then
Error "restored to: $1 does not exist ."
Fi

# Some info output
Echo "----------------------------"
Echo
Echo "$0: MySQL restore script"
START_RESTORE_TIME = 'date + % f'' % t'' % W'
Echo "database restoration started at: $ START_RESTORE_TIME"
Echo

PARENT_DIR = 'dirname $1'


If [$ PARENT_DIR = $ FULLBACKUP_DIR]; then
FULLBACKUP = $1
Echo "Restore 'basename $ FULLBACKUP '"
Echo

Else
If ['dirname $ PARENT_DIR '= $ INCRBACKUP_DIR]; then
INCR = 'basename $1'
FULL = 'basename $ PARENT_DIR'
FULLBACKUP = $ FULLBACKUP_DIR/$ FULL
If [! -D $ FULLBACKUP]; then
Error "Full backup: $ FULLBACKUP does not exist ."
Fi
Echo "Restore $ FULL to incremental $ INCR"
Echo
Echo "Prepare complete backup set ..........."
Echo "*****************************"
$ INNOBACKUPEX_PATH -- defaults-file = $ MY_CNF -- apply-log -- redo-only -- use-memory = $ MEMORY $ FULLBACKUP> $ TMP_LOG 2> & 1
Check_innobackupex_fail

# Determining lsn
Check_incre_file = 'Find $ PARENT_DIR-mindepth 1-maxdepth 1-type d-printf "% P \ n" | sort-nr | grep-V' ^ $ '| head-n 1'

Check_incre_lastlsn =$ {PARENT_DIR}/$ {check_incre_file}/xtrabackup_checkpoints

Fetch_incre_lastlsn = 'grep-I "^ last_lsn" $ {check_incre_lastlsn} | cut-d =-f 2'

Check_full_file = 'Find $ FULLBACKUP/-mindepth 1-maxdepth 1-type d-printf "% P \ n" | sort-nr | grep-V' ^ $ '| head-n 1'

Check_full_lastlsn = $ FULLBACKUP/$ {check_full_file}/xtrabackup_checkpoints

Fetch_full_lastlsn = 'grep-I "^ last_lsn" $ {check_incre_lastlsn} | cut-d =-f 2'

 


# Prepare Incremental Backup set, which applies Incremental backup to the full backup directory
For I in 'Find $ PARENT_DIR-mindepth 1-maxdepth 1-type d-printf "% P \ n" | sort-n ';
Do
###### Determine the LSN
If ["$ {fetch_incre_lastlsn}" = "$ {fetch_full_lastlsn}"]; then
Echo "************************************** ***"
Echo "LSN does not require prepare! "
Echo "************************************** ***"
Echo
Break
Else

###### Determine the LSN
Echo "Prepare Incremental Backup set $ I ........"
Echo "*****************************"
$ INNOBACKUPEX_PATH -- defaults-file = $ MY_CNF -- apply-log -- redo-only -- use-memory = $ MEMORY $ FULLBACKUP -- incremental-dir = $ PARENT_DIR/$ I> $ TMP_LOG 2> & amp; 1
Check_innobackupex_fail

If [$ INCR = $ I]; then
Break
Fi

Fi
###### Determine the LSN
Done
 
Else
Error "unknown Backup Type"
Fi
Fi


Echo "prepare full backup set, roll back those uncommitted transactions ..........."
$ INNOBACKUPEX_PATH -- defaults-file = $ MY_CNF -- apply-log -- use-memory = $ MEMORY $ FULLBACKUP> $ TMP_LOG 2> & 1
Check_innobackupex_fail
Echo "*****************************"
Echo "1. Restoring the database... please wait"
Echo "*****************************"

$ INNOBACKUPEX_PATH -- defaults-file = $ MY_CNF -- copy-back $ FULLBACKUP> $ TMP_LOG 2> & 1
Check_innobackupex_fail


Rm-f $ TMP_LOG
Echo "2. Congratulations, restored successfully !. "
Echo "*****************************"


# Modify directory permissions
Echo "Modify mysql directory permissions ."
Mysqlcnf = "/usr/local/mysql/my. cnf"
Mysqldatadir = 'grep-I "^ basedir" $ mysqlcnf | cut-d =-f 2'
'Echo 'chown-R mysql: mysql' $ {mysqldatadir }'
Echo "3. Permission modification successful! "
Echo "*****************************"


# Automatically start mysql

INIT_NUM = 1
If [! -X $ MYSQLD_SAFE]; then
Echo "the mysql Startup file is not installed in $ MYSQLD_SAFE or has no execution permission"
Exit 1 #0 indicates that the execution is successful, and 1 indicates that the execution is unsuccessful.
Else
Echo "Start the mysql port of the Local Machine: $ MYSQL_PORT service"
$ MYSQLD_SAFE -- defaults-file = $ MY_CNF>/dev/null &
While [$ INIT_NUM-le 6]
Do
PORTNUM = 'netstat-lnt | grep $ {MYSQL_PORT} | wc-l'
Echo "mysql is starting... please wait ..."
Sleep 5
If [$ PORTNUM = 1];
Then
Echo "mysql ***** started successfully ****"
Exit 0
Fi
INIT_NUM = $ ($ INIT_NUM + 1 ))
Done
Echo-e "mysql failed to start or started for too long. Check the error log 'echo 'cat' $ {ERRORLOG }'"
Echo "************************************** ***"
Exit 0
Fi

 


END_RESTORE_TIME = 'date + % f'' % t'' % W'
Echo "database restoration completed at: $ END_RESTORE_TIME"
Exit 0


 

 

 

Related Article

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.