xtrabackup mysql 5.6_percona for mysql5.6 使用xtrabackup 备份一周全备每天基于全备进行增量备份脚本...

1 [root@mysql-backup-192.168.4.2 scripts]$ cat xtrabackup.sh

2 #!/bin/bash3 # MySQL backup script4 # https://github.com/gregorystorme/autoxtrabackup

5 # Copyright (c) 2014Gregory Storme6 # Version: 0.2

7

8 if [ -f /etc/default/autoxtrabackup ] ; then

9 . /etc/default/autoxtrabackup10 else

11 backupDir=$1

12 hoursBeforeFull=24

13 mysqlUser=root14 mysqlPwd=1qaz2wsx15 myhost=127.0.0.1

16 compression=true

17 compressThreads=4

18 keepDays=7

19 sendEmail=onerror20 emailAddress=fuyuntao@baidu-mgame.com21 hostip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`22 remotepass=2018_mgame23 remoteip=192.168.1.112

24 tmpfile=/tmp/compeletd25 fi

26

27 #####28 # No editing should be required below this line29 #####30

31 usage () {32 echo -e "\tRestore a full backup";33 echo -e "\t\tRestore a compressed backup:";34 echo -e "\t\t\tinnobackupex --decompress $backupDir/BACKUP-DIR";35 echo -e "\t\t\tFollow same steps as for non-compressed backups";36 echo -e "\t\tRestore a non-compressed backup:";37 echo -e "\t\t\tinnobackupex --apply-log $backupDir/BACKUP-DIR";38 echo -e "\t\t\tStop your MySQL server";39 echo -e "\t\t\tDelete everything in the MySQL data directory (usually /var/lib/mysql)";40 echo -e "\t\t\tinnobackupex --copy-back $backupDir/BACKUP-DIR";41 echo -e "\t\t\tRestore the ownership of the files in the MySQL data directory (chown -R mysql:mysql /var/lib/mysql/)";42 echo -e "\t\t\tStart your MySQL server";43 echo -e "\tRestore an incremental backup";44 echo -e "\t\t\tIf compressed, first decompress the backup (see above)";45 echo -e "\t\t\tFirst, prepare the base backup";46 echo -e "\t\t\tinnobackupex --apply-log --redo-only $backupDir/FULL-BACKUP-DIR";47 echo -e "\t\t\tNow, apply the incremental backup to the base backup.";48 echo -e "\t\t\tIf you have multiple incrementals, pass the --redo-only when merging all incrementals except for the last one. Also, merge them in the chronological order that the backups were made";49 echo -e "\t\t\tinnobackupex --apply-log --redo-only $backupDir/FULL-BACKUP-DIR --incremental-dir=$backupDir/INC-BACKUP-DIR";50 echo -e "\t\t\tOnce you merge the base with all the increments, you can prepare it to roll back the uncommitted transactions:";51 echo -e "\t\t\tinnobackupex --apply-log $backupDir/BACKUP-DIR";52 echo -e "\t\t\tFollow the same steps as for a full backup restore now";53 }54

55 while getopts ":h" opt; do

56 case $opt in

57 h)58 usage;59 exit 0

60 ;;61 \?)62 echo "Invalid option: -$OPTARG" >&2

63 exit 1

64 ;;65 esac

66 done

67

68 dateNow=`date +%Y-%m-%d_%H-%M-%S`69 dateNowUnix=`date +%s`70 backupLog=${backupDir}/backuplog71 delDay=`date -d "-$keepDays days" +%Y-%m-%d`72

73 # Check if innobackupex is installed (percona-xtrabackup)74 if [[ -z "$(command -v innobackupex)" ]]; then

75 echo "The innobackupex executable was not found, check if you have installed percona-xtrabackup."

76 exit 1

77 fi

78

79 ## check sshpass is install

80 if [[ -z "$(command -v sshpass)" ]]; then

81 echo "The innobackupex executable was not found, check if you have installed percona-xtrabackup."

82 yum -y installsshpass mutt83 fi

84

85

86 # Check ifbackup directory exists87 if [ ! -d "$backupDir" ]; then

88 echo "Backup directory does not exist. Check your config and create the backup directory"

89 exit 1

90 fi

91

92 # Check ifmail is installed93 if [[ $sendEmail == always ]] || [[ $sendEmail == onerror ]]; then

94 if [[ -z "$(command -v mail)" ]]; then

95 echo "You have enabled mail, but mail is not installed or not in PATH environment variable"

96 exit 1

97 fi

98 fi

99

100 # Check ifyou set a correct retention101 if [ $(($keepDays * 24)) -le $hoursBeforeFull ]; then

102 echo "ERROR: You have set hoursBeforeFull to $hoursBeforeFull and keepDays to $keepDays, this will delete all your backups... Change this"

103 exit 1

104 fi

105

106 # If you enabled sendEmail, check ifyou also set a recipient107 if [[ -z $emailAddress ]] && [[ $sendEmail == onerror ]]; then

108 echo "Error, you have enabled sendEmail but you have not configured any recipient"

109 exit 1

110 elif [[ -z $emailAddress ]] && [[ $sendEmail == always ]]; then

111 echo "Error, you have enabled sendEmail but you have not configured any recipient"

112 exit 1

113 fi

114

115 # If compression is enabled, pass it on to the backup command116 if [[ $compression == true ]]; then

117 compress="--compress"

118 compressThreads="--compress-threads=$compressThreads"

119 else

120 compress=

121 compressThreads=

122 fi

123

124 if [ -f "$backupDir"/latest_full ]; then

125 lastFull=`cat "$backupDir"/latest_full`126 fi

127

128 # Check foran existing full backup129 if [ ! -f "$backupDir"/latest_full ]; then

130 #echo "Latest full backup information not found... taking a first full backup now"

131 echo $dateNowUnix > "$backupDir"/latest_full132 lastFull=`cat "$backupDir"/latest_full`133 /usr/bin/innobackupex --host=$myhost --user=$mysqlUser --password=$mysqlPwd --no-timestamp $compress $compressThreads --slave-info --rsync "$backupDir"/"$dateNow"_full > $backupLog 2>&1

134 else

135 # Calculate the time since the lastfull backup136 difference=$((($dateNowUnix - $lastFull) / 60 / 60))137

138 # Check ifwe must take a full or incremental backup139 if [ $difference -lt $hoursBeforeFull ]; then

140 #echo "It's been $difference hours since last full, doing an incremental backup"

141 lastFullDir=`date -d@"$lastFull" '+%Y-%m-%d_%H-%M-%S'`142 /usr/bin/innobackupex --host=$myhost --user=$mysqlUser --password=$mysqlPwd --no-timestamp $compress $compressThreads --rsync --slave-info --incremental --incremental-basedir="$backupDir"/"$lastFullDir"_full "$backupDir"/"$dateNow"_incr > $backupLog 2>&1

143 else

144 #echo "It's been $difference hours since last full backup, time for a new full backup"

145 echo $dateNowUnix > "$backupDir"/latest_full146 /usr/bin/innobackupex --host=$myhost --user=$mysqlUser --password=$mysqlPwd --no-timestamp $compress $compressThreads --rsync --slave-info "$backupDir"/"$dateNow"_full > $backupLog 2>&1

147 fi

148 fi

149

150

151 #backup tail -n4#152 tail -n1 ${backupLog} >${tmpfile}153 #failinfo=`tail -n30 $backupLog`154 #sshpass -p2018_mgame scp -rp $backupDir/backuplog root@192.168.1.112:/tmp/

155 #txt=/tmp/${hostip}_backuplog156 #sshpass -p2018_mgame scp -rp -o StrictHostKeyChecking=no ${backupDir}/backuplog root@${remoteip}:${txt}157

158 # Check if the backup succeeded or failed, and e-mail the logfile, ifenabled159 if grep -q "completed OK" $tmpfile; then

160 echo "Backup completed OK"

161 if [[ $sendEmail = always ]]; then

162 sshpass -p$remotepass root@${remoteip} "echo"backup finished successfull"| mutt -s"${hostip}_AutoXtraBackup_log"$emailAddress -a $txt"

163 fi

164 else

165 echo "Backup FAILED"

166 if [[ $sendEmail = always ]] || [[ $sendEmail = onerror ]]; then

167 echo "onerror"

168 txt=/tmp/${hostip}_backuplog169 sshpass -p2018_mgame scp -rp -o StrictHostKeyChecking=no ${backupDir}/backuplog root@${remoteip}:${txt}170 #cat $backupLog | mutt -s "AutoXtraBackup log ${hostip}"$emailAddress171 sshpass -p$remotepass ssh -o 'StrictHostKeyChecking no' root@${remoteip} "echo"Xtrabackup backup ERROR specific information grab check attachment"| mutt -s"${hostip}_AutoXtraBackup_ERROR_log"$emailAddress -a $txt"

172 fi

173 exit 1

174 fi

175

176 # Delete backups older than retention date

177 rm -rf $backupDir/$delDay*

178

179 # Delete incremental backups with full backup base directory that was deleted180 for i in `find "$backupDir"/*incr -type f -iname xtrabackup_info 2>/dev/null | xargs grep $delDay | awk '{print $10}' | cut -d '=' -f2`; do rm -rf $i; done181

182 exit 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值