本文是参考几个博客整理加上自己遇到的问题后总结形成。参考文链接在文章底部。再此感谢~
XtraBackup介绍和安装请参考我的《Percona XtraBackup安装总结》。
- 安装完成后会有几个工具,说明如下:
innobackupex:这个是其实是下面三个工具的一个perl脚本封装,可以备份MyISAM, InnoDB, XtraDB表。但在处理Myisam时需要加一个读锁。
xtrabackup:一个由C编译而来的二进制文件,只能备份InnoDB和XtraDB数据。
xbcrypt:用来加密或解密备份的数据。
xbstream:用来解压或压缩xbstream格式的压缩文件。
-
innobackupex :
xtrabackup命令只备份数据文件,并不备份数据表结构(.frm),所以使用xtrabackup恢复的时候必须有对应表结构文件(.frm)。用innobackupex命令,此命令相当于冷备份,复制数据目录的索引,数据,结构文件,但会有短暂的锁表(时间依赖于MyISAM大小)。
参数:
--defaults-file:指定my.cnf参数文件的位置[此配置文件里必须指定datadir]
--apply-log:同xtrabackup的--prepare参数,一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据 文件仍处理不一致状态。--apply-log的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态。
--copy-back:做数据恢复时将备份数据文件拷贝到MySQL服务器的datadir
--remote-host=HOSTNAME: 通过ssh将备份数据存储到进程服务器上
--stream=[tar]:备份文件输出格式, 该文件可在XtarBackup binary文件中获得. 在使用参数stream=tar备份的时候,你的xtrabackup_logfile可能会临时放在/tmp目录下,如果你备份的时候并发写入较大的话,xtrabackup_logfile可能会很大(5G+),很可能会撑满你的/tmp目录,可以通过参数--tmpdir指定目录来解决这个问题.
--tmpdir=DIRECTORY:当有指定--remote-host or --stream时, 事务日志临时存储的目录, 默认采用MySQL配置文件中所指定的临时目录tmpdir
--redo-only --apply-log:强制备份日志时只redo,跳过rollback,这在做增量备份时非常必要
--use-memory=*:该参数在prepare的时候使用,控制prepare时innodb实例使用的内存
--databases=LIST:列出需要备份的databases,如果没有指定该参数,所有包含MyISAM和InnoDB表的database都会被备份
--slave-info:备份从库, 加上--slave-info备份目录下会多生成一个xtrabackup_slave_info 文件, 这里会保存主日志文件以及偏移, 文件内容类似于:CHANGE MASTER TO MASTER_LOG_FILE='', MASTER_LOG_POS=0
--socket=SOCKET:指定mysql.sock所在位置,以便备份进程登录mysql.
全备
初始化数据库:
mysql> create database xtra_test default charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use xtra_test
Database changed
mysql> create table M(id int,name varchar(10))engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> create table I(id int,name varchar(10))engine=innodb;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into M values(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> insert into I values(11,'A'),(22,'B'),(33,'C'),(44,'D'),(55,'E');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from M;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
+------+------+
rows in set (0.00 sec)
mysql> select * from I;
+------+------+
| id | name |
+------+------+
| 11 | A |
| 22 | B |
| 33 | C |
| 44 | D |
| 55 | E |
+------+------+
rows in set (0.00 sec)
测试备份
root@ha1:~# innobackupex --user=root --password=123456 --host=127.0.0.1 --defaults-file=/etc/my.cnf --databases=xtra_test /home/lgc/xtrabackup/
InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved.
This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p
11:56:58 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/mysql/my.cnf;mysql_read_default_group=xtrabackup;host=192.168.200.51' as 'zjy' (using password: YES).
11:56:58 innobackupex: Connected to MySQL server
11:56:58 innobackupex: Executing a version check against the server...
11:56:58 innobackupex: Done.
11:56:58 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
innobackupex: Using server version 5.5.38-0ubuntu0.12.04.1-log
innobackupex: Created backup directory /home/zhoujy/xtrabackup/2014-11-11_11-56-58
11:56:58 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/etc/mysql/my.cnf" --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58 --innodb_log_file_size="5242880" --innodb_data_file_path="ibdata1:10M:autoextend" --tmpdir=/tmp --extra-lsndir='/tmp' --databases='xtra_test'
innobackupex: Waiting for ibbackup (pid=10539) to suspend
innobackupex: Suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2'
xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: )
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 5242880
>> log scanned up to (7363097930)
xtrabackup: Generating a list of tablespaces
[01] Copying ./ibdata1 to /home/zhoujy/xtrabackup/2014-11-11_11-56-58/ibdata1
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
>> log scanned up to (7363097930)
[01] ...done
>> log scanned up to (7363097930)
xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2' with pid '10540'
11:57:11 innobackupex: Continuing after ibbackup has suspended
11:57:11 innobackupex: Executing FLUSH TABLES WITH READ LOCK...
11:57:11 innobackupex: All tables locked and flushed to disk
11:57:11 innobackupex: Starting to backup non-InnoDB tables and files
innobackupex: in subdirectories of '/var/lib/mysql/'
innobackupex: Backing up file '/var/lib/mysql//xtra_test/db.opt'
innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.MYI'
>> log scanned up to (7363097930)
innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.frm'
innobackupex: Backing up file '/var/lib/mysql//xtra_test/I.frm'
innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.MYD'
11:57:11 innobackupex: Finished backing up non-InnoDB tables and files
11:57:11 innobackupex: Executing FLUSH ENGINE LOGS...
11:57:11 innobackupex: Waiting for log copying to finish
xtrabackup: The latest check point (for incremental): '7363097930'
xtrabackup: Stopping log copying thread.
.>> log scanned up to (7363097930)
xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_log_copied' with pid '10540'
xtrabackup: Transaction log of lsn (7363097930) to (7363097930) was copied.
11:57:12 innobackupex: All tables unlocked
innobackupex: Backup created in directory '/home/zhoujy/xtrabackup/2014-11-11_11-56-58'
innobackupex: MySQL binlog position: filename 'mysql-bin51.000001', position 967
11:57:12 innobackupex: Connection to database server closed
11:57:12 innobackupex: completed OK!
查看备份文件:
ls -lh 2014-11-11_11-56-58/
total 1.2G
-rw-r--r-- 1 root root 188 11月 11 11:56 backup-my.cnf
-rw-r----- 1 root root 1.2G 11月 11 11:57 ibdata1
-rw-r--r-- 1 root root 25 11月 11 11:57 xtrabackup_binlog_info
-rw-r----- 1 root root 95 11月 11 11:57 xtrabackup_checkpoints
-rw-r--r-- 1 root root 666 11月 11 11:57 xtrabackup_info
-rw-r----- 1 root root 2.5K 11月 11 11:57 xtrabackup_logfile
drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test
/home/zhoujy/xtrabackup/ 备份存放的位置,备份会在该目录下生成一个按照时间命名的文件夹。用--no-timestamp参数可以指定到自己想要的备份文件夹,不受时间命名的文件夹限制。
利用 --apply-log的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态。
root@localhost:/home/lgc/xtrabackup# innobackupex --apply-log /home/lgc/xtrabackup/2014-11-11_11-56-58/
应用完之后再查看备份文件看是否有变化:
root@localhost:/home/lgc/xtrabackup# ls -lh 2014-11-11_11-56-58/
total 1.2G
-rw-r--r-- 1 root root 188 11月 11 11:56 backup-my.cnf
-rw-r----- 1 root root 1.2G 11月 11 12:02 ibdata1
-rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile0
-rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile1
-rw-r--r-- 1 root root 25 11月 11 11:57 xtrabackup_binlog_info
-rw-r--r-- 1 root root 38 11月 11 12:02 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 95 11月 11 12:02 xtrabackup_checkpoints
-rw-r--r-- 1 root root 666 11月 11 11:57 xtrabackup_info
-rw-r----- 1 root root 2.0M 11月 11 12:02 xtrabackup_logfile
drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test
看到redo log已经出现。xtrabackup_开头的几个文件记录了一些日志偏移量的信息和日志名和时间等信息。在xtrabackup_checkpoints中记录了备份的模式:
backup_type = full-backuped。
xtrabackup_binlog_info:记录当前最新的LOG Position
xtrabackup_binlog_pos_innodb:innodb log postion
xtrabackup_checkpoints: 存放备份的起始位置beginlsn和结束位置endlsn,增量备份需要这个lsn[增量备份可以在这里面看from和to两个值的变化
xtrabackup_info:备份的一些具体信息日志
增量备份
在做增量备份的时候需要做一次全量备份,只是针对所有库的备份:
root@localhost:/home/lgc/xtrabackup# innobackupex --apply-log /home/lgc/xtrabackup/2014-11-11_11-56-58/
操作数据库,新增数据或删除数据,保证测试有效性。
mysql> insert into X values('dxy');
Query OK, 1 row affected (0.01 sec)
mysql> insert into X values('浙江');
Query OK, 1 row affected (0.01 sec)
基于全量增备:
innobackupex --user=lgc --password=123456 --host=127.0.01 --defaults-file=/etc/my.cnf --incremental --incremental-basedir=/home/lgc/xtrabackup/2014-11-11_13-57-11/ /home/zhoujy/increment_data/
其中,--incremental指明是增量备份,--incremental-basedir指定上次完整备份或者增量备份文件的位置。这里的增量备份其实只针对的是InnoDB,对于MyISAM来说,还是完整备份。
基于增量的增备:
就是--incremental-basedir 指定增量备份文件的位置,此时如需还原,则须还原2次。
#还原全量备份:
root@localhost:/home/lgc# innobackupex --apply-log /home/lgc/xtrabackup/2014-11-11_13-57-11/
#第一次的增量备份应用日志,应用完日志后,将合并到全备上,恢复使用全备恢复
root@localhost:~# innobackupex --apply-log /home/lgc/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/lgc/increment_data/2014-11-11_14-14-56/
#第二次的增量备份应用日志,应用完日志后,将合并到全备上,恢复使用全备恢复
root@localhost:~# innobackupex --apply-log /home/lgc/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/lgc/increment_data/2014-11-11_14-25-36/
此时两次增量备份其实都合并到全备上了,恢复是只需要使用全备进行恢复就可以了
压缩备份:必须使用-i参数拆包解压
打包(Tar)备份
innobackupex --user=root--password=123456 --host=127.0.0.1 --defaults-file=/etc/my.cnf --databases=xtra_test --stream=tar /home/lgc/xtrabackup/ 1>/home/lgc/xtrabackup/xtra_test.tar
但有可能出错,(表示:使用tar格式远程增量备份时收到如下提示,即只支持xbstream),如下提示:
xtrabackup: error: streaming incremental backups are incompatible with the
'tar' streaming format. Use --stream=xbstream instead.
还原
增备和全备还原方法一致,只是增备需要还原多次。
1、删除数据库表
2、关闭数据库
3、开始恢复
a:innobackupex --defaults-file=/etc/my.cnf --copy-back /home/lgc/xtrabackup/2014-11-11_11-56-58/
执行上面的命令需要保证数据库目录是空的,否则会失败,因为目录里有binlog日志,要么移走,要么就用手动复制需要的文件。所以说日志不要和数据放在一起!
b:cp ib_logfile0 ib_logfile1 ibdata1 /var/lib/mysql/
cp -R xtra_test /var/lib/mysql/
注意:此步骤命令是把备份文件中的ib_logfile0、ib_logfile1 ibdata1、xtra_test文件cp到当前数据库中。
4、设置权限:
root@ha1:/var/lib/mysql# chown -R mysql.mysql ib*
root@ha1:/var/lib/mysql# chown -R mysql.mysql xtra_test/
5、开启数据库
6、查看数据是否恢复
参考资料:(Percona XtraBackup使用说明)https://blog.csdn.net/wfs1994/article/details/80396604#t3
参考资料:(Percona XtraBackup使用说明)http://www.cnblogs.com/zhoujinyi/p/4088866.html
参考资料:(自动备份脚本)https://www.cnblogs.com/jiangwenju/p/4919633.html