xtrabackup+binlog异机增量恢复到某个时间点实验

时间:2022-10-17 06:25:50

一.基本信息
版本:10.0.20-MariaDB-log
数据文件和redo log位置:/apps/dbdat/mariadb10_data3306/
bing log位置:/apps/dbdat/mariadb10_data3306/log
主机信息:mvxl0784 10.16.24.109
配置文件:/etc/my3306.cnf

二.xtrabackup工具准备
下载,并解压到/usr/local下
cd /data/tool
tar -xvf percona-xtrabackup-2.2.9-Linux-x86_64.tar.gz -C /usr/local/
cd /usr/local
chown -R apps:apps percona-xtrabackup-2.2.9-Linux-x86_64

修改.bash_profile环境变量:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

export LANG=zh_CN.GB18030
export LANG=zh_CN.GB18030
export PATH=$PATH:/usr/local/percona-xtrabackup-2.2.9-Linux-x86_64/bin


三.从备份主机中copy最新备份或从磁带中还原xtrabackup的备份集

假定我们已从生产主机中copy了最新一份备份:
[root@MCBL051 xtrabackup]# ls -ltr
total 8
drwxr-xr-x 8 apps apps 4096 May 15 01:11 xtrabackup_20160515010002
drwxr-xr-x 8 apps apps 4096 May 17 01:11 xtrabackup_20160517010001

copy上面5月17号1点11分产生的xtrabackup备份到恢复主机,我们需要在恢复主机上将mysql恢复到18号0点,需要将增量备份的binlog找到后copy到恢复主机上。
[root@MCBL051 log]# ls -ltr
total 2183208
-rw-rw---- 1 apps apps 536871276 May 15 22:27 mysql-bin.000269
-rw-rw---- 1 apps apps 536871156 May 16 17:14 mysql-bin.000270
-rw-rw---- 1 apps apps 536871119 May 17 09:29 mysql-bin.000271
-rw-rw---- 1 apps apps 536871210 May 18 00:56 mysql-bin.000272
-rw-rw---- 1 apps apps       260 May 18 00:56 mysql-bin.index
-rw-rw---- 1 apps apps  86433462 May 18 09:31 mysql-bin.000273

上面是所有binlog显示,只需将mysql-bin.000271、mysql-bin.000272进行copy。

恢复主机上的备份文件在:
[apps@mvxl0784 backup]$ ls -ltr
total 1048596
drwxr-xr-x 8 root root      4096 May 18 09:30 xtrabackup_20160517010001
-rw-r----- 1 root root 536871119 May 18 09:37 mysql-bin.000271
-rw-r----- 1 root root 536871210 May 18 09:37 mysql-bin.000272
[apps@mvxl0784 backup]$ pwd
/apps/backup
更改文件的onwer权限:
chown -R apps:apps /apps/backup

四.恢复
解压:
innobackupex --decompress /apps/backup/xtrabackup_20160517010001
最后显示如下,表示成功解压
160518 09:59:05  innobackupex: completed OK!

准备备份集:
innobackupex --apply-log /apps/backup/xtrabackup_20160517010001
最后出现如下,表示成功完成.
[notice (again)]
  If you use binary log and don't use any hack of group commit,
  the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 291755824, file name /apps/dbdat/mariadb10_data3306/log/mysql-bin.000271

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 377271951224
160518 10:03:36  innobackupex: completed OK!

执行恢复:
innobackupex --defaults-file=/etc/my3306.cnf  --copy-back /apps/backup/xtrabackup_20160517010001
最后出现如下,表示成功完成
innobackupex: Starting to copy InnoDB log files
innobackupex: in '/apps/backup/xtrabackup_20160517010001'
innobackupex: back to original InnoDB log directory '/apps/dbdat/mariadb10_data3306'
innobackupex: Copying '/apps/backup/xtrabackup_20160517010001/ib_logfile3' to '/apps/dbdat/mariadb10_data3306/ib_logfile3'
innobackupex: Copying '/apps/backup/xtrabackup_20160517010001/ib_logfile0' to '/apps/dbdat/mariadb10_data3306/ib_logfile0'
innobackupex: Copying '/apps/backup/xtrabackup_20160517010001/ib_logfile2' to '/apps/dbdat/mariadb10_data3306/ib_logfile2'
innobackupex: Copying '/apps/backup/xtrabackup_20160517010001/ib_logfile1' to '/apps/dbdat/mariadb10_data3306/ib_logfile1'
innobackupex: Finished copying back files.

160518 11:32:32  innobackupex: completed OK!
五.启动mysql
/apps/svr/mariadb10/bin/mysqld_safe --defaults-file=/apps/dbdat/mariadb10_data3306/my3306.cnf  --socket=/tmp/mysql3306.sock &
登入mysql,并更改root密码:
SET PASSWORD FOR root@localhost = password('safe2016')

六.用binlog文件增量恢复到2016年5月18日0点
我们查看备份集中的xtrabackup_info和xtrabackup_binlog_info文件
cat xtrabackup_info
start_time = 2016-05-17 01:00:01
end_time = 2016-05-17 01:11:55
lock_time = 2
binlog_pos = filename 'mysql-bin.000271', position 291755824, GTID of the last change '0-19873306-348307239'

cat xtrabackup_binlog_info
mysql-bin.000271        291755824       0-19873306-348307239

从上面看到,备份开始时间为2016-05-17 01:00:01,完成时间为2016-05-17 01:11:55,对应的的binlog为mysql-bin.000271,position为291755824。

正常执行完xtrabckup的全备恢复后,对以下binlog文件进行增量恢复,binlog文件如下:
[apps@mvxl0784 backup]$ ls -ltr
total 3751088
-rw-r----- 1 apps apps  536871119 May 18 09:37 mysql-bin.000271
-rw-r----- 1 apps apps  536871210 May 18 09:37 mysql-bin.000272
drwxr-xr-x 8 apps apps       4096 May 18 10:03 xtrabackup_20160517010001

[apps@mvxl0784 backup]$ time /apps/svr/mariadb10/bin/mysqlbinlog mysql-bin.000271 --start-position=291755824|/apps/svr/mariadb10/bin/mysql -u root -psafe2016 --socket=/tmp/mysql3306.sock

real    3m15.437s
user    0m28.510s
sys     0m18.515s
[apps@mvxl0784 backup]$ time /apps/svr/mariadb10/bin/mysqlbinlog mysql-bin.000272 --stop-datetime='2016-05-18 00:00:00'|/apps/svr/mariadb10/bin/mysql -u root -psafe2016 --socket=/tmp/mysql3306.sock

real    7m2.584s
user    1m40.549s
sys     1m8.097s

七.恢复过程中遇到的问题分享

innobackupex恢复操作解压备份文件报错一例:http://blog.csdn.net/zengxuewen2045/article/details/51446789

innobackupex恢复操作报错两例:http://blog.csdn.net/zengxuewen2045/article/details/51446770

记一次xtrabackup工具恢复后mysqld_safe启动问题:http://blog.csdn.net/zengxuewen2045/article/details/51446754

利用xtrabackup和binlog增量恢复时提示表记录不存在案例:http://blog.csdn.net/zengxuewen2045/article/details/51454290