Xtrabackup流備份與恢復

Xtrabackup是MySQL數據庫的備份不可多得的工具之一。提供了全備,增備,數據庫級別,表級別備份等等。最牛X的還有不落盤的備份,即流備份方式。對於服務器上空間不足,或是搭建主從,直接使用流式備份大大簡化了備份後的壓縮複製所帶來的更多開銷。Xtrabackup支持tar格式以及xbstream格式的流備份。本文便是對此展開的相關描述。數據庫

 

一、基於tar格式備份
a、備份到本地
# innobackupex --stream=tar /tmp >/backup/bak.tar ###非壓縮方式
# innobackupex --stream=tar /tmp |gzip >/backup/bakz.tar.gz ###壓縮方式
# ls -hltr
total 42M
-rw-r--r-- 1 root root 39M Apr 15 17:23 bak.tar
-rw-r--r-- 1 root root 3.3M Apr 15 17:24 bakz.tar.gzbash

###解壓備份
# mkdir bak bakz
# tar -xivf bak.tar -C /backup/bak
# tar -xizvf bakz.tar.gz -C /backup/bakz
# du -sh *
38M bak
39M bak.tar
38M bakz
3.3M bakz.tar.gz服務器

b、備份到遠程
# innobackupex --stream=tar /tmp | ssh root@192.168.1.7 \ "cat - > /backup/bak.tar" ###非壓縮方式
# innobackupex --stream=tar /tmp | ssh root@192.168.1.7 \ "gzip >/backup/bak.tar.gz" ###壓縮方式app

 

二、使用xbstream格式備份
a、備份到本地
# innobackupex --stream=xbstream /tmp >/backup/bak.xbstream ###非壓縮方式
# innobackupex --stream=xbstream --compress /tmp >/backup/bak_compress.xbstream ###壓縮方式
# ls -hltr
total 43M
-rw-r--r-- 1 root root 37M Apr 15 17:41 bak.xbstream
-rw-r--r-- 1 root root 6.0M Apr 15 17:41 bak_compress.xbstreamssh

###解壓備份
# mkdir bk bk_compress
# xbstream -x < bak.xbstream -C /backup/bk ###解壓xbstream格式ide

###解壓xbstream格式,compress參數的備份
# xbstream -x < bak_compress.xbstream -C /backup/bk_compress ###首先解壓xbstream
# for bf in `find . -iname "*\.qp"`; do qpress -d $bf $(dirname $bf) && rm $bf; done ###再解壓qp壓縮格式
# innobackupex --decompress /backup/bk_compress ###若是xtrabackup版本大於2.1.4,能夠直接經過該方式解壓工具

b、備份到遠程
###使用壓縮備份到遠程並解壓
# innobackupex --stream=xbstream --compress /tmp | ssh root@192.168.1.7 "xbstream -x -C /backup/stream".net

 

三、流備份的全備與增備
###全備數據庫,使用--extra-lsndir參數生產checkpoints文件
# innobackupex --stream=xbstream --compress --extra-lsndir=/backup/chkpoint /tmp >/backup/bak_compress.xbstream
# more /backup/chkpoint/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 8408290
last_lsn = 8408290
compact = 0日誌

### Author : Leshami
### Blog : http://blog.csdn.net/leshami
###增備數據庫,若是後續還須要再次增備,則能夠再次指定--extra-lsndir,若是與上次備份指定相同的位置,該文件被覆蓋
# innobackupex --compress --incremental --extra-lsndir=/backup/chkpoint --incremental-basedir=/backup/chkpoint \
> --stream=xbstream /tmp >/backup/bak_compress_inc.xbstreamcode

 

四、流備份的異機恢復
###備份到異機
# innobackupex --stream=xbstream --extra-lsndir=/backup/chkpoint /tmp | sshroot@192.168.1.7 "xbstream -x -C /backup/stream"
# innobackupex --incremental --extra-lsndir=/backup/chkpoint --incremental-basedir=/backup/chkpoint --stream=xbstream \
> /tmp | ssh root@192.168.1.7 "xbstream -x -C /backup/stream_inc"

###異機恢復,copy-back及後續步驟省略
# innobackupex --apply-log --redo-only /backup/stream
# innobackupex --apply-log /backup/stream --incremental-dir=/backup/stream_inc

 

五、其它注意事項
a、若是使用xbstream格式異機備份時,異機未安裝xbstream(封裝在xtrabackup中)則出現以下錯誤提示。
bash: xbstream: command not found
xtrabackup: Error writing file 'UNOPENED' (Errcode: 32 - Broken pipe)
xb_stream_write_data() failed.
compress: write to the destination stream failed.
xtrabackup: Error writing file 'UNOPENED' (Errcode: 32 - Broken pipe)
xb_stream_write_data() failed.
xtrabackup: Error writing file 'UNOPENED' (Errcode: 32 - Broken pipe)
[01] xtrabackup: Error: xtrabackup_copy_datafile() failed.
[01] xtrabackup: Error: failed to copy datafile.
innobackupex: Error: The xtrabackup child process has died at /usr/bin/innobackupex line 2681.

b、異機備份時須要創建等效性,以下示例
# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
29:45:ee:8d:b3:55:f1:5f:2f:da:2a:88:0c:0d:37:9f root@vdbsrv1

###copy 公鑰到遠程主機
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.7
21
The authenticity of host '192.168.1.7 (192.168.1.7)' can't be established.
RSA key fingerprint is 1d:7c:40:98:ef:de:6f:b8:8c:b2:87:72:0e:79:db:0a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.7' (RSA) to the list of known hosts.
root@192.168.1.7's password:
Now try logging into the machine, with "ssh 'root@192.168.1.7'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

###驗證等效性是否成功
# ssh 192.168.1.7 date;
Wed Apr 15 17:55:31 CST 2015

c、使用tar格式遠程增量備份時收到以下提示,即只支持xbstream
xtrabackup: error: streaming incremental backups are incompatible with the
'tar' streaming format. Use --stream=xbstream instead.

d、用流備份,默認的臨時目錄都是系統的/tmp目錄,須要保證該目錄有足夠的空間,或指定--tmpdir選項

e、流備份日誌輸出innobackupex --stream=xbstream /tmp 2>>"$backupLog" | gzip > "$backup_file" 2>>"$backupLog"

相關文章
相關標籤/搜索