下文內(nèi)容主要給大家?guī)鞽trabackup安裝指南,所講到的知識(shí),與書籍略有不同,都是創(chuàng)新互聯(lián)專業(yè)技術(shù)人員在與用戶接觸過程中,總結(jié)出來的,具有一定的經(jīng)驗(yàn)分享價(jià)值,希望給廣大讀者帶來幫助。
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都石牌坊等企業(yè)提供專業(yè)服務(wù)。
文件準(zhǔn)備
[root@wuxiang11 ~]# cd percona-xtrabackup/
[root@wuxiang11 percona-xtrabackup]# ls
libev-4.15-1.el6.rf.x86_64.rpm percona-release-0.1-4.noarch.rpm percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
開始安裝依賴文件
[root@wuxiang11 percona-xtrabackup]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm
warning: libev-4.15-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing... ########################################### [100%]
1:libev ########################################### [100%]
安裝-01
[root@wuxiang11 percona-xtrabackup]# rpm -ivH percona-release-0.1-4.noarch.rpm
Preparing packages for installation...
percona-release-0.1-4
[root@wuxiang11 percona-xtrabackup]# yum list | grep percona
馬上就安裝完成
[root@wuxiang11 percona-xtrabackup]# yum install percona-xtrabackup-24
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.11-1.el6 will be installed
--> Processing Dependency: perl(DBD::MySQL) for package: percona-xtrabackup-24-2.4.11-1.el6.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
--> Processing Dependency: perl(DBI::Const::GetInfoType) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: perl(DBI) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Running transaction check
---> Package Percona-Server-shared-51.x86_64 0:5.1.73-rel14.12.625.rhel6 will be installed
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
percona-xtrabackup-24 x86_64 2.4.11-1.el6 percona-release-x86_64 8.1 M
Installing for dependencies:
Percona-Server-shared-51 x86_64 5.1.73-rel14.12.625.rhel6 percona-release-x86_64 2.1 M
perl-DBD-MySQL x86_64 4.013-3.el6 centos6.7_64 134 k
perl-DBI x86_64 1.609-4.el6 centos6.7_64 705 k
Transaction Summary
===============================================================================================================================================================================================
Install 4 Package(s)
Total download size: 11 M
Installed size: 12 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): Percona-Server-shared-51-5.1.73-rel14.12.625.rhel6.x86_64.rpm | 2.1 MB 00:02
(2/4): percona-xtrabackup-24-2.4.11-1.el6.x86_64.rpm | 8.1 MB 00:01
(3/4): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm | 134 kB 00:00
(4/4): perl-DBI-1.609-4.el6.x86_64.rpm | 705 kB 00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 2.5 MB/s | 11 MB 00:04
warning: rpmts_HdrFromFdno: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Percona
Importing GPG key 0xCD2EFD2A:
Userid : Percona MySQL Development Team
Package: percona-release-0.1-4.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-Percona
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
** Found 3 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.6.6-6.el6_5.x86_64 has missing requires of libmysqlclient.so.16()(64bit)
2:postfix-2.6.6-6.el6_5.x86_64 has missing requires of libmysqlclient.so.16(libmysqlclient_16)(64bit)
2:postfix-2.6.6-6.el6_5.x86_64 has missing requires of mysql-libs
Installing : perl-DBI-1.609-4.el6.x86_64 1/4
Installing : Percona-Server-shared-51-5.1.73-rel14.12.625.rhel6.x86_64 2/4
Installing : perl-DBD-MySQL-4.013-3.el6.x86_64 3/4
Installing : percona-xtrabackup-24-2.4.11-1.el6.x86_64 4/4
Verifying : percona-xtrabackup-24-2.4.11-1.el6.x86_64 1/4
Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 2/4
Verifying : Percona-Server-shared-51-5.1.73-rel14.12.625.rhel6.x86_64 3/4
Verifying : perl-DBI-1.609-4.el6.x86_64 4/4
Installed:
percona-xtrabackup-24.x86_64 0:2.4.11-1.el6
Dependency Installed:
Percona-Server-shared-51.x86_64 0:5.1.73-rel14.12.625.rhel6 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6
Complete!
檢驗(yàn)是否安裝成功
[root@wuxiang11 percona-xtrabackup]# rpm -qa|grep xtrabackup percona-xtrabackup-24-2.4.11-1.el6.x86_64 [root@wuxiang11 percona-xtrabackup]#
Xtrabackup提供了全量備份和增量備份兩種方式,全量就不解釋了,增量是指其可以只備份指定位置后的新增數(shù)據(jù)。本文介紹的增量備份方法使用到了LSN(Log Sequence Number),可以從備份成功文件夾的xtrabackup_checkpoints文件中獲取,其中to_lsn就是下次增量備份的起始位置。
結(jié)合兩者,我們可以得到下圖的備份思路。
解釋如下:
#!/bin/sh
# xtrabackup的相關(guān)配置
INNOBACKUPEX="innobackupex "
MY_CNF="/home/config/mysql/3307.backup.cnf"
MY_USER="xtrabackup"
MY_PASSWORD="xtrabackup"
MY_SOCKET="/home/socket/mysql/3307.sock"
# 遠(yuǎn)程備份機(jī) 文件名配置
REMOTE_HOST="dbbackup"
REMOTE_DIR="/home/backup/mysql/test"
LOCAL_LSN_FILE="~/.to_lsn_important"
DATE_NAME=`date +%Y-%m-%d-%H-%M-%S`
REMOTE_FILE=$DATE_NAME.tar.gz
LOCK_FILE="~/.mysql.backup.lock"
LOCAL_BACKUP_DIR="/home/backup/mysql/test/$DATE_NAME"
# 輸出幫助信息
function usage()
{
echo "Usage:"
echo "-f db will be backuped fully with this parameter. If not , incrementally. "
}
#防止同時(shí)執(zhí)行兩個(gè)備份命令,發(fā)生沖突
if [ -f $LOCK_FILE ] ;then
echo 'Mysql backup lockfile is locked!'
exit 0
fi
full=0
while getopts "fh" arg #選項(xiàng)后面的冒號(hào)表示該選項(xiàng)需要參數(shù)
do
case $arg in
f)
full=1
;;
h) # 輸出幫助信息
usage
exit 0
;;
esac
done
echo "backup dir is $REMOTE_DIR/$REMOTE_FILE"
# backup up db to remote host
echo "start to backup db!"`date +%Y-%m-%d-%H-%M-%S`
if [ "$full"x = "1"x ] ;then
# 全量備份
echo '1' > $LOCK_FILE
$INNOBACKUPEX --defaults-file=$MY_CNF --user=$MY_USER --password=$MY_PASSWORD --socket=$MY_SOCKET ./ --stream=tar | gzip | ssh $REMOTE_HOST "cat - > $REMOTE_DIR/FULL-$REMOTE_FILE"
ssh $REMOTE_HOST "cd $REMOTE_DIR;rm -f xtrabackup_checkpoints;tar zxfi $REMOTE_DIR/FULL-$REMOTE_FILE xtrabackup_checkpoints "
toLSN=$( ssh $REMOTE_HOST "cat $REMOTE_DIR/xtrabackup_checkpoints|grep to_lsn|awk -F= '{gsub(/ /,\"\",\$2);print \$2}'" )
if [ $toLSN ] ;then
echo $toLSN > $LOCAL_LSN_FILE
else
echo 'no lsn from remote host!please check!'
fi
else
# 增量備份
if [ -f $LOCAL_LSN_FILE ] ;then
toLSN=`cat $LOCAL_LSN_FILE`
fi
if [ ! $toLSN ] ;then
echo 'last LSN is not set !please check!'
exit 0
fi
echo '1' > $LOCK_FILE
mkdir -p $LOCAL_BACKUP_DIR
echo "last to lsn is "$toLSN
$INNOBACKUPEX --parallel=6 --defaults-file=$MY_CNF --user=$MY_USER --password=$MY_PASSWORD --socket=$MY_SOCKET --incremental --incremental-lsn=$toLSN $LOCAL_BACKUP_DIR 2>/tmp/innobackexLog
toLSN=$( cd $LOCAL_BACKUP_DIR/*; cat xtrabackup_checkpoints|grep to_lsn|awk -F= '{gsub(/ /,"",$2);print $2}' )
echo "new to lsn is "$toLSN;
if [ $toLSN ] ;then
echo $toLSN > $LOCAL_LSN_FILE
cd $LOCAL_BACKUP_DIR/*;tar zc .|ssh $REMOTE_HOST "cat - > $REMOTE_DIR/$REMOTE_FILE"
echo "save file to $REMOTE_HOST @ $REMOTE_DIR/$REMOTE_FILE"
else
echo 'no lsn from local backup file!delete remote backup file!'$LOCAL_BACKUP_DIR/$REMOTE_FILE
fi
echo "remove $LOCAL_BACKUP_DIR"
rm -rf $LOCAL_BACKUP_DIR
fi
rm -f $LOCK_FILE
echo "end to backup db!"`date +%Y-%m-%d-%H-%M-%S`
將上述腳本配置好后,放到mysql主機(jī)上,執(zhí)行 sh db-backup.sh -f進(jìn)行全量備份,之后將sh backup.sh添加到crontab中每小時(shí)增量備份一次就可以了。
注意--parallel=6使用是有條件的,參考文檔
一旦線上數(shù)據(jù)庫發(fā)生宕機(jī)之類的災(zāi)難性事故,備份數(shù)據(jù)就要立馬發(fā)揮作用了。打開備份文件夾一看,如上面的圖,每小時(shí)一個(gè)壓縮文件,時(shí)間久了,茫茫多,上百個(gè)壓縮文件,簡(jiǎn)直是一定的,這必須得來個(gè)腳本自動(dòng)化恢復(fù)數(shù)據(jù)才成。思路很簡(jiǎn)單,見下圖:
解釋如下:
#!/bin/sh
BACK_DIR="/home/backup/mysql/test"
RESTORE_DIR="/home/tmpback/test"
# xtrabackup恢復(fù)時(shí)使用的配置文件可能與數(shù)據(jù)庫配置文件不同
RESTORE_MY_CNF="/home/config/mysql/3307.backup.cnf"
# 啟動(dòng)mysql需要的配置文件,按需設(shè)定
MY_CNF="/home/config/mysql/3307.cnf"
MY_DATA_DIR="/home/data/mysql/3307"
MY_SOCK_DIR="/home/socket/mysql/"
MY_LOG_DIR="/home/logs/mysqld/3307"
#extract all tar file
fullFileName=""
for file in $( ls $BACK_DIR | grep "tar.gz" )
do
fileName=${file%.tar.gz}
full=${file%%-*}
if [ "${full}"x = "FULL"x ] ;then
fullFileName=$fileName
fi
DEST_DIR="$RESTORE_DIR/$fileName"
if [ -d $DEST_DIR ] ;then
echo "$DEST_DIR exists!"
else
mkdir -p $DEST_DIR
echo "start to extract file $file to $DEST_DIR"
tar zxif $BACK_DIR/$file -C $RESTORE_DIR/$fileName
echo "end to extract file "$file
fi
done
echo "full backup dir is "$fullFileName
echo "**start to repare full backup dir"
# 恢復(fù)全量數(shù)據(jù)
echo "innobackupex --apply-log --redo-only --use-memory=4G $RESTORE_DIR/$fullFileName"
innobackupex --apply-log --redo-only --use-memory=4G "$RESTORE_DIR/$fullFileName"
echo "**end to repare full backup dir"
# 恢復(fù)增量數(shù)據(jù)
for file in $( ls $RESTORE_DIR|grep -v "FULL" )
do
echo "**start to repare $file"
echo "innobackupex --apply-log --redo-only --use-memory=4G $RESTORE_DIR/$fullFileName --incremental-dir=$RESTORE_DIR/$file"
innobackupex --apply-log --redo-only --use-memory=4G "$RESTORE_DIR/$fullFileName" --incremental-dir="$RESTORE_DIR/$file"
echo "**end to repare $file"
done
echo "**start to copy back data to mysql"
rm -rf $MY_DATA_DIR
mkdir $MY_DATA_DIR
mkdir $MY_SOCK_DIR
mkdir $MY_LOG_DIR
#將數(shù)據(jù)恢復(fù)到mysql中
echo "innobackupex --defaults-file=$RESTORE_MY_CNF --copy-back $RESTORE_DIR/$fullFileName"
innobackupex --defaults-file=$RESTORE_MY_CNF --copy-back $RESTORE_DIR/$fullFileName
echo "**end to copy back data to mysql"
chown -R mysql:mysql $MY_DATA_DIR
chown -R mysql:mysql $MY_SOCK_DIR
chown -R mysql:mysql $MY_LOG_DIR
echo "All data has been restored! Try to start mysql now"
echo "mysqld_multi --defaults-file=$MY_CNF start 1"
使用方法很簡(jiǎn)單,配置好后,直接運(yùn)行sh restore.sh就可以了。
對(duì)于以上關(guān)于Xtrabackup安裝指南,如果大家還有更多需要了解的可以持續(xù)關(guān)注我們創(chuàng)新互聯(lián)的行業(yè)推新,如需獲取專業(yè)解答,可在官網(wǎng)聯(lián)系售前售后的,希望該文章可給大家?guī)硪欢ǖ闹R(shí)更新。