Practice: INNOBACKUPEXformysql5.6 automatically restores the script

Source: Internet
Author: User

#! /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

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

# 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

# Prepare Incremental Backup set
For I in 'Find $ PARENT_DIR-mindepth 1-maxdepth 1-type d-printf "% P \ n" | sort-n'; do

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
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'
'Echo 'chown-R mysql: mysql' $ {mysqldatadir: 8 }'
Echo "3. Permission modification successful! "
Echo "*****************************"

# Automatically start mysql
Echo-n "Enter OK to start mysql [1]:"
Read var
Echo "you entered $ var"
Errorlog = 'grep-I "^ log-error" $ MY_CNF'
If [$ var = 1]; then
Echo "mysql is starting ............"
'/Usr/local/mysql/bin/mysqld_safe>/dev/null &'
Sleep 10
PortNum = 'netstat-lnt | grep 3306 | wc-l'
If [$ PortNum = 1];
Then
Echo "4. mysql started successfully"
Echo "*****************************"
Else
Echo "4. mysql startup failed. Check the error log $ errorlog"
Echo "************************************** ***"
Fi
Exit 0
Else
Echo "incorrect input! "
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.