mysql xtrabackup online backup and restore (full backup + additional backup)

工具安装:
[root@mysqlserver var]# tar -xvf Percona-XtraBackup-2.3.4-re80c779-el6-x86_64-bundle.tar   --解压包
percona-xtrabackup-2.3.4-1.el6.x86_64.rpm
percona-xtrabackup-debuginfo-2.3.4-1.el6.x86_64.rpm
percona-xtrabackup-test-2.3.4-1.el6.x86_64.rpm

[root@mysqlserver var]# rpm -ivh percona-xtrabackup-2.3.4-1.el6.x86_64.rpm
warning: percona-xtrabackup-2.3.4-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
error: Failed dependencies:
libev.so.4()(64bit) is needed by percona-xtrabackup-2.3.4-1.el6.x86_64
perl(DBD::mysql) is needed by percona-xtrabackup-2.3.4-1.el6.x86_64


http://rpmfind.net/linux/rpm2html/search.php?query=libev.so.4()(64bit)  --下载libev.so

[root@mysqlserver var]# rpm -ivh libev-4.04-2.el6.x86_64.rpm
warning: libev-4.04-2.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 66534c2b: NOKEY
Preparing...                ########################################### [100%]
   1:libev                  ########################################### [100%]

[root@mysqlserver var]# yum list |grep  perl-DBD
perl-DBD-MySQL.x86_64                  4.013-3.el6                      base   
perl-DBD-Pg.x86_64                     2.15.1-4.el6_3                   base   
perl-DBD-SQLite.x86_64                 1.27-3.el6                       base  

[root@mysqlserver var]# yum install perl-DBD-MySQL.x86_64

[root@mysqlserver var]# rpm -ivh percona-xtrabackup-2.3.4-1.el6.x86_64.rpm --- Installation succeeded!
warning: percona-xtrabackup-2.3.4-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing... ################ ########################### [100%]
   1:percona-xtrabackup ############## ############################# [100%]


Basic idea of ​​backup and restoration:
1. Full
  xtrabackup --defaults-file=/ etc/my.cnf --backup --target-dir=/backup/xtrabackup/full

2. Incremental backup 1 
  xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/backup /xtrabackup/full/ --incremental-dir=/backup/xtrabackup/increm1

3. Incremental backup 2 
  xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/backup/xtrabackup/full/ --incremental-dir=/backup/xtrabackup/increm2

restore -- only need to restore the last full backup and the last incremental backup.
 
  The other is to back up the data between incremental backups
  1--2 xtrabackup --defauls-file=/etc/my.cnf --backup --target-dir=/backup/xtrabackup/increm2 --incremental-basedir =/backup/xtrabackup/increm1
  
4. More incremental backups

5. Application log 0 
  xtrabackup --defaults-file=/etc/my.cnf --apply-log --redo-only --target-dir=/backup /xtrabackup/full

6. Application log 1
  xtrabackup --defaults-file=/etc/my.cnf --apply-log --redo-only --target-dir=/backup/xtrabackup/full --incremental-dir= /backup/xtrabackup/increm1
 
7. Application log 2
  xtrabackup --defaults-file=/etc/my.cnf --apply-log --target-dir=/backup/xtrabackup/full --incremental-dir=/backup/xtrabackup/increm2

8. Copy the data back
  to xtrabackup - -defaults-file=/etc/my.cnf --copy-back --target-dir=/backup/xtrabackup/full

 
Description:
1) Incremental backup is only for engines that support transactions such as InnoDB. For engines such as MyISAM, is still fully prepared.
2) Backup using this method will include flush tables operation. If there is a long query or table deadlock in production, it may cause the backup to be unable to end for a long time. Pay attention to observe


--------------- ------ Actual test by myself -------------
os: centos7.0
mysql: 5.7.10
xtrabackup: percona-xtrabackup-24-2.4.1-1.el6.x86_64. rpm

1. The first full backup
At this point , there are 5 tables in the database: t1 t2 milestat miletest t_timetest library name testdb
innobackupex --defaults-file=/etc/my.cnf --user='root' --password=' root'/mysql_bak/full

