1. 程式人生 > >percona for mysql5.6 使用xtrabackup 備份一周全備每天基於全備進行增量備份腳本

percona for mysql5.6 使用xtrabackup 備份一周全備每天基於全備進行增量備份腳本

庫服務器 sshpass als top 備份腳本 basedir grep error: 調用腳本

此備份方案設置為兩個腳本 xtrabackup.sh 為通用腳本
call_xtrabackup_copy.sh為調用腳本
經過兩臺服務器進行備份,所有腳本在192.168.4.2上運行,備份遠端的數據庫服務器,而且數據庫服務器上需要安裝sshpass
192.168.1.112 服務器是為mutt發送郵件服務器

 1 [[email protected]192.168.4.2 scripts]$ cat call_xtrabackup_copy.sh 
 2 #!/bin/bash
 3 log(){
 4     echo "`date "+%Y-%m-%d %H:%M:%S"
` [email protected]" 5 } 6 7 8 #項目名 9 total=userchannelp1 10 log "項目名 ${total}" 11 #遠程服務器(需要備份的數據庫服務器)存放備份的目錄,此目錄為掛載的nfs目錄 12 remote_backupdir=/backup/backup/${total} 13 log "遠程服務器存放備份的目錄 ${remote_backupdir}" 14 #此目錄為存放備份文件的目錄存在於備份存放服務器 15 backupDir=/home/bigdata/backup/${total} 16 log "
此目錄為存放備份文件的目錄存在於備份存放服務器 ${backupDir}" 17 #bos系統存放備份的目錄 18 bosdir=/xtrabackup-new/${total} 19 log "bos系統存放備份的目錄 ${bosdir}" 20 #備份產生的日誌在4.2的目錄 21 backupLog=${backupDir}/backuplog 22 log "備份產生的日誌在4.2的目錄 ${backupLog}" 23 #過濾備份日誌的結果存放的臨時文件 24 tmpfile=/tmp/completed 25 log "過濾備份日誌的結果存放的臨時文件 ${tmpfile}"
26 #遠程數據庫ip 27 hostip=192.168.2.250 28 log "遠程數據庫ip ${hostip}" 29 #-s $1為備份腳本添加參數,此參數代表在遠程服務器備份的目錄,代表掛載的備份盤 30 sshpass -p2018_mgame ssh -o StrictHostKeyChecking no [email protected]${hostip} -C "/bin/bash -s -x" < /home/work/scripts/xtrabackup.sh $remote_backupdir 31 log "-s $1為備份腳本添加參數,此參數代表在遠程服務器備份的目錄,代表掛載的備份盤: sshpass -p2018_mgame ssh -o ‘StrictHostKeyChecking no‘ [email protected]${hostip} -C "/bin/bash -s -x" < /home/work/scripts/xtrabackup.sh $remote_backupdir" 32 33 # tar backup 34 LATEST_FULL_BACKUP_CREATED_TIME=`find $backupDir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1` 35 log "查找最近備份文件 ${LATEST_FULL_BACKUP_CREATED_TIME}" 36 37 #cd $backupDir && tar -c $LATEST_FULL_BACKUP_CREATED_TIME/ | lz4 -B4 > $tar 38 tar=$LATEST_FULL_BACKUP_CREATED_TIME.tar.lz4 39 log "要被壓縮所產生的文件名: ${tar}" 40 #解壓說明:lz4+tar uncompress "lz4 -d -B7 2019-04-09_14-20-15_incr.tar.lz4 |tar -xC aa/" 41 42 tail -n1 ${backupLog} > ${tmpfile} 43 log "過濾備份日誌,檢查備份是否成功然後導入到臨時文件中,以備查詢 tail -n1 ${backupLog} > ${tmpfile}" 44 45 if grep -q "completed OK" $tmpfile; then 46 echo "Backup completed OK" 47 log "echo "Backup completed OK"" 48 cd $backupDir;tar -c ${LATEST_FULL_BACKUP_CREATED_TIME}/ | lz4 -B4 > $tar;bcecmd bos cp $tar bos:${bosdir}/;rm -rf $tar 49 log "cd $backupDir;tar -c ${LATEST_FULL_BACKUP_CREATED_TIME}/ | lz4 -B4 > $tar;bcecmd bos cp $tar bos:${bosdir}/;rm -rf $tar 執行完成" 50 else 51 # failinfo=`tail -n30 ${backupLog}a` 52 echo "Backup FAILED" 53 log "echo "Backup FAILED"" 54 # sshpass -p${muttserverpass} ssh -o StrictHostKeyChecking no [email protected]${muttserverip} " echo "backup fail" | mutt -s "${hostip}_AutoXtraBackup_error_log" $emailAddress" 55 rm -rf ${backupDir}/${LATEST_FULL_BACKUP_CREATED_TIME} 56 log "備份失敗 刪除相關失敗的備份目錄 rm -rf ${backupDir}/${LATEST_FULL_BACKUP_CREATED_TIME}" 57 echo "fail" 58 log "fail" 59 exit 1 60 log "end" 61 fi
  1 [[email protected]192.168.4.2 scripts]$ cat xtrabackup.sh 
  2 #!/bin/bash
  3 # MySQL backup script
  4 # https://github.com/gregorystorme/autoxtrabackup
  5 # Copyright (c) 2014 Gregory Storme
  6 # Version: 0.2
  7 
  8 if [ -f /etc/default/autoxtrabackup ] ; then
  9         . /etc/default/autoxtrabackup
 10 else
 11         backupDir=$1
 12         hoursBeforeFull=24
 13         mysqlUser=root
 14         mysqlPwd=1qaz2wsx
 15     myhost=127.0.0.1
 16         compression=true
 17         compressThreads=4
 18         keepDays=7
 19         sendEmail=onerror
 20         [email protected]mgame.com
 21     hostip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk {print $2}|tr -d "addr:"`
 22     remotepass=2018_mgame
 23     remoteip=192.168.1.112
 24     tmpfile=/tmp/compeletd
 25 fi
 26 
 27 #####
 28 # No editing should be required below this line
 29 #####
 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}/backuplog
 71 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 install sshpass mutt
 83 fi
 84 
 85 
 86 # Check if backup directory exists
 87 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 if mail is installed
 93 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 if you set a correct retention
