Xtrabackup
Xtrabackup是由percona开发的一个开源软件,它是innodb热备工具ibbackup(收费的商业软件)的一个开源替代品。Xtrabackup由个部分组成:xtrabackup和innobackupex,其中xtrabackup工具用于备份innodb和 xtraDB引擎的表;而innobackupex工具用于备份myisam和innodb引擎的表,本文将介绍如何用innobackupex工具做全量和增量备份。
如果你备份的数据库的引擎为Innodb那么可以支持热备与增量备份。如果你备份的数据库引擎有MYISAM的,那么不支持热备也不支持真正的增量备份,而是温备,所谓的热备也是完全备份。
innobackupex –defaults-file=/opt/mysql/my.cnf –user=root –password=*** –use-memory=4G –apply-log /backup/mysql/data/2013-10-29_09-05-25
innobackupex –defaults-file=/opt/mysql/my.cnf –user=root –password=*** –copy-back /backup/mysql/data/2013-10-29_09-05-25
下载:
https://www.percona.com/downloads/XtraBackup/LATEST/
# yum -y install percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm
应用已提交事务,回滚未提交事务,有增量备份不能回滚,因为一会儿还要合并增量,再结合二进制日志文件还原到最近时间点。
MariaDB [biplog_00]> SET GLOBAL innodb_import_table_from_xtrabackup = 1;
一、完全备份
MariaDB [house50]> SHOW GLOBaL VARIABLES LIKE ‘innodb_file_per%’;
+———————–+——-+
| Variable_name | Value |
+———————–+——-+
| innodb_file_per_table | ON |
+———————–+——-+
1 row in set (0.00 sec)
MariaDB [newhouse_00]> SHOW TABLE STATUS;
*************************** 1. row ***************************
Name: appointment
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 10
Avg_row_length: 1638
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-08-17 10:51:01
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
CREATE USER ‘back_name’@’localhost’ IDENTIFIED BY ‘backup’;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ‘back_name’;
grant RELOAD,LOCK TABLES,REPLICATION CLIENT,PROCESS,SUPER,CREATE,INSERT,SELECT on *.* to ‘back_name’@’localhost’;
FLUSH PRIVILEGES;
# innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=back_name –password=backup /backups/
innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=back_name –password=backup /backups/
#################################### innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=back_name –password=backup –databases=newhouse_00 /backups/
出现:
170830 09:06:59 Backup created in directory ‘/backups/2017-08-30_09-06-58/’
MySQL binlog position: filename ‘mysql-bin.000026’, position ‘4801’
170830 09:06:59 [00] Writing /backups/2017-08-30_09-06-58/backup-my.cnf
170830 09:06:59 [00] …done
170830 09:06:59 [00] Writing /backups/2017-08-30_09-06-58/xtrabackup_info
170830 09:06:59 [00] …done
xtrabackup: Transaction log of lsn (549568072) to (549568072) was copied.
170830 09:06:59 completed OK!
# tar -zcf db.tar.gz 2017-08-30_10-16-32/
使用innobakupex备份时,其会调用xtrabackup备份所有的InnoDB表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件。这些文件会被保存至一个以时间命名的目录中。
在备份的同时,innobackupex还会在备份目录中创建如下文件:
(1)xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
每个InnoDB页(通常为16k大小)都会包含一个日志序列号,即LSN。LSN是整个数据库系统的系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的。
(2)xtrabackup_binlog_info —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置。
(3)xtrabackup_binlog_pos_innodb —— 二进制日志文件及用于InnoDB或XtraDB表的二进制日志文件的当前position。
(4)xtrabackup_binary —— 备份中用到的xtrabackup的可执行文件;
(5)backup-my.cnf —— 备份命令用到的配置选项信息;
在使用innobackupex进行备份时,还可以使用–no-timestamp选项来阻止命令自动创建一个以时间命名的目录;如此一来,innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据。
二、准备(prepare)一个完全备份
在新数据库上创建一个账号:
CREATE USER ‘back_name’@’localhost’ IDENTIFIED BY ‘backup’;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ‘back_name’;
grant RELOAD,LOCK TABLES,REPLICATION CLIENT,PROCESS,SUPER,CREATE,INSERT,SELECT on *.* to ‘back_name’@’localhost’;
FLUSH PRIVILEGES;
# tar -zxf db.tar.gz
# innobackupex –apply-log /root/2017-08-30_10-16-32/
正常执行出现:
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 550917962
170930 09:50:29 completed OK!
三、从一个完全备份中恢复数据
–port=PORT
–password=WORD
–user=WORD
# innobackupex –copy-back /root/2017-08-30_10-16-32/ –port=3507 –user=back_name –password=backup
At the end of a successful copy-back run innobackupex
prints “completed OK!”.
复制原数据库配置文件:
log_error = /data/mysql/mysql-error.log #错误日志路径
slow_query_log_file = /data/log/mysql-slow.log
datadir = /data
touch: cannot touch ‘/data/log/mysql-slow.log’: No such file or directory
# mkdir /data/log
# touch /data/log/mysql-slow.log
# chown mariadb:mariadb -R /data/
# rm -rf /data/ib_logfile
#######注意几个日志文件
四、使用xtrabackup增量完全备份数据库并还原
1、创建账户
MariaDB [(none)]> CREATE USER ‘incre’@’localhost’ IDENTIFIED BY ‘increpass’;
MariaDB [(none)]> REVOKE ALL PRIVILEGES, GRANT OPTION FROM ‘incre’@’localhost’;
MariaDB [(none)]> grant RELOAD,LOCK TABLES,REPLICATION CLIENT,PROCESS,SUPER,CREATE,INSERT,SELECT on *.* to ‘incre’@’localhost’;
MariaDB [(none)]> FLUSH PRIVILEGES;
2、创建一个完全备份
innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=incre –password=increpass /backups/ –use-memory=4G
# cat 2017-08-30_16-40-38/xtrabackup_info
binlog_pos = filename ‘mysql-bin.000002’, position ‘245’
innodb_from_lsn = 0
innodb_to_lsn = 549592206
3、滚动日志并验证
MariaDB [(none)]> SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000003 | 245 | | |
+——————+———-+————–+——————+
# mysqladmin flush-logs -uroot -p
MariaDB [(none)]> SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000004 | 245 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
4、执行增量备份
# innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=incre –password=increpass –use-memory=4G –incremental /backups –incremental-basedir=/backups/2017-08-30_16-40-38/
# cat 2017-08-30_17-10-00/xtrabackup_info
binlog_pos = filename ‘mysql-bin.000004’, position ‘245’
innodb_from_lsn = 549592206
innodb_to_lsn = 549649800
5、异地主机还原备份
[root@localhost ~]# mkdir -pv /mariadb/data
mkdir: created directory ‘/mariadb’
mkdir: created directory ‘/mariadb/data’
[root@localhost ~]# mkdir -pv /mariadb/log
mkdir: created directory ‘/mariadb/log’
[root@localhost backups]# chown -R mysql:mysql /mariadb
# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
datadir = /mariadb/data
log_error = /mariadb/log/mysql-error.log #错误日志路径
log-bin = /mariadb/log/logbin
slow_query_log_file = /mariadb/log/mysql-slow.log
innodb_force_recovery = 1
slow_query_log = 1
long_query_time = 1 #慢
skip_name_resolve = ON
max_connect_errors = 6000
open_files_limit = 65535
lower_case_table_names = 1
innodb_file_per_table = 1
innodb_import_table_from_xtrabackup = 1
#foreign_key_checks = 0
#foreign_key_checks = no
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
6、prepare
其中,BASEDIR指的是完全备份所在的目录,此命令执行结束后,innobackupex命令会在/backup目录中创建一个新的以时间命名的目录以存放所有的增量备份数据。另外,在执行过增量备份之后再一次进行增量备份时,其–incremental-basedir应该指向上一次的增量备份所在的目录。
需要注意的是,增量备份仅能应用于InnoDB或XtraDB表,对于MyISAM表而言,执行增量备份时其实进行的是完全备份。
“准备”(prepare)增量备份与整理完全备份有着一些不同,尤其要注意的是:
(1)需要在每个备份(包括完全和各个增量备份)上,将已经提交的事务进行“重放”。“重放”之后,所有的备份数据将合并到完全备份上。
(2)基于所有的备份将未提交的事务进行“回滚”。
于是,操作就变成了:
# innobackupex –apply-log –redo-only BASE-DIR
接着执行:
# innobackupex –apply-log –redo-only BASE-DIR –incremental-dir=INCREMENTAL-DIR-1
而后是第二个增量:
# innobackupex –apply-log –redo-only BASE-DIR –incremental-dir=INCREMENTAL-DIR-2
其中BASE-DIR指的是完全备份所在的目录,而INCREMENTAL-DIR-1指的是第一次增量备份的目录,INCREMENTAL-DIR-2指的是第二次增量备份的目录,其它依次类推,即如果有多次增量备份,每一次都要执行如上操作;
[root@localhost backups]# innobackupex –apply-log –redo-only /root/backups/2017-08-30_16-40-38/
[root@localhost backups]# innobackupex –apply-log –redo-only /root/backups/2017-08-30_16-40-38/ –incremental-dir=/root/backups/2017-08-30_17-10-00/
[root@localhost backups]# innobackupex –copy-back –use-memory=4G /root/backups/2017-08-30_16-40-38/
7、二进制日志回滚到最新
# tar xf mysql-bin.000004.tar.gz
position ‘245’
[root@localhost ~]# setenforce 0
[root@localhost backups]# mysqlbinlog –start-position=245 mysql-bin.000004 > binlog.sql
[root@localhost backups]# systemctl start mariadb
MariaDB [biplog_00]> source /root/backups/binlog.sql
五、备份单个数据表并还原
foreign_key_checks = ON
innodb_force_recovery = 0
改:innodb_expand_import=1(大于5.5.10-20.1版本)或innodb_import_table_from_xtrabackup=1(小于5.5.10-20.1版本)选项
innodb_file_per_table = 1
1、进入数据库drop掉一个table
MariaDB [order_00]> select * from cs_finance_customer;
+———————-+———–+———————————-+——————–+————-+—————+————–+——————-+———————+———————+———–+——————+————————–+
| ID | CLIENT_NO | PARTY_ID | IDENTITY_CARD | PRODUCT_ID | LOAN_ACCT_NO | TOTAL_AMOUNT | AMOUNT_VALID_DATE | CREATE_TIME | UPDATE_TIME | STATUS_ID | APPLICATION_ID | WITHDRAW_CASH_PASSWORD |
+———————-+———–+———————————-+——————–+————-+—————+————–+——————-+———————+———————+———–+——————+————————–+
| 00000000000000000002 | 028 | 10201707161518058890000000000001 | 500232199401060217 | A1001001001 | NULL | NULL | NULL | 2017-07-16 03:18:06 | 2017-07-16 03:35:18 | NULL | BO20170716000001 | NULL |
| 00000000000000000031 | 028 | 10201707171131324800000000000002 | 320523197309040018 | A1001001001 | 6010019149248 | 11000.00 | 20190505 | 2017-07-16 23:31:32 | 2017-07-26 02:12:18 | NULL | BO20170717000003 | NULL |
| 00000000000000000040 | 028 | 10201707171358441900000000000003 | 620502198510031686 | A1001001001 | NULL | NULL | NULL | 2017-07-17 01:58:44 | 2017-07-17 01:58:45 | NULL | BO20170717000005 | NULL |
| 00000000000000000088 | 028 | 10201707171452518310000000000004 | 430527199309204211 | A1001001001 | NULL | NULL | NULL | 2017-07-17 02:52:52 | 2017-07-17 02:52:53 | NULL | BO20170717000006 | NULL |
| 00000000000000000319 | 028 | 10201707251621182380000000000005 | 211322198209145296 | A1001001001 | NULL | NULL | NULL | 2017-07-25 04:21:18 | 2017-07-25 04:21:20 | NULL | BO20170725000081 | NULL |
| 00000000000000000911 | 028 | 10201708091144269030000000000006 | 530381198912240598 | A1001001001 | NULL | NULL | NULL | 2017-08-08 23:44:27 | 2017-08-09 03:20:29 | NULL | BO20170809000012 | 4QrcOUm6Wau+VuBX8g+IPg== |
| 00000000000000000955 | 028 | 10201708091303225210000000000007 | NULL | A1001001001 | NULL | NULL | NULL | 2017-08-09 01:03:23 | NULL | NULL | NULL | NULL |
+———————-+———–+———————————-+——————–+————-+—————+————–+——————-+———————+———————+———–+——————+————————–+
7 rows in set (0.00 sec)
MariaDB [order_00]> DROP TABLES cs_finance_customer;
Query OK, 0 rows affected (0.01 sec)
2、导出数据库文件:innobackupex –apply-log –export /pat/to/partial/backup
[root@localhost backups]# innobackupex –apply-log –export /root/backups/2017-08-30_16-40-38/
3、手动导出tables创建文件
CREATE TABLE `cs_finance_customer` (
`ID` varchar(20) NOT NULL DEFAULT ”,
`CLIENT_NO` varchar(23) DEFAULT NULL,
`PARTY_ID` varchar(50) DEFAULT NULL,
`IDENTITY_CARD` varchar(20) DEFAULT NULL,
`PRODUCT_ID` varchar(23) DEFAULT NULL,
`LOAN_ACCT_NO` varchar(23) DEFAULT NULL,
`TOTAL_AMOUNT` decimal(25,2) DEFAULT NULL,
`AMOUNT_VALID_DATE` varchar(20) DEFAULT NULL,
`CREATE_TIME` timestamp NULL DEFAULT NULL,
`UPDATE_TIME` timestamp NULL DEFAULT NULL,
`STATUS_ID` varchar(40) DEFAULT NULL,
`APPLICATION_ID` varchar(40) DEFAULT NULL,
`WITHDRAW_CASH_PASSWORD` varchar(60) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `finance_customer_partyId_idCard` (`PARTY_ID`,`IDENTITY_CARD`),
KEY `finance_customer_idCard_partyId` (`IDENTITY_CARD`,`PARTY_ID`),
KEY `partyid` (`PARTY_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4、恢复数据
MariaDB [order_00]> ALTER TABLE cs_finance_customer DISCARD TABLESPACE;
Query OK, 0 rows affected (0.01 sec)
[root@localhost order_00]# ll cs_finance_*
-rw-rw—-. 1 mysql mysql 9116 Aug 31 03:06 cs_finance_customer.frm
[root@localhost order_00]# cp /root/backups/2017-08-30_16-40-38/order_00/cs_finance_customer.exp /mariadb/data/order_00/
[root@localhost order_00]# cp /root/backups/2017-08-30_16-40-38/order_00/cs_finance_customer.ibd /mariadb/data/order_00/
MariaDB [order_00]> ALTER TABLE cs_finance_customer IMPORT TABLESPACE;
ERROR 1030 (HY000): Got error -1 from storage engine
ALTER TABLE cs_finance_customer IMPORT TABLESPACE;
[root@localhost backups]# chown -R mysql:mysql /mariadb
MariaDB [order_00]> ALTER TABLE cs_finance_customer DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)
MariaDB [order_00]> FLUSH TABLES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [order_00]> ALTER TABLE cs_finance_customer IMPORT TABLESPACE;
Query OK, 0 rows affected (0.00 sec)
恢复成功!!!!!!!!
六、备份单个数据库
1、创建单个数据库备份
[root@whyt2 ~]# innobackupex –defaults-file=/etc/my.cnf -H localhost -P 3507 –user=incre –password=increpass –databases=house50 /backups/ –use-memory=4G
[root@whyt2 backups]# tar -zcf da50.tar.gz 2017-08-31_16-13-11/
[root@localhost ~]# tar -zxf da50.tar.gz
[root@localhost house50]# innobackupex –apply-log /root/2017-08-31_16-13-11/
[root@localhost house50]# innobackupex –apply-log –export /root/2017-08-31_16-13-11/
[root@localhost 2017-08-31_16-13-11]# mv house50 /mariadb/data/
[root@localhost 2017-08-31_16-13-11]# chown -R mysql:mysql /mariadb/
[root@localhost 2017-08-31_16-13-11]# systemctl restart mariadb
OTHERSERVER|mysql> CREATE TABLE mytable (…) ENGINE=InnoDB;
then discard its tablespace:
OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable DISCARD TABLESPACE;
After this, copy mytable.ibd and mytable.exp ( or mytable.cfg if importing to MySQL 5.6) files to database’s home, and import its tablespace:
OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable IMPORT TABLESPACE;
后面同导入单表类似。
[root@localhost ~]# innobackupex –copy-back –use-memory=4G /root/backups/2017-08-30_16-40-38/
[root@localhost ~]# setenforce 0
[root@localhost backups]# mysqlbinlog –start-position=245 mysql-bin.000004 > binlog.sql
MariaDB [biplog_00]> source /root/backups/binlog.sql
my.cnf
##############################################################
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
datadir = /mariadb/data
log_error = /mariadb/log/mysql-error.log #错误日志路径
log-bin = /mariadb/log/logbin
slow_query_log_file = /mariadb/log/mysql-slow.log
slow_query_log = 1
long_query_time = 1 #慢
innodb_force_recovery = 0
skip_name_resolve = ON
max_connect_errors = 6000
open_files_limit = 65535
lower_case_table_names = 1
innodb_file_per_table = 1
innodb_import_table_from_xtrabackup = 1
#foreign_key_checks = 0
#foreign_key_checks = no
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
##################################################################
innodb_expand_import