本文主要記錄下使用percona xtrabackup 2.4來備份mysql數據。最終效果是實現mysql所在主機定時進行全量備份和增量備份並自動刪除舊備份文件。另有一台數據存儲機,定時通過ssh抓取各個mysql主機上的備份文件,保存更長時間後刪除舊文件。
用到的軟件和技術有:xtrabackup(免費的mysql備份程序),qpress(xtrabackup壓縮需要的依賴,據說壓縮比很高),python3.5(ssh的遠程抓取,因為我不會bash)。
mkdir -p /root/42/script cd /root/42/script #安裝qpress wget http://www.quicklz.com/qpress-11-linux-x64.tar tar xf qpress-11-linux-x64.tar mv qpress /usr/local/bin/ #centos6安裝xtrabackup yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm yum install libev -y yum install percona-xtrabackup-24 -y #新建backup.sh(來源未知)內容如下:
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin BACKUP_BASE_DIR="/data/backup/xtrabackup" INC_BASE_LIST="${BACKUP_BASE_DIR}/inc_list.txt" XTRABACKUP_PATH="/usr/bin/innobackupex" MYSQL_CNF="/etc/my.cnf" MYSQL_HOSTNAME=127.0.0.1 MYSQL_USERNAME=root MYSQL_PASSWORD="" LOCK_FILE="/tmp/innobackupex.lock" THREAD=3 mkdir -p ${BACKUP_BASE_DIR} CURRENT_BACKUP_PATH="${BACKUP_BASE_DIR}/$(date +%F_%H-%M)" [[ -d ${CURRENT_BACKUP_PATH} ]] && CURRENT_BACKUP_PATH="${BACKUP_BASE_DIR}/$(date +%F_%H-%M-%S)" print_help(){ echo "--------------------------------------------------------------" echo "Usage: $0 full | inc | help " echo "--------------------------------------------------------------" exit 1 } [[ $# -lt 1 || "$1" == "help" ]] && print_help [[ -f "$LOCK_FILE" ]] && echo -e "Usage: rm -f $LOCK_FILE\nUsage: chattr -i $LOCK_FILE && rm -f $LOCK_FILE" && exit 1 FullBackup(){ touch $LOCK_FILE chattr +i $LOCK_FILE local rc=0 ${XTRABACKUP_PATH} \ --defaults-file=${MYSQL_CNF} \ --user=${MYSQL_USERNAME} \ --password=${MYSQL_PASSWORD} \ --host=${MYSQL_HOSTNAME} \ --compress \ --compress-threads=${THREAD} \ --parallel=${THREAD} \ --no-timestamp ${CURRENT_BACKUP_PATH} > ${CURRENT_BACKUP_PATH}_full.log 2>&1 grep ".*\ completed\ OK\!" ${CURRENT_BACKUP_PATH}_full.log > /dev/null 2>&1 if [ $? -ne 0 ];then rc=1 [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && rm -rf ${CURRENT_BACKUP_PATH} else echo "NULL|${CURRENT_BACKUP_PATH}|full" >> ${INC_BASE_LIST} [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && chattr +i ${CURRENT_BACKUP_PATH} || rc=1 fi chattr -i ${LOCK_FILE} rm -f $LOCK_FILE chattr +a ${INC_BASE_LIST} return $rc } IncBackup(){ touch $LOCK_FILE chattr +i $LOCK_FILE local rc=0 PREV_BACKUP_DIR=$(sed '/^$/d' ${INC_BASE_LIST} | tail -1 | awk -F '|' '{print $2}') ${XTRABACKUP_PATH} \ --defaults-file=${MYSQL_CNF} \ --user=${MYSQL_USERNAME} \ --password=${MYSQL_PASSWORD} \ --host=${MYSQL_HOSTNAME} \ --compress \ --compress-threads=${THREAD} \ --no-timestamp --incremental ${CURRENT_BACKUP_PATH} \ --incremental-basedir=${PREV_BACKUP_DIR} > ${CURRENT_BACKUP_PATH}_inc.log 2>&1 grep ".*\ completed\ OK\!" ${CURRENT_BACKUP_PATH}_inc.log > /dev/null 2>&1 if [ $? -ne 0 ];then rc=1 [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && rm -rf ${CURRENT_BACKUP_PATH} else echo "${PREV_BACKUP_DIR}|${CURRENT_BACKUP_PATH}|inc" >> ${INC_BASE_LIST} [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && chattr +i ${CURRENT_BACKUP_PATH} || rc=1 fi chattr -i ${LOCK_FILE} rm -f $LOCK_FILE chattr +a ${INC_BASE_LIST} return $rc } ## 全量備份 if [ "$1" == "full" ];then FullBackup fi ## 增量備份 if [ "$1" == "inc" ];then ## 若全量備份不存在,則執行全量備份 if [[ ! -f ${INC_BASE_LIST} || $(sed '/^$/d' ${INC_BASE_LIST} | wc -l) -eq 0 ]];then FullBackup else IncBackup fi fi ## 刪除22天前的備份 if [[ -d ${BACKUP_BASE_DIR} && $(pwd) != "/" ]];then find ${BACKUP_BASE_DIR} -name "$(date -d '22 days ago' +'%F')_*" | xargs chattr -i find ${BACKUP_BASE_DIR} -name "$(date -d '22 days ago' +'%F')_*" | xargs rm -rf fi
#可能需要修改backup.sh中的如下參數 BACKUP_BASE_DIR="/data/backup/xtrabackup" XTRABACKUP_PATH="/usr/bin/innobackupex" MYSQL_CNF="/etc/my.cnf" MYSQL_HOSTNAME=127.0.0.1 MYSQL_USERNAME=root MYSQL_PASSWORD="" #給腳本添加執行權限 chmod 755 /root/42/script/backup.sh #执行一次全量备份 /root/42/script/backup.sh full #如果沒有錯誤信息,就可以看到BACKUP_BASE_DIR下生成了備份文件 #檢查下cron是否啟動 service crond status #如果未啟動則將cron設置為開機啟動並手動啟動下 chkconfig crond on service crond start #添加定時任務 crontab -e #在底部新增內容 ## 每周六凌晨3:30一次全量备份 ## 每周二、四、日的凌晨3:30点执行增量备份 30 3 * * 6 /root/42/script/backup.sh full 30 3 * * 2,4,7 /root/42/script/backup.sh inc
本文更新於 2017/09/25。