101 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 if you also set a recipient
107 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 command
116 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 for an existing full backup
129 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_full
132         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 last full backup
136         difference=$((($dateNowUnix - $lastFull) / 60 / 60))
137 
138         # Check if we must take a full or incremental backup
139         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_full
146                 /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  [email protected]192.168.1.112:/tmp/
155 #txt=/tmp/${hostip}_backuplog
156 #sshpass -p2018_mgame scp -rp -o StrictHostKeyChecking=no ${backupDir}/backuplog  [email protected]${remoteip}:${txt}
157 
158 # Check if the backup succeeded or failed, and e-mail the logfile, if enabled
159 if grep -q "completed OK" $tmpfile; then
160         echo "Backup completed OK"
161         if [[ $sendEmail = always ]]; then
162         sshpass -p$remotepass  [email protected]${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}_backuplog
169         sshpass -p2018_mgame scp -rp -o StrictHostKeyChecking=no ${backupDir}/backuplog  [email protected]${remoteip}:${txt}
170         #cat $backupLog | mutt -s "AutoXtraBackup log ${hostip}" $emailAddress
171         sshpass -p$remotepass ssh -o StrictHostKeyChecking no [email protected]${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 deleted
180 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; done
181 
182 exit 0

 最後一個調用腳本是一個外國友人寫的 我只不過在他的基礎上進行包裝了一次,此腳本未進行完善,還有部分問題,每臺機器上需要安裝sshpass 及lz4。

此腳本的中心是,腳本運行在一臺統一備份服務器上進行遠程備份,並且把備份服務器進行nfs掛載到要備份的數據庫服務器上。

percona for mysql5.6 使用xtrabackup 備份一周全備每天基於全備進行增量備份腳本