A timestamp folder was generated: /mysql_bak/full/2016-04-08_17-20-10
total 12340
-rw-r-----. 1 root root 412 Apr 8 17:20 backup-my.cnf
-rw -r-----. 1 root root 361 Apr 8 17:20 ib_buffer_pool
-rw-r-----. 1 root root 12582912 Apr 8 17:20 ibdata1
drwxr-x---. 2 root root 4096 Apr 8 17:20 mysql
drwxr-x---. 2 root root 8192 Apr 8 17:20 performance_schema
drwxr-x---. 2 root root 8192 Apr 8 17:20 sys
drwxr-x---. 2 root root 4096 Apr 8 17:20 testdb
-rw-r-----. 1 root root 113 Apr 8 17:20 xtrabackup_checkpoints
-rw-r-----. 1 root root 452 Apr 8 17:20 xtrabackup_info
-rw-r -----. 1 root root 2560 Apr 8 17:20 xtrabackup_logfile

[root@kafka1 2016-04-08_17-20-10]# cat xtrabackup_checkpoints ---records the start and end position of backup
backup_type = full-backuped --- backup type
from_lsn = 0 -- backup start point
to_lsn = 2520447 ---Backup end point
last_lsn = 2520456
compact = 0
recover_binlog_info = 0

2. After doing some operations, add
drop table t1 for the first time;
drop table t2;

innobackupex --defaults-file=/etc/my.cnf -- user='root' --password='root' --incremental
--incremental-basedir=/mysql_bak/full/2016-04-08_17-20-10 /mysql_bak/zengliang1

cd /mysql_bak/zengliang1/2016-04-08_17 -31-18/ -- also generates a timestamp directory
total 12340
-rw-r-----. 1 root root 412 Apr 8 17:28 backup-my.cnf
-rw-r-----. 1 root root 361 Apr 8 17:28 ib_buffer_pool
-rw-r-----. 1 root root 12582912 Apr 8 17:28 ibdata1
drwxr-x---. 2 root root 4096 Apr 8 17:28 mysql
drwxr-x---. 2 root root 8192 Apr 8 17:28 performance_schema
drwxr-x---. 2 root root 8192 Apr 8 17:28 sys
drwxr-x---. 2 root root 4096 Apr 8 17:28 testdb
-rw-r-----. 1 root root 113 Apr 8 17:28 xtrabackup_checkpoints
-rw-r-----. 1 root root 526 Apr 8 17:28 xtrabackup_info
-rw-r-----. 1 root root 2560 Apr 8 17:28 xtrabackup_logfile
[root @kafka1 2016-04-08_17-31-18]# cat xtrabackup_checkpoints
backup_type = incremental -- backup type
from_lsn = 2520447 -- backup start point (aligned with full backup)
to_lsn = 2528700 -- backup end point
last_lsn = 2528709
compact = 0
recover_binlog_info = 0

3. After doing some operations,
create table t3(id int primary key,name varchar(10)) engine=innodb;
create table t4(id int primary key,name varchar(10 ) )) engine=innodb;

The content between the first and second additions is backed up:
innobackupex --defaults-file=/etc/my.cnf --user='root' --password=' root' --incremental
--incremental-basedir=/mysql_bak/zengliang1/2016-04-08_17-31-18 /mysql_bak/zengliang2

cat /mysql_bak/zengliang2/2016-04-08_17-37-53/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2528700 -- the start point of the backup (matches the end point of the first addition)
to_lsn = 2536798 -- the end point of the backup
last_lsn = 2536807
compact = 0
recover_binlog_info = 0


You can also directly back up the content up to this point:
innobackupex --defaults-file=/etc/my.cnf  --user='root' --password='root' --incremental
--incremental-basedir=/mysql_bak/full/2016-04-08_17-20-10  /mysql_bak/zengliang2

