当前位置: 首页 > 工具软件 > XtraBackup > 使用案例 >

Xtrabackup流备与恢复

刘俊语
2023-12-01

Xtrabackup是MySQL数据库的备份不可多得的工具之一。提供了全备,增备,数据库级别,表级别备份等等。最牛X的还有不落盘的备份,即流备份方式。对于服务器上空间不足,或是搭建主从,直接使用流式备份大大简化了备份后的压缩复制所带来的更多开销。Xtrabackup支持tar格式以及xbstream格式的流备份。本文即是对此展开的相关描述。

 

1、基于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.gz

###解压备份
# 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" ###压缩方式

 

2、使用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.xbstream

###解压备份
# mkdir bk bk_compress
# xbstream -x < bak.xbstream -C /backup/bk ###解压xbstream格式

###解压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"

 

3、流备份的全备与增备
###全备数据库,使用--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.xbstream

 

4、流备份的异机恢复
###备份到异机
# 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

 

5、其它注意事项
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"

转自:https://www.cnblogs.com/hllnj2008/p/5207066.html

 类似资料: