The script updates again, share!
#!/bin/sh
#
Usage
#./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 folder
fullbackup_dir= $BACKUP _dir/full # full-Library backup folder
incrbackup_dir= $BACKUP _dir/incre # Incremental backup of folders
memory=4096m # Number of memory limits to use when restoring
Errorlog= ' Grep-i "^log-error" $MY _cnf |cut-d =-F 2 '
Mysqld_safe=/usr/local/mysql/bin/mysqld_safe
mysql_port=3306
#############################################################################
#显示错误
#############################################################################
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 run:"; 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 Check
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 folder $backup_dir does not exist."
Fi
if [$#! = 1]; Then
Error "Usage: $ $ Use absolute path to restore folder"
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
#推断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, applying an incremental backup to a fully-prepared folder
For i in ' Find $PARENT _dir-mindepth 1-maxdepth 1-type d-printf "%p\n" | Sort-n ';
Do
##### #推断LSN
if ["${fetch_incre_lastlsn}" = "${fetch_full_lastlsn}"];then
echo "*****************************************"
echo "LSN does not need prepare!"
echo "*****************************************"
Echo
Break
Else
##### #推断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>&1
Check_innobackupex_fail
if [$INCR = $i]; Then
Break
Fi
Fi
##### #推断LSN
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 folder."
mysqlcnf= "/USR/LOCAL/MYSQL/MY.CNF"
Mysqldatadir= ' Grep-i "^basedir" $mysqlcnf |cut-d =-F 2 '
' Echo ' Chown-r mysql:mysql ' ${mysqldatadir} '
echo "3. Permissions changed successfully!"
echo "*****************************"
#自己主动启动mysql
Init_num=1
if [!-x $MYSQLD _safe]; Then
echo "MySQL installation when boot files are not installed to $mysqld_safe or no run permissions"
Exit 1 #0是运行成功, 1 is not running successfully
Else
echo "Start native Mysqlport: $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 startup .... Please wait a moment ... "
Sleep 5
if [$PORTNUM = 1];
Then
echo "MySQL Startup success * * * * * * * * *"
Exit 0
Fi
init_num=$ (($INIT _num + 1))
Done
Echo-e "MySQL startup failed or the boot time is too long, please check the error log ' echo ' Cat ' ${errorlog} '"
echo "*****************************************"
Exit 0
Fi
End_restore_time= ' Date +%f '%T '%w '
echo "Database restore completed: $END _restore_time"
Exit 0
Actual combat: Innobackupex for MySQL 5.6 own active restore script-v2