XtraBackup数据库还原脚本
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
$ cat restore.sh #!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin export PATH backup_dir="/backup/mysql" restore_dir="$2" mysql_cnf="/etc/my.cnf" mysql_user="root" mysql_password="admin" mysql_sock="/usr/local/mysql/tmp/mysql.sock" backup_week_year=$(date +%U) backup_day_week=$(date +%u) backup_application="/usr/local/bin/innobackupex" return_value=0 restore_base_dir=`ls -l {restore_dir} | awk '{print $9}' | grep -v "^$" | head -1` restore_base_date=`ls -l ${restore_dir} | awk '{print $9}' | grep -v "^$" | awk -F '_' '{print $1}' | head -1` restore_sn_number=`ls -l ${restore_dir} | awk '{print $9}' | grep -v "^$" | awk -F '_' '{print $2}' | head -1` full_apply_log(){ if [ ${restore_sn_number} -eq 0 ]; then ${backup_application} --defaults-file={mysql_cnf} --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --apply-log --redo-only ${restore_dir}/${restore_base_dir} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Recovery Error" > /tmp/MySQL_Innodb_backupex_recovery.txt fi else echo "" echo -e "\033[31m = = = = 命令执行失败. = = = = \033[0m" echo "" exit 2 fi } full_copy_back(){ if [ {restore_sn_number} -eq 0 ]; then ${backup_application} --defaults-file={mysql_cnf} --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --copy-back ${restore_dir}/${restore_base_dir} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Recovery Error" > /tmp/MySQL_Innodb_backupex_recovery.txt fi else echo "" echo -e "\033[31m = = = = 命令执行失败. = = = = \033[0m" echo "" exit 2 fi } incremental_apply_logreccovery(){ if [ {restore_sn_number} -eq 0 ]; then ${backup_application} --defaults-file={mysql_cnf} --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --apply-log --redo-only ${restore_dir}/${restore_base_dir} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Recovery Error" > /tmp/MySQL_Innodb_backupex_recovery.txt fi count=`ls {restore_dir} | wc -l` for i in `seq $((${count} - 1))` do ${backup_application} --defaults-file=${mysql_cnf} --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --apply-log --redo-only ${restore_dir}/${restore_base_dir} --incremental-dir=${restore_dir}/$(date -d "-$i day ago ${restore_base_date}" +%F)_$i >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Recovery Error" > /tmp/MySQL_Innodb_backupex_recovery.txt fi done else echo "" echo -e "\033[31m = = = = 命令执行失败. = = = = \033[0m" echo "" exit 2 fi } incremental_copy_back_reccovery(){ if [ {restore_sn_number} -eq 0 ]; then ${backup_application} --defaults-file={mysql_cnf} --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --copy-back ${restore_dir}/${restore_base_dir} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Recovery Error" > /tmp/MySQL_Innodb_backupex_recovery.txt fi fi } case $1 in FullApplyLog) if [ $# != 2 ] ; then echo "USAGE: $0 FullApplyLog RestoreDir" echo " e.g.: $0 FullApplyLog /backup/mysql/18" exit 1; fi full_apply_log echo -e "\033[32m= = = = 应用备份日志成功,请关闭MySQL后执行FullCopyBack操作 = = = =\033[0m" echo -e "\033[32m= = = = 关闭MySQL后,请将数据库目录改名,请新建一个数据库目录 = = = =\033[0m" ;; FullCopyBack) if [ $# != 2 ] ; then echo "USAGE: $0 FullCopyBack RestoreDir" echo " e.g.: $0 FullCopyBack /backup/mysql/18" exit 1; fi full_copy_back echo -e "\033[32m= = = = 数据还原成功,请修改MySQL数据库目录权限 = = = =\033[0m" ;; IncrementalApplyLogReccovery) if [ $# != 2 ] ; then echo "USAGE: $0 IncrementalApplyLogReccovery RestoreDir" echo " e.g.: $0 IncrementalApplyLogReccovery /backup/mysql/18" exit 1; fi incremental_apply_logreccovery echo -e "\033[32m= = = = 应用备份日志成功,请关闭MySQL后执行IncrementalCopyBackReccovery操作 = = = =\033[0m" echo -e "\033[32m= = = = 关闭MySQL后,请将数据库目录改名,请新建一个数据库目录 = = = =\033[0m" ;; IncrementalCopyBackReccovery) if [ $# != 2 ] ; then echo "USAGE: $0 IncrementalCopyBackReccovery RestoreDir" echo " e.g.: $0 IncrementalCopyBackReccovery /backup/mysql/18" exit 1; fi incremental_copy_back_reccovery echo -e "\033[32m= = = = 数据还原成功,请修改MySQL数据库目录权限 = = = =\033[0m" ;; *) echo "Usage:$0(FullApplyLog|FullCopyBack|IncrementalApplyLogReccovery|IncrementalCopyBackReccovery)" ;; esac |
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!