您好,登錄后才能下訂單哦!
本次測試使用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)創建備份
#創建存儲備份數據的目錄 [root@rhel7 ~]# mkdir /mysqlbackup #開始備份 [root@rhel7 ~]# 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! #在指定的目錄下生成了一個時間目錄 [root@rhel7 ~]# ls -l /mysqlbackup/ total 4 drwxr-x---. 7 root root 4096 Dec 13 12:27 2016-12-13_12-27-13 [root@rhel7 ~]# du -sm /mysqlbackup/* 90 /mysqlbackup/2016-12-13_12-27-13
注意:innobackupex會去讀取my.cnf文件中的[mysqld]和[xtrabackup]部分,也可以使用--defaults-file指定參數文件。
2)恢復全備的數據
備份的數據不能直接使用,需要先做prepare操作(可以理解為應用日志的過程,commit的事務提交,未commit的事務回滾)
#使用--apply-log應用日志, [root@rhel7 ~]# 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后數據目錄的大小 [root@rhel7 ~]# ls -l /mysqlbackup/ total 4 drwxr-x---. 7 root root 4096 Dec 13 12:36 2016-12-13_12-27-13 [root@rhel7 ~]# du -sm /mysqlbackup/* 206 /mysqlbackup/2016-12-13_12-27-13
prepare完成后恢復數據到原MySQL的數據目錄
注意:MySQL的數據目錄必須為空,MySQL服務也必須停止,否則恢復會報錯(除非恢復部分備份)
#創建一個測試庫zx [root@rhel7 ~]# 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目錄 [root@rhel7 ~]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@rhel7 ~]# cd $MYSQL_HOME [root@rhel7 mysql]# ls bin COPYING data docs include lib man mysql-test README share support-files [root@rhel7 mysql]# mv data data_bak [root@rhel7 mysql]# mkdir data #恢復數據 [root@rhel7 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目錄下的權限 [root@rhel7 mysql]# chown mysql:mysql -R data #啟動MySQL數據庫查看恢復是否成功 [root@rhel7 mysql]# service mysqld start Starting MySQL. SUCCESS! [root@rhel7 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)創建備份,增量備份需要有一個全量備份做基礎,所以先做一個全量備份再做增量備份
#創建全備 [root@rhel7 mysql]# innobackupex --user=root --password=123456 /mysqlbackup/ [root@rhel7 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) #創建增量備份(基于全備) [root@rhel7 mysql]# innobackupex --user=root --password=123456 --incremental /mysqlbackup/ --incremental-basedir=/mysqlbackup/2016-12-13_13-10-48/ ...... [root@rhel7 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 [root@rhel7 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) #創建增量備份(基于上次增量備份) [root@rhel7 mysql]# innobackupex --user=root --password=123456 --incremental /mysqlbackup/ --incremental-basedir=/mysqlbackup/2016-12-13_13-16-17/ ...... [root@rhel7 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 [root@rhel7 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的事務 [root@rhel7 mysql]# innobackupex --apply-log --redo-only /mysqlbackup/2016-12-13_13-10-48/ #第一次增量備份prepare [root@rhel7 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參數 [root@rhel7 mysql]# innobackupex --apply-log /mysqlbackup/2016-12-13_13-10-48/ --incremental-dir=/mysqlbackup/2016-12-13_13-18-30/ #停止MySQL服務創建新的data目錄 [root@rhel7 mysql]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@rhel7 mysql]# rm -rf data [root@rhel7 mysql]# mkdir data #恢復數據 [root@rhel7 mysql]# innobackupex --copy-back /mysqlbackup/2016-12-13_13-10-48/ #修改data目錄權限 [root@rhel7 mysql]# chown mysql:mysql -R data #啟動MySQL服務并驗證 [root@rhel7 mysql]# service mysqld start Starting MySQL... SUCCESS! [root@rhel7 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庫還在,但是庫里的表已經沒有了。
前邊只是提供了簡單的恢復方法,如果數據庫備份之后運行了一段時間,數據庫宕機了,如果只恢復備份的數據那就會丟失一部分數據。那這部分數據要怎么恢復呢。答案就是應用binlog。
在啟用binlog的數據庫中,備份完成后日志中會輸出關于binlog的信息,生成的備份目錄中也會有一個xtrabackup_binlog_info文件保存備份時的binlog位置。
161214 10:24:34 Executing UNLOCK TABLES 161214 10:24:34 All tables unlocked 161214 10:24:34 [00] Copying ib_buffer_pool to /mysqlbackup//2016-12-14_10-24-27/ib_buffer_pool 161214 10:24:34 [00] ...done 161214 10:24:34 Backup created in directory '/mysqlbackup//2016-12-14_10-24-27' #binlog位置 MySQL binlog position: filename 'mysql-bin.000001', position '154' 161214 10:24:34 [00] Writing backup-my.cnf 161214 10:24:34 [00] ...done 161214 10:24:34 [00] Writing xtrabackup_info 161214 10:24:34 [00] ...done xtrabackup: Transaction log of lsn (2677865) to (2677874) was copied. 161214 10:24:34 completed OK! [root@rhel7 2016-12-14_10-24-27]# ls -l total 77876 -rw-r-----. 1 root root 425 Dec 14 10:24 backup-my.cnf -rw-r-----. 1 root root 307 Dec 14 10:24 ib_buffer_pool -rw-r-----. 1 root root 79691776 Dec 14 10:24 ibdata1 drwxr-x---. 2 root root 4096 Dec 14 10:24 mysql drwxr-x---. 2 root root 8192 Dec 14 10:24 performance_schema drwxr-x---. 2 root root 8192 Dec 14 10:24 sys drwxr-x---. 2 root root 87 Dec 14 10:24 test -rw-r-----. 1 root root 21 Dec 14 10:24 xtrabackup_binlog_info -rw-r-----. 1 root root 113 Dec 14 10:24 xtrabackup_checkpoints -rw-r-----. 1 root root 469 Dec 14 10:24 xtrabackup_info -rw-r-----. 1 root root 2560 Dec 14 10:24 xtrabackup_logfile #記錄binlog位置的文件 [root@rhel7 2016-12-14_10-24-27]# cat xtrabackup_binlog_info mysql-bin.000001 154
有了這個binlog日志位置就可以應用binlog恢復備份到宕機時間的數據
mysqlbinlog --start-position=154 mysql-bin.000001 | mysql -uroot -p123456
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。