4.插入一些数据,第三次增备
mysql> insert into t3 values(1,'haha'),(2,'enen');
Query OK, 2 rows affected (0.09 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+----+------+
| id | name |
+----+------+
|  1 | haha |
|  2 | enen |
+----+------+
2 rows in set (0.00 sec)

innobackupex --defaults-file=/etc/my.cnf  --user='root' --password='root' --incremental --incremental-basedir=/mysql_bak/zengliang2/2016-04-08_17-37-53  /mysql_bak/zengliang3

[root@kafka1 zengliang3]# cd 2016-04-11_10-26-18/
[root@kafka1 2016-04-11_10-26-18]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2536798 --- Backup start point (second End point of secondary backup)
to_lsn = 2540333 -- backup end point
last_lsn = 2540342
compact = 0
recover_binlog_info = 0

Finally insert two pieces of data:
mysql> insert into t3 values(3,'test'),(4,'gei' ); ---No backup
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0


5. Prepare to restore:
Because it is restored on this machine, some directories need to be removed.

Stop the service: service mysql57 stop
Move the mv data data_old /usr/local/mysql under the
         mkdir data

application full log:
innobackupex --defaults-file=/etc/my.cnf --user='root' --password='root' --apply-log --redo-only /mysql_bak/full/2016-04-08_17-20- 10Apply

the first additional log:
innobackupex --defaults-file=/etc/my.cnf --user='root' --password='root' --apply-log --redo-only /mysql_bak/full /2016-04-08_17-20-10 --incremental-dir=/mysql_bak/zengliang1/2016-04-08_17-31-18

restore data:
innobackupex --defaults-file=/etc/my.cnf --user= 'root' --password='root' --copy-back /mysql_bak/full/2016-04-08_17-20-10

or copy everything in the full directory to the data directory cp -r * /usr/local/mysql/data

Modify permissions: chown -R mysql:mysql data

to start the database, restore full backup + first additional backup:
[root@kafka1 mysql]# service mysql57 start
Starting MySQL.. SUCCESS!
[root@kafka1 mysql]# /usr/local/mysql/bin/mysql -uroot -p
mysql> use testdb
mysql> show tables;
+-----------------+
| Tables_in_testdb |
+----------------- -+
| milestat |
| miletest |
| t_timetest |
+------------------+
5 rows in set (0.00 sec) -- delete t1 and t2 tables


above is once Sex is done, but we may follow up with daily supplements, and use subsequent supplements to restore on the original basis.

Log application for the second additional backup:
innobackupex --defaults-file=/etc/my.cnf --user='root' --password='root' --apply-log --redo-only /mysql_bak/full /2016-04-08_17-20-10 --incremental-dir=/mysql_bak/zengliang2/2016-04-08_17-37-53

Log application for the third addition:
innobackupex --defaults-file=/etc/my .cnf --user='root' --password='root'
The last application log, and rollback for crash recovery process (without --redo-only option):

stop service: service mysql57 stop

data recovery: mv data data_old2
         mkdir data 
         chown mysql:mysql data
innobackupex --defaults-file=/ etc/my.cnf --user='root' --password='root' --copy-back /mysql_bak/full/2016-04-08_17-20-10

Modify permissions: chown -R mysql:mysql data

start the service : service mysql57 start
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------- ---------+
| Tables_in_testdb |
+-------------------+
| milestat |
| miletest |
| t3 |
| t4 |
| t_timetest |
+-----------------+ --- all logs have been applied, t1, t2 deleted, t3.t4 created, t3 Two records are inserted.
5 rows in set (0.00 sec)

mysql> select * from t3;
+----+------+
| id | name |
+----+------+
| 1 | haha |
| 2 | enen |
+----+------+
2 rows in set (0.01 sec) 


You can use this tool to perform full backup + additional backup + binlog daily, and perform incomplete backup and recovery of mysql library. Since the binlog log was not activated during the test, there is no subsequent test, but it is relatively simple to use the mysqlbinlog command to restore. You can refer to the following:

-- Get the log position after the increment 
SHELL> cd /hotbak/inc/ 
SHELL> more xtrabackup_binlog_info 
inst3606bin.000001 774 --Use 
 
stop-datetime to append the log to the specified time
mysqlbinlog /data/inst3606/log/bin/inst3606bin.000001 --start-position=774 --stop-datetime="2014-12-25 11:53:54" \ 
> |mysql -urobin -pxxx -P3606 -S /tmp/mysql3606.sock 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993770&siteId=291194637