使用XtraBackup 备份MySQL数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用XtraBackup 备份MySQL数据库相关的知识,希望对你有一定的参考价值。
本次测试使用XtraBackup备份mysql数据库
版本:XtraBackup2.4.5+MySQL5.7.16
下载地址:https://www.percona.com/downloads/XtraBackup/
1、安装XtraBackup
本次为了方便,使用解压版本进行安装,直接解压就可以用了。
使用过程中可能会遇到缺少perl依赖包的问题,我的方法是直接操作把镜像包里的perl都安装了,yum install -y perl*
2、简介
XtraBackup主要包括两个备份工具xtrabackup和innobackupex。其中innobackupex对xtrabackup进行了封装。本次只介绍innobackupex备份MySQL
3、全量备的操作方法
1)创建备份
#创建存储备份数据的目录 [[email protected] ~]# mkdir /mysqlbackup #开始备份 [[email protected] ~]# innobackupex --user=root --password=123456 /mysqlbackup 161213 12:27:13 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!". 161213 12:27:13 version_check Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_group=xtrabackup‘ as ‘root‘ (using password: YES). Failed to connect to MySQL server: DBI connect(‘;mysql_read_default_group=xtrabackup‘,‘root‘,...) failed: Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2) at - line 1314. 161213 12:27:13 Connecting to MySQL server host: localhost, user: root, password: set, port: 0, socket: (null) Using server version 5.7.16 innobackupex version 2.4.5 based on MySQL server 5.7.13 Linux (x86_64) (revision id: e41c0be) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /usr/local/mysql/data 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:12M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 50331648 InnoDB: Number of pools: 1 161213 12:27:13 >> log scanned up to (2671643) xtrabackup: Generating a list of tablespaces InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0 161213 12:27:13 [01] Copying ./ibdata1 to /mysqlbackup/2016-12-13_12-27-13/ibdata1 161213 12:27:14 >> log scanned up to (2671643) 161213 12:27:15 [01] ...done 161213 12:27:15 >> log scanned up to (2671643) 161213 12:27:15 [01] Copying ./mysql/plugin.ibd to /mysqlbackup/2016-12-13_12-27-13/mysql/plugin.ibd 161213 12:27:15 [01] ...done ...... 161213 12:27:16 [01] Copying ./wl/zx.ibd to /mysqlbackup/2016-12-13_12-27-13/wl/zx.ibd 161213 12:27:16 [01] ...done 161213 12:27:16 >> log scanned up to (2671643) 161213 12:27:16 Executing FLUSH NO_WRITE_TO_BINLOG TABLES... 161213 12:27:16 Executing FLUSH TABLES WITH READ LOCK... 161213 12:27:16 Starting to backup non-InnoDB tables and files 161213 12:27:16 [01] Copying ./mysql/db.opt to /mysqlbackup/2016-12-13_12-27-13/mysql/db.opt 161213 12:27:16 [01] ...done ...... 161213 12:27:18 [01] Copying ./wl/wl.frm to /mysqlbackup/2016-12-13_12-27-13/wl/wl.frm 161213 12:27:18 [01] ...done 161213 12:27:18 [01] Copying ./wl/zx.frm to /mysqlbackup/2016-12-13_12-27-13/wl/zx.frm 161213 12:27:18 [01] ...done 161213 12:27:18 Finished backing up non-InnoDB tables and files 161213 12:27:18 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS... xtrabackup: The latest check point (for incremental): ‘2671634‘ xtrabackup: Stopping log copying thread. .161213 12:27:18 >> log scanned up to (2671643) 161213 12:27:18 Executing UNLOCK TABLES 161213 12:27:18 All tables unlocked 161213 12:27:18 [00] Copying ib_buffer_pool to /mysqlbackup/2016-12-13_12-27-13/ib_buffer_pool 161213 12:27:18 [00] ...done 161213 12:27:18 Backup created in directory ‘/mysqlbackup/2016-12-13_12-27-13‘ 161213 12:27:18 [00] Writing backup-my.cnf 161213 12:27:18 [00] ...done 161213 12:27:18 [00] Writing xtrabackup_info 161213 12:27:18 [00] ...done xtrabackup: Transaction log of lsn (2671634) to (2671643) was copied. 161213 12:27:19 completed OK! #在指定的目录下生成了一个时间目录 [[email protected] ~]# ls -l /mysqlbackup/ total 4 drwxr-x---. 7 root root 4096 Dec 13 12:27 2016-12-13_12-27-13 [[email protected] ~]# du -sm /mysqlbackup/* 90 /mysqlbackup/2016-12-13_12-27-13
注意:innobackupex会去读取my.cnf文件中的[mysqld]和[xtrabackup]部分,也可以使用--defaults-file指定参数文件。
2)恢复全备的数据
备份的数据不能直接使用,需要先做prepare操作(可以理解为应用日志的过程,commit的事务提交,未commit的事务回滚)
#使用--apply-log应用日志, [[email protected] ~]# innobackupex --apply-log /mysqlbackup/2016-12-13_12-27-13/ 161213 12:36:24 innobackupex: Starting the apply-log operation IMPORTANT: Please check that the apply-log run completes successfully. At the end of a successful apply-log run innobackupex prints "completed OK!". innobackupex version 2.4.5 based on MySQL server 5.7.13 Linux (x86_64) (revision id: e41c0be) xtrabackup: cd to /mysqlbackup/2016-12-13_12-27-13/ xtrabackup: This target seems to be not prepared yet. InnoDB: Number of pools: 1 xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(2671634) xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend xtrabackup: innodb_log_group_home_dir = . xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 8388608 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend xtrabackup: innodb_log_group_home_dir = . xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 8388608 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter) InnoDB: PUNCH HOLE support not available InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Uses event mutexes InnoDB: GCC builtin __sync_synchronize() is used for memory barrier InnoDB: Compressed tables use zlib 1.2.3 InnoDB: Number of pools: 1 InnoDB: Not using CPU crc32 instructions InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M InnoDB: Completed initialization of buffer pool InnoDB: page_cleaner coordinator priority: -20 InnoDB: Highest supported file format is Barracuda. InnoDB: Log scan progressed past the checkpoint lsn 2671634 InnoDB: Doing recovery: scanned up to log sequence number 2671643 (0%) InnoDB: Doing recovery: scanned up to log sequence number 2671643 (0%) InnoDB: Database was not shutdown normally! InnoDB: Starting crash recovery. InnoDB: Creating shared tablespace for temporary tables InnoDB: Setting file ‘./ibtmp1‘ size to 12 MB. Physically writing the file full; Please wait ... InnoDB: File ‘./ibtmp1‘ size is now 12 MB. InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active. InnoDB: 32 non-redo rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.7.13 started; log sequence number 2671643 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 2671671 InnoDB: Number of pools: 1 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend xtrabackup: innodb_log_group_home_dir = . xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 50331648 InnoDB: PUNCH HOLE support not available InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Uses event mutexes InnoDB: GCC builtin __sync_synchronize() is used for memory barrier InnoDB: Compressed tables use zlib 1.2.3 InnoDB: Number of pools: 1 InnoDB: Not using CPU crc32 instructions InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M InnoDB: Completed initialization of buffer pool InnoDB: page_cleaner coordinator priority: -20 InnoDB: Setting log file ./ib_logfile101 size to 48 MB InnoDB: Setting log file ./ib_logfile1 size to 48 MB InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 InnoDB: New log files created, LSN=2671671 InnoDB: Highest supported file format is Barracuda. InnoDB: Log scan progressed past the checkpoint lsn 2672140 InnoDB: Doing recovery: scanned up to log sequence number 2672149 (0%) InnoDB: Doing recovery: scanned up to log sequence number 2672149 (0%) InnoDB: Database was not shutdown normally! InnoDB: Starting crash recovery. InnoDB: Removed temporary tablespace data file: "ibtmp1" InnoDB: Creating shared tablespace for temporary tables InnoDB: Setting file ‘./ibtmp1‘ size to 12 MB. Physically writing the file full; Please wait ... InnoDB: File ‘./ibtmp1‘ size is now 12 MB. InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active. InnoDB: 32 non-redo rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.7.13 started; log sequence number 2672149 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 2672168 161213 12:36:30 completed OK! #查看prepare后数据目录的大小 [[email protected] ~]# ls -l /mysqlbackup/ total 4 drwxr-x---. 7 root root 4096 Dec 13 12:36 2016-12-13_12-27-13 [[email protected] ~]# du -sm /mysqlbackup/* 206 /mysqlbackup/2016-12-13_12-27-13
prepare完成后恢复数据到原MySQL的数据目录
注意:MySQL的数据目录必须为空,MySQL服务也必须停止,否则恢复会报错(除非恢复部分备份)
#创建一个测试库zx [[email protected] ~]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | wl | +--------------------+ 6 rows in set (0.10 sec) mysql> create database zx; Query OK, 1 row affected (0.00 sec) mysql> exit Bye #停止MySQL数据库并创建新的data目录 [[email protected] ~]# service mysqld stop Shutting down MySQL.. SUCCESS! [[email protected] ~]# cd $MYSQL_HOME [[email protected] mysql]# ls bin COPYING data docs include lib man mysql-test README share support-files [[email protected] mysql]# mv data data_bak [[email protected] mysql]# mkdir data #恢复数据 [[email protected] mysql]# innobackupex --copy-back /mysqlbackup/2016-12-13_12-27-13/ 161213 13:03:50 innobackupex: Starting the copy-back operation IMPORTANT: Please check that the copy-back run completes successfully. At the end of a successful copy-back run innobackupex prints "completed OK!". innobackupex version 2.4.5 based on MySQL server 5.7.13 Linux (x86_64) (revision id: e41c0be) 161213 13:03:50 [01] Copying ib_logfile0 to /usr/local/mysql/data/ib_logfile0 161213 13:03:50 [01] ...done 161213 13:03:51 [01] Copying ib_logfile1 to /usr/local/mysql/data/ib_logfile1 161213 13:03:51 [01] ...done 161213 13:03:52 [01] Copying ibdata1 to /usr/local/mysql/data/ibdata1 161213 13:03:54 [01] ...done 161213 13:03:55 [01] Copying ./mysql/plugin.ibd to /usr/local/mysql/data/mysql/plugin.ibd 161213 13:03:55 [01] ...done 161213 13:03:55 [01] Copying ./mysql/servers.ibd to /usr/local/mysql/data/mysql/servers.ibd 161213 13:03:55 [01] ...done 161213 13:03:55 [01] Copying ./mysql/help_topic.ibd to /usr/local/mysql/data/mysql/help_topic.ibd 161213 13:03:55 [01] ...done 161213 13:03:55 [01] Copying ./mysql/help_category.ibd to /usr/local/mysql/data/mysql/help_category.ibd 161213 13:03:55 [01] ...done ....... 161213 13:03:57 [01] Copying ./ibtmp1 to /usr/local/mysql/data/ibtmp1 161213 13:03:57 [01] ...done 161213 13:03:57 completed OK! #修改data目录下的权限 [[email protected] mysql]# chown mysql:mysql -R data #启动MySQL数据库查看恢复是否成功 [[email protected] mysql]# service mysqld start Starting MySQL. SUCCESS! [[email protected] mysql]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | wl | +--------------------+ 6 rows in set (0.00 sec) #数据恢复成功,测试数据库zx不见了。
4.增量备份的操作步骤
1)创建备份,增量备份需要有一个全量备份做基础,所以先做一个全量备份再做增量备份
#创建全备 [[email protected] mysql]# innobackupex --user=root --password=123456 /mysqlbackup/ [[email protected] mysql]# ls -l /mysqlbackup/ total 4 drwxr-x---. 8 root root 4096 Dec 13 13:10 2016-12-13_13-10-48 #删除一个测试库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | wl | | zx | +--------------------+ 7 rows in set (0.00 sec) mysql> drop database zx; Query OK, 0 rows affected (0.07 sec) #创建增量备份(基于全备) [[email protected] mysql]# innobackupex --user=root --password=123456 --incremental /mysqlbackup/ --incremental-basedir=/mysqlbackup/2016-12-13_13-10-48/ ...... [[email protected] mysql]# ls -l /mysqlbackup/ total 8 drwxr-x---. 8 root root 4096 Dec 13 13:10 2016-12-13_13-10-48 drwxr-x---. 7 root root 4096 Dec 13 13:16 2016-12-13_13-16-17 [[email protected] mysql]# du -sm /mysqlbackup/* 90 /mysqlbackup/2016-12-13_13-10-48 3 /mysqlbackup/2016-12-13_13-16-17 #删除一个测试库 mysql> drop database wl; Query OK, 0 rows affected (0.07 sec) #创建增量备份(基于上次增量备份) [[email protected] mysql]# innobackupex --user=root --password=123456 --incremental /mysqlbackup/ --incremental-basedir=/mysqlbackup/2016-12-13_13-16-17/ ...... [[email protected] mysql]# ls -l /mysqlbackup/ total 12 drwxr-x---. 8 root root 4096 Dec 13 13:10 2016-12-13_13-10-48 drwxr-x---. 7 root root 4096 Dec 13 13:16 2016-12-13_13-16-17 drwxr-x---. 6 root root 4096 Dec 13 13:18 2016-12-13_13-18-30 [[email protected] mysql]# du -sm /mysqlbackup/* 90 /mysqlbackup/2016-12-13_13-10-48 3 /mysqlbackup/2016-12-13_13-16-17 4 /mysqlbackup/2016-12-13_13-18-30
2)恢复增量备份
同全量备份一样恢复里也需要prepare
#全量备份的prepare --redo-only表示只提交commit的事务 [[email protected] mysql]# innobackupex --apply-log --redo-only /mysqlbackup/2016-12-13_13-10-48/ #第一次增量备份prepare [[email protected] mysql]# innobackupex --apply-log --redo-only /mysqlbackup/2016-12-13_13-10-48/ --incremental-dir=/mysqlbackup/2016-12-13_13-16-17/ #最后一次增量备份prepare 不再需要redo-only参数 [[email protected] mysql]# innobackupex --apply-log /mysqlbackup/2016-12-13_13-10-48/ --incremental-dir=/mysqlbackup/2016-12-13_13-18-30/ #停止MySQL服务创建新的data目录 [[email protected] mysql]# service mysqld stop Shutting down MySQL.. SUCCESS! [[email protected] mysql]# rm -rf data [[email protected] mysql]# mkdir data #恢复数据 [[email protected] mysql]# innobackupex --copy-back /mysqlbackup/2016-12-13_13-10-48/ #修改data目录权限 [[email protected] mysql]# chown mysql:mysql -R data #启动MySQL服务并验证 [[email protected] mysql]# service mysqld start Starting MySQL... SUCCESS! [[email protected] mysql]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | wl | +--------------------+ 6 rows in set (0.00 sec) mysql> show tables in wl; Empty set (0.00 sec) #测试库zx没有了,wl库还在,但是库里的表已经没有了。
本文出自 “DBA Fighting!” 博客,请务必保留此出处http://hbxztc.blog.51cto.com/1587495/1882228
以上是关于使用XtraBackup 备份MySQL数据库的主要内容,如果未能解决你的问题,请参考以下文章