此备份方案设置为两个脚本 xtrabackup.sh 为通用脚本
call_xtrabackup_copy.sh为调用脚本
经过两台服务器进行备份,所有脚本在192.168.4.2上运行,备份远端的数据库服务器,而且数据库服务器上需要安装sshpass
192.168.1.112 服务器是为mutt发送邮件服务器
1 [root@mysql-backup-192.168.4.2 scripts]$ cat call_xtrabackup_copy.sh 2 #!/bin/bash 3 log(){ 4 echo "`date "+%Y-%m-%d %H:%M:%S" ` $@" 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' root@${hostip} -C "/bin/bash -s -x" < /home/work/scripts/xtrabackup.sh $remote_backupdir 31 log "-s $1为备份脚本添加参数,此参数代表在远程服务器备份的目录,代表挂载的备份盘: sshpass -p2018_mgame ssh -o 'StrictHostKeyChecking no' root@${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' root@${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 [root@mysql-backup-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 emailAddress=fuyuntao@baidu-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 root@192.168.1.112:/tmp/ 155 #txt=/tmp/${hostip}_backuplog 156 #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, if enabled 159 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}_backuplog 169 sshpass -p2018_mgame scp -rp -o StrictHostKeyChecking=no ${backupDir}/backuplog root@${remoteip}:${txt} 170 #cat $backupLog | mutt -s "AutoXtraBackup log ${hostip}" $emailAddress 171 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 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挂载到要备份的数据库服务器上。