Actual combat: Innobackupex for MySQL 5.6 auto-Restore Script

Source: Internet
Author: User

#!/bin/sh
#
# How to use:
#./restore.sh/full path of your backup files
#[email protected]

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-Library backup directory
incrbackup_dir= $BACKUP _dir/incre # Incremental backup directory
memory=4096m # Number of memory limits to use when restoring

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

#显示错误

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

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

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

# Check for Innobackupex error output

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

Check_innobackupex_fail ()
{
If [-z] ' Tail-2 $TMP _log | grep ' completed ok! ' `" ] ; Then
echo "$INNOBACKUPEX command failed to execute:"; Echo
echo "----------$INNOBACKUPEX error Output----------"
Cat $TMP _log
#保留一份备份的详细日志
Logfiledate=restore. ' Date +%y%m%d%h%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, verify that the link is installed or verified correctly."
Fi

if [!-D $BACKUP _dir]; Then
Error "Backup directory $backup_dir does not exist."
Fi

if [$#! = 1]; Then
Error "How to: Use the absolute path to restore a directory"
Fi

if [!-D $]; Then
Error "revert to: $ not present."
Fi

# Some Info Output
echo "----------------------------"
Echo
echo "$0:mysql Restore Script"
Start_restore_time= ' Date +%f '%T '%w '
echo "Database restore started at: $START _restore_time"
Echo

Parent_dir= ' DirName $ '


if [$PARENT _dir = $FULLBACKUP _dir]; Then
Fullbackup=$1
echo "Restore ' basename $FULLBACKUP '"
Echo

Else
If [' dirname $PARENT _dir ' = $INCRBACKUP _dir]; Then
Incr= ' basename $ '
Full= ' basename $PARENT _dir '
fullbackup= $FULLBACKUP _dir/$FULL

if [!-D $FULLBACKUP]; Then
Error "All ready: $FULLBACKUP not present."
Fi

echo "restore $full to Delta $incr"
Echo

echo "Prepare full 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>&1
Check_innobackupex_fail

if [$INCR = $i]; Then
Break
Fi
Done
Else
Error "Unknown backup type"
Fi
Fi


echo "Prepare full 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. Database Restore in ... Just a moment, please.
echo "*****************************"

$INNOBACKUPEX _path--defaults-file= $MY _cnf--copy-back $FULLBACKUP > $TMP _log 2>&1
Check_innobackupex_fail


Rm-f $TMP _log
echo "2. Congratulations, restore success!."
echo "*****************************"


#修改目录权限
echo "Modify the permissions of the MySQL directory."
mysqlcnf= "/USR/LOCAL/MYSQL/MY.CNF"
Mysqldatadir= ' Grep-i "^basedir=" $mysqlcnf '
' Echo ' Chown-r mysql:mysql ' ${mysqldatadir:8} '
echo "3. Permissions modified successfully!"
echo "*****************************"


#自动启动mysql
Echo-n "Please enter confirm start mysql[1]:"
Read Var
Echo, "You're typing in $var."
Errorlog= ' Grep-i "^log-error" $MY _cnf '
if [$var = 1]; Then
echo "MySQL boot up ......"
'/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 failed to start, please check error log $errorlog"
echo "*****************************************"
Fi
Exit 0
Else
echo "Input not correct!"
Fi

End_restore_time= ' Date +%f '%T '%w '
echo "Database restore completed: $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.