1. 程式人生 > >05: mysqldump 實時增量備份 、 innobackupex

05: mysqldump 實時增量備份 、 innobackupex

Top

NSD DBA1 DAY05

1 資料備份與恢復

1.1 問題

本案例要求熟悉MySQL的備份與恢復,完成以下任務操作:

  • 邏輯備份工具 mysqldump
  • 使用mysql 恢復資料庫

1.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:使用mysqldump進行邏輯備份

1)備份MySQL伺服器上的所有庫

將所有的庫備份為mysql-all.sql檔案:

  1. [[email protected] ~]# mysqldump -u root -p --all-databases > /root/alldb.sql
  2. Enter password:                                 //驗證口令
  3. [[email protected] mysql]# file /root/alldb.sql         //確認備份檔案型別
  4. /root/alldb.sql: UTF-8 Unicode English text, with very long lines

檢視備份檔案alldb.sql的部分內容:

  1. [[email protected] ~]# grep -vE '^/|^-|^$' /root/alldb.sql | head -15
  2. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `home` /*!40100 DEFAULT CHARACTER SET latin1 */;
  3. USE `home`;
  4. DROP TABLE IF EXISTS `biao01`;
  5. CREATE TABLE `biao01` (
  6. `id` int(2) NOT NULL,
  7. `name` varchar(8) DEFAULT NULL
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  9. LOCK TABLES `biao01` WRITE;
  10. UNLOCK TABLES;
  11. DROP TABLE IF EXISTS `biao02`;
  12. CREATE TABLE `biao02` (
  13. `id` int(4) NOT NULL,
  14. `name` varchar(8) DEFAULT NULL,
  15. PRIMARY KEY (`id`)
  16. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  17. .. ..

注意:若資料庫都使用MyISAM儲存引擎,可以採用冷備份的方式,直接複製對應的資料庫目錄即可;恢復時重新複製回來就行。

2)只備份指定的某一個庫

將userdb庫備份為userdb.sql檔案:

  1. [[email protected] ~]# mysqldump -u root -p userdb > userdb.sql
  2. Enter password:                                 //驗證口令

檢視備份檔案userdb.sql的部分內容:

  1. [[email protected] ~]# grep -vE '^/|^-|^$' /root/userdb.sql
  2. DROP TABLE IF EXISTS `stu_info`;
  3. CREATE TABLE `stu_info` (
  4. `name` varchar(12) NOT NULL,
  5. `gender` enum('boy','girl') DEFAULT 'boy',
  6. `age` int(3) NOT NULL
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  8. LOCK TABLES `stu_info` WRITE;
  9. .. ..

3)同時備份指定的多個庫

同時備份mysql、userdb庫,儲存為mysql+userdb.sql檔案:

  1. [[email protected] ~]# mysqldump -u root -p -B mysql userdb > mysql+test+userdb.sql
  2. Enter password:                                 //驗證口令

檢視備份檔案userdb.sql的部分內容:

  1. [[email protected] ~]# grep '^CREATE DATA' /root/mysql+userdb.sql
  2. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */;
  3. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `userdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

步驟二:使用mysql命令從備份中恢復資料庫、表

以恢復userdb庫為例,可參考下列操作。通常不建議直接覆蓋舊庫,而是採用建立新庫並匯入邏輯備份的方式執行恢復,待新庫正常後即可廢棄或刪除舊庫。

1)建立名為userdb2的新庫

  1. mysql> CREATE DATABASE userdb2;
  2. Query OK, 1 row affected (0.00 sec)

2)匯入備份檔案,在新庫中重建表及資料

  1. [[email protected] ~]# mysql -u root -p userdb2 < /root/userdb.sql
  2. Enter password:                                 //驗證口令

3)確認新庫正常,啟用新庫

  1. mysql> USE userdb2;                             //切換到新庫
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> SELECT sn,username,uid,gid,homedir         //查詢資料,確認可用
  6. -> FROM userlist LIMIT 10;
  7. +----+----------+-----+-----+-----------------+
  8. | sn | username | uid | gid | homedir |
  9. +----+----------+-----+-----+-----------------+
  10. | 1 | root | 0 | 0 | /root |
  11. | 2 | bin | 1 | 1 | /bin |
  12. | 3 | daemon | 2 | 2 | /sbin |
  13. | 4 | adm | 3 | 4 | /var/adm |
  14. | 5 | lp | 4 | 7 | /var/spool/lpd |
  15. | 6 | sync | 5 | 0 | /sbin |
  16. | 7 | shutdown | 6 | 0 | /sbin |
  17. | 8 | halt | 7 | 0 | /sbin |
  18. | 9 | mail | 8 | 12 | /var/spool/mail |
  19. | 10 | operator | 11 | 0 | /root |
  20. +----+----------+-----+-----+-----------------+
  21. 10 rows in set (0.00 sec)

4)廢棄或刪除舊庫

  1. mysql> DROP DATABASE userdb;
  2. Query OK, 2 rows affected (0.09 sec)

2 使用binlog日誌

2.1 問題

利用binlog恢復庫表,要求如下:

  • 啟用binlog日誌
  • 建立db1庫tb1表,插入3條記錄
  • 刪除tb1表中剛插入的3條記錄
  • 使用mysqlbinlog恢復刪除的3條記錄

2.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:啟用binlog日誌

1)調整/etc/my.cnf配置,並重啟服務

  1. [[email protected] ~]# vim /etc/my.cnf
  2. [mysqld]
  3. .. ..
  4. log-bin-index=mysql-bin                             //啟用二進位制日誌,並指定字首
  5. server_id=1
  6. binlog_format=STATEMENT
  7. //在Mysql5.7中,binlog日誌格式預設為ROW,但它不記錄sql語句上下文相關資訊。需要將binlog日誌格式修改為STATEMENT
  8. .. ..
  9. [[email protected] ~]# systemctl restart mysqld.service

2)確認binlog日誌檔案

新啟用binlog後,每次啟動MySQl服務都會新生成一份日誌檔案:

  1. [[email protected] ~]# ls /var/lib/mysql/mysql-bin.*
  2. /var/lib/mysql/mysql-bin.000001 /var/lib/mysql/mysql-bin.index

其中mysql-bin.index檔案記錄了當前保持的二進位制檔案列表:

  1. [[email protected] ~]# cat /var/lib/mysql/mysql-bin.index
  2. ./mysql-bin.000001

重啟MySQL服務程式,或者執行SQL操作“FLUSH LOGS;”,會生成一份新的日誌:

  1. [[email protected] ~]# ls /var/lib/mysql/mysql-bin.*
  2. /var/lib/mysql/mysql-bin.000001 /var/lib/mysql/mysql-bin.index
  3. /var/lib/mysql/mysql-bin.000002
  4. [[email protected] ~]# cat /var/lib/mysql/mysql-bin.index
  5. ./mysql-bin.000001
  6. ./mysql-bin.000002

步驟二:利用binlog日誌重做資料庫操作

1)執行資料庫表新增操作

建立db1·庫tb1表,表結構自定義:

  1. mysql> CREATE DATABASE db1;
  2. Query OK, 1 row affected (0.05 sec)
  3. mysql> USE db1;
  4. Database changed
  5. mysql> CREATE TABLE tb1(
  6. -> id int(4) NOT NULL,name varchar(24)
  7. -> );
  8. Query OK, 0 rows affected (0.28 sec)

插入3條表記錄:

  1. mysql> INSERT INTO tb1 VALUES
  2. -> (1,'Jack'),
  3. -> (2,'Kenthy'),
  4. -> (3,'Bob');
  5. Query OK, 3 rows affected (0.12 sec)
  6. Records: 3 Duplicates: 0 Warnings: 0

確認插入的表記錄資料:

  1. mysql> SELECT * FROM tb1;
  2. +----+--------+
  3. | id | name |
  4. +----+--------+
  5. | 1 | Jack |
  6. | 2 | Kenthy |
  7. | 3 | Bob |
  8. +----+--------+
  9. 3 rows in set (0.00 sec)

2)刪除前一步新增的3條表記錄

執行刪除所有表記錄操作:

  1. mysql> DELETE FROM tb1;
  2. Query OK, 3 rows affected (0.09 sec)

確認刪除結果:

  1. mysql> SELECT * FROM tb1;
  2. Empty set (0.00 sec)

步驟三:通過binlog日誌恢復表記錄

binlog會記錄所有的資料庫、表更改操作,所以可在必要的時候重新執行以前做過的一部分資料操作,但對於啟用binlog之前已經存在的庫、表資料將不適用。

根據上述“恢復被刪除的3條表記錄”的需求,應通過mysqlbinlog工具檢視相關日誌檔案,找到刪除這些表記錄的時間點,只要恢復此前的SQL操作(主要是插入那3條記錄的操作)即可。

1)檢視mysql-bin.000002日誌內容

  1. [[email protected] ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000002
  2. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
  3. /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
  4. DELIMITER /*!*/;
  5. # at 4
  6. #170412 12:05:32 server id 1 end_log_pos 123 CRC32 0x6d8c069c Start: binlog v 4, server v 5.7.17-log created 170412 12:05:32 at startup
  7. # Warning: this binlog is either in use or was not closed properly.
  8. ROLLBACK/*!*/;
  9. BINLOG '
  10. jKftWA8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  11. AAAAAAAAAAAAAAAAAACMp+1YEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
  12. AZwGjG0=
  13. '/*!*/;
  14. # at 123
  15. #170412 12:05:32 server id 1 end_log_pos 154 CRC32 0x17f50164 Previous-GTIDs
  16. # [empty]
  17. # at 154
  18. #170412 12:05:59 server id 1 end_log_pos 219 CRC32 0x4ba5a976 Anonymous_GTID last_committed=0 sequence_number=1
  19. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
  20. # at 219
  21. #170412 12:05:59 server id 1 end_log_pos 310 CRC32 0x5b66ae13 Query thread_id=3 exec_time=0 error_code=0
  22. SET TIMESTAMP=1491969959/*!*/;
  23. SET @@session.pseudo_thread_id=3/*!*/;
  24. SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
  25. SET @@session.sql_mode=1436549152/*!*/;
  26. SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
  27. /*!\C utf8 *//*!*/;
  28. SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
  29. SET @@session.lc_time_names=0/*!*/;
  30. SET @@session.collation_database=DEFAULT/*!*/;
  31. CREATE DATABASE db1
  32. /*!*/;
  33. # at 310
  34. #170412 12:06:23 server id 1 end_log_pos 375 CRC32 0x2967cc28 Anonymous_GTID last_committed=1 sequence_number=2
  35. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
  36. # at 375
  37. #170412 12:06:23 server id 1 end_log_pos 502 CRC32 0x5de09aae Query thread_id=3 exec_time=0 error_code=0
  38. use `db1`/*!*/;
  39. SET TIMESTAMP=1491969983/*!*/;
  40. CREATE TABLE tb1(
  41. id int(4) NOT NULL,name varchar(24)
  42. )
  43. /*!*/;
  44. # at 502
  45. #170412 12:06:55 server id 1 end_log_pos 567 CRC32 0x0b8cd418 Anonymous_GTID last_committed=2 sequence_number=3
  46. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
  47. # at 567
  48. #170412 12:06:55 server id 1 end_log_pos 644 CRC32 0x7e8f2fa0 Query thread_id=3 exec_time=0 error_code=0
  49. SET TIMESTAMP=1491970015/*!*/;
  50. BEGIN
  51. /*!*/;
  52. # at 644
  53. #170412 12:06:55 server id 1 end_log_pos 772 CRC32 0x4e3f728e Query thread_id=3 exec_time=0 error_code=0 //插入表記錄的起始時間點
  54. SET TIMESTAMP=1491970015/*!*/;
  55. INSERT INTO tb1 VALUES(1,'Jack'),(2,'Kenthy'), (3,'Bob')
  56. /*!*/;
  57. # at 772
  58. #170412 12:06:55 server id 1 end_log_pos 803 CRC32 0x6138b21f Xid = 10
  59. //確認事務的時間點
  60. COMMIT/*!*/;
  61. # at 803
  62. #170412 12:07:24 server id 1 end_log_pos 868 CRC32 0xbef3f472 Anonymous_GTID last_committed=3 sequence_number=4
  63. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
  64. # at 868
  65. #170412 12:07:24 server id 1 end_log_pos 945 CRC32 0x5684e92c Query thread_id=3 exec_time=0 error_code=0
  66. SET TIMESTAMP=1491970044/*!*/;
  67. BEGIN
  68. /*!*/;
  69. # at 945
  70. #170412 12:07:24 server id 1 end_log_pos 1032 CRC32 0x4c1c75fc Query thread_id=3 exec_time=0 error_code=0 //刪除表記錄的時間點
  71. SET TIMESTAMP=1491970044/*!*/;
  72. DELETE FROM tb1
  73. /*!*/;
  74. # at 1032
  75. #170412 12:07:24 server id 1 end_log_pos 1063 CRC32 0xccf549b2 Xid = 12
  76. COMMIT/*!*/;
  77. SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
  78. DELIMITER ;
  79. # End of log file
  80. /*!50003 SET [email protected]_COMPLETION_TYPE*/;
  81. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

2) 執行指定Pos節點範圍內的sql命令恢復資料

根據上述日誌分析,只要恢復從2014.01.12 20:12:14到2014.01.12 20:13:50之間的操作即可。可通過mysqlbinlog指定時間範圍輸出,結合管道交給msyql命令執行匯入重做:

  1. [[email protected] ~]# mysqlbinlog \
  2. --start-datetime="2017-04-12 12:06:55" \
  3. --stop-datetime="2017-04-12 12:07:23" \
  4. /var/lib/mysql/mysql-bin.000002 | mysql -u root -p
  5. Enter password:                                  //驗證口令

3)確認恢復結果

  1. mysql> SELECT * FROM db1.tb1;
  2. +----+--------+
  3. | id | name |
  4. +----+--------+
  5. | 1 | Jack |
  6. | 2 | Kenthy |
  7. | 3 | Bob |
  8. +----+--------+
  9. 3 rows in set (0.00 sec)

3 innobackupex備份工具

3.1 問題

  • 安裝percona軟體包
  • innobackupex完整備份、增量備份操作。
  • 恢復資料

3.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:安裝XtraBackup軟體包

1)瞭解軟體包描述資訊

  1. [[email protected] pub]# rpm -qpi percona-xtrabackup-24-2.4.6-2.el7.x86_64.rpm
  2. Name : percona-xtrabackup-24
  3. Version : 2.4.6
  4. Release : 2.el7
  5. Architecture: x86_64
  6. Install Date: (not installed)
  7. Group : Applications/Databases
  8. Size : 32416340
  9. License : GPLv2
  10. Signature : DSA/SHA1, 2017年02月27日 星期一 20時28分17秒, Key ID 1c4cbdcdcd2efd2a
  11. Source RPM : percona-xtrabackup-24-2.4.6-2.el7.src.rpm
  12. Build Date : 2017年02月27日 星期一 20時27分21秒
  13. Build Host : vps-centos7-x64-01.ci.percona.com
  14. Relocations : (not relocatable)
  15. URL : http://www.percona.com/software/percona-xtrabackup
  16. Summary : XtraBackup online backup for MySQL / InnoDB
  17. Description :
  18. Percona XtraBackup is OpenSource online (non-blockable) backup solution for InnoDB and XtraDB engines

2)安裝依賴包perl-DBD-MySQL perl-Digest-MD5 libev

使用RHEL 7自帶的即可,yum方式安裝:

  1. [[email protected] pub]# yum -y install perl-DBD-MySQL perl-Digest-MD5
  2. libev使用網上找的rpm包 libev-4.15-1.el6.rf.x86_64.rpm //該包由講師提供
  3. [[email protected] pub]#rpm –ivh libev-4.15-1.el6.rf.x86_64.rpm

如果未安裝這些依賴包,則直接安裝percona-xtrabackup時會報錯:

程式碼

3)安裝percona-xtrabackup

  1. [[email protected] pub]#rpm -ivh percona-xtrabackup-*.rpm
  2. 警告:percona-xtrabackup-24-2.4.6-2.el7.x86_64.rpm: 頭V4 DSA/SHA1 Signature, 金鑰 ID cd2efd2a: NOKEY
  3. 準備中... ################################# [100%]
  4. 正在升級/安裝...
  5. 1:percona-xtrabackup-24-2.4.6-2.el7################################# [ 33%]
  6. 2:percona-xtrabackup-test-24-2.4.6-################################# [ 67%]
  7. 3:percona-xtrabackup-24-debuginfo-2################################# [100%]

4)確認安裝的主要程式/指令碼

  1. [[email protected] pub]# rpm -ql percona-xtrabackup-24-2.4.6-2.el7.x86_64
  2. /usr/bin/innobackupex
  3. /usr/bin/xbcloud
  4. /usr/bin/xbcloud_osenv
  5. /usr/bin/xbcrypt
  6. /usr/bin/xbstream
  7. /usr/bin/xtrabackup
  8. /usr/share/doc/percona-xtrabackup-24-2.4.6
  9. /usr/share/doc/percona-xtrabackup-24-2.4.6/COPYING
  10. /usr/share/man/man1/innobackupex.1.gz
  11. /usr/share/man/man1/xbcrypt.1.gz
  12. /usr/share/man/man1/xbstream.1.gz
  13. /usr/share/man/man1/xtrabackup.1.gz

步驟二:innobackupex完整備份、增量備份操作

--host 主機名

--port 3306

--user 使用者名稱

--password 密碼

--databases="庫名"

--databases="庫1 庫2"

--databases="庫.表"

--no-timestamp 不用日期命名備份檔案儲存的子目錄,使用備份的資料庫名做備份目錄名

--no-timestmap 不使用日期命名備份目錄名

1)做一個完整備份

預設情況下,備份檔案儲存的子目錄會用日期命名,

innobackupex作為客戶端工具,以mysql協議連入mysqld,將資料備份到/backup資料夾:

  1. [[email protected] ~]# innobackupex --user=root --password=1234567 /backup/mysql –no-timestamp
  2. 170425 11:05:44 innobackupex: Starting the backup operation
  3. IMPORTANT: Please check that the backup run completes successfully.
  4. At the end of a successful backup run innobackupex
  5. prints "completed OK!".
  6. Unrecognized character \x01; marked by <-- HERE after <-- HERE near column 1 at - line 1374.
  7. 170425 11:05:45 Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
  8. Using server version 5.7.17
  9. innobackupex version 2.4.6 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 8ec05b7)
  10. xtrabackup: uses posix_fadvise().
  11. xtrabackup: cd to /var/lib/mysql
  12. xtrabackup: open files limit requested 0, set to 1024
  13. xtrabackup: using the following InnoDB configuration:
  14. xtrabackup: innodb_data_home_dir = .
  15. xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
  16. xtrabackup: innodb_log_group_home_dir = ./
  17. xtrabackup: innodb_log_files_in_group = 2
  18. xtrabackup: innodb_log_file_size = 50331648
  19. InnoDB: Number of pools: 1
  20. 170425 11:05:45 >> log scanned up to (2543893)
  21. xtrabackup: Generating a list of tablespaces
  22. InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0
  23. 170425 11:05:45 [01] Copying ./ibdata1 to /backup/ibdata1
  24. 170425 11:05:45 [01] ...done
  25. 170425 11:05:46 [01] Copying ./mysql/plugin.ibd to /backup/mysql/plugin.ibd
  26. 170425 11:05:46 [01] ...done
  27. 170425 11:05:46 [01] Copying ./mysql/servers.ibd to /backup/mysql/servers.ibd
  28. 170425 11:05:46 [01] ...done
  29. 170425 11:05:46 [01] Copying ./mysql/help_topic.ibd to /backup/mysql/help_topic.ibd
  30. 170425 11:05:46 [01] ...done
  31. 170425 11:05:46 >> log scanned up to (2543893)
  32. .. ..
  33. 170425 11:06:00 [01] Copying ./sys/[email protected]_global_by_latency.frm to /backup/sys/[email protected]_global_by_latency.frm
  34. 170425 11:06:00 [01] ...done
  35. 170425 11:06:00 [01] Copying ./sys/session_ssl_status.frm to /backup/sys/session_ssl_status.frm
  36. 170425 11:06:00 [01] ...done
  37. 170425 11:06:00 [01] Copying ./db1/db.opt to /backup/db1/db.opt
  38. 170425 11:06:00 [01] ...done
  39. 170425 11:06:00 [01] Copying ./db1/tb1.frm to /backup/db1/tb1.frm
  40. 170425 11:06:00 [01] ...done
  41. 170425 11:06:00 Finished backing up non-InnoDB tables and files
  42. 170425 11:06:00 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
  43. xtrabackup: The latest check point (for incremental): '2543884'
  44. xtrabackup: Stopping log copying thread.
  45. .170425 11:06:00 >> log scanned up to (2543893)
  46. 170425 11:06:00 Executing UNLOCK TABLES
  47. 170425 11:06:00 All tables unlocked
  48. 170425 11:06:00 [00] Copying ib_buffer_pool to /backup/ib_buffer_pool
  49. 170425 11:06:00 [00] ...done
  50. 170425 11:06:00 Backup created in directory '/backup/'
  51. 170425 11:06:00 [00] Writing backup-my.cnf
  52. 170425 11:06:00 [00] ...done
  53. 170425 11:06:00 [00] Writing xtrabackup_info
  54. 170425 11:06:00 [00] ...done
  55. xtrabackup: Transaction log of lsn (2543884) to (2543893) was copied.
  56. 170425 11:06:01 completed OK

確認備份好的檔案資料:

  1. [[email protected] ~]#ls /backup/
  2. backup-my.cnf ib_buffer_pool mysql sys xtrabackup_info
  3. db1 ibdata1 performance_schema xtrabackup_checkpoints xtrabackup_logfile

2)做一個增量備份(基於前一步的完整備份)

隨意做一些新增或更改庫表的操作,比如在db1庫中新建一個mytb的表:

  1. mysql> USE db1;
  2. Database changed
  3. mysql> CREATE TABLE mytb(id int(4), name varchar(24));
  4. Query OK, 0 rows affected (0.38 sec)
  5. mysql> INSERT INTO tb1 VALUES
  6. -> (1,'bon'),
  7. -> (2,'bo'),
  8. Query OK, 2 rows affected (0.12 sec)
  9. Records: 2 Duplicates: 0 Warnings: 0
  10. mysql> SELECT * FROM tb1;
  11. +------+------+
  12. | id | name |
  13. +------+------+
  14. | 1 | bob |
  15. | 2 | bo |
  16. +------+------+
  17. 2 rows in set (0.00 sec)

以前一次儲存到/backup的完整備份為基礎,做一個增量備份,儲存到/incr01/,指定增量備份參照的基本目錄(完整備份目錄)需要用到選項--incremental-basedir。相關操作如下:

  1. [[email protected] ~]# innobackupex --user=root --password=12345678 --incremental /incr01 --incremental-basedir=/backup/ --no-timestamp
  2. 170425 11:30:14 innobackupex: Starting the backup operation
  3. IMPORTANT: Please check that the backup run completes successfully.
  4. At the end of a successful backup run innobackupex
  5. prints "completed OK!".
  6. Unrecognized character \x01; marked by <-- HERE after <-- HERE near column 1 at - line 1374.
  7. 170425 11:30:14 Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
  8. Using server version 5.7.17
  9. innobackupex version 2.4.6 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 8ec05b7)
  10. incremental backup from 2543884 is enabled.
  11. xtrabackup: uses posix_fadvise().
  12. xtrabackup: cd to /var/lib/mysql
  13. xtrabackup: open files limit requested 0, set to 1024
  14. xtrabackup: using the following InnoDB configuration:
  15. xtrabackup: innodb_data_home_dir = .
  16. xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
  17. xtrabackup: innodb_log_group_home_dir = ./
  18. xtrabackup: innodb_log_files_in_group = 2
  19. xtrabackup: innodb_log_file_size = 50331648
  20. InnoDB: Number of pools: 1
  21. 170425 11:30:14 >> log scanned up to (2549933)
  22. xtrabackup: Generating a list of tablespaces
  23. InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0
  24. xtrabackup: using the full scan for incremental backup
  25. 170425 11:30:15 [01] Copying ./ibdata1 to /incr01/ibdata1.delta
  26. 170425 11:30:15 [01] ...done
  27. 170425 11:30:15 >> log scanned up to (2549933)
  28. 170425 11:30:15 [01] Copying ./mysql/plugin.ibd to /incr01/mysql/plugin.ibd.delta
  29. 170425 11:30:15 [01] ...done
  30. ... ...
  31. 170425 11:30:35 Executing UNLOCK TABLES
  32. 170425 11:30:35 All tables unlocked
  33. 170425 11:30:35 [00] Copying ib_buffer_pool to /incr01/ib_buffer_pool
  34. 170425 11:30:35 [00] ...done
  35. 170425 11:30:35 Backup created in directory '/incr01/'
  36. 170425 11:30:35 [00] Writing backup-my.cnf
  37. 170425 11:30:35 [00] ...done
  38. 170425 11:30:35 [00] Writing xtrabackup_info
  39. 170425 11:30:35 [00] ...done
  40. xtrabackup: Transaction log of lsn (2549924) to (2549933) was copied.
  41. 170425 11:30:35 completed OK!

確認備份好的檔案資料:

  1. [[email protected] ~]# ls /incr01/
  2. backup-my.cnf ib_buffer_pool ibdata1.meta performance_schema xtrabackup_checkpoints xtrabackup_logfile
  3. db1 ibdata1.delta mysql sys

對比完整備份、增量備份的大小:

  1. [[email protected] ~]# du -sh /backup/ /incr01/
  2. 142M    /backup/ //完整備份的大小
  3. 3.5M    /incr01/ //增量備份的大小

步驟三:恢復資料

通過XtraBackup工具備份的資料庫目錄,若要恢復到另一個MySQL伺服器,需要先做一個“--apply-log --redo-only ”的準備操作。

1)準備恢復“完整備份”

完成準備以後,最終/backup可用來重建MySQL伺服器。這種情況下,需要先做一個“--apply-log --redo-only ”的準備操作,以確保資料一致性:

  1. [[email protected] ~]#innobackupex --user=root --password=12345678 --apply-log --redo-only /backup/
  2. 170425 11:42:19 innobackupex: Starting the apply-log operation
  3. IMPORTANT: Please check that the apply-log run completes successfully.
  4. At the end of a successful apply-log run innobackupex
  5. prints "completed OK!".
  6. innobackupex version 2.4.6 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 8ec05b7)
  7. xtrabackup: cd to /backup/
  8. xtrabackup: This target seems to be already prepared.
  9. InnoDB: Number of pools: 1
  10. xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.
  11. xtrabackup: using the following InnoDB configuration for recovery:
  12. xtrabackup: innodb_data_home_dir = .
  13. xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
  14. xtrabackup: innodb_log_group_home_dir = .
  15. xtrabackup: innodb_log_files_in_group = 2
  16. xtrabackup: innodb_log_file_size = 50331648
  17. xtrabackup: using the following InnoDB configuration for recovery:
  18. xtrabackup: innodb_data_home_dir = .
  19. xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
  20. xtrabackup: innodb_log_group_home_dir = .
  21. xtrabackup: innodb_log_files_in_group = 2
  22. xtrabackup: innodb_log_file_size = 50331648
  23. xtrabackup: Starting InnoDB instance for recovery.
  24. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
  25. InnoDB: PUNCH HOLE support available
  26. InnoDB: Mutexes and rw_locks use GCC atomic builtins
  27. InnoDB: Uses event mutexes
  28. InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
  29. InnoDB: Compressed tables use zlib 1.2.7
  30. InnoDB: Number of pools: 1
  31. InnoDB: Not using CPU crc32 instructions
  32. InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
  33. InnoDB: Completed initialization of buffer pool
  34. InnoDB: page_cleaner coordinator priority: -20
  35. InnoDB: Highest supported file format is Barracuda.
  36. xtrabackup: starting shutdown with innodb_fast_shutdown = 1
  37. InnoDB: Starting shutdown...
  38. InnoDB: Shutdown completed; log sequence number 2544177
  39. InnoDB: Number of pools: 1
  40. 170425 11:42:20 completed OK!

準備恢復“增量備份”

  1. [[email protected] ~]#innobackupex --user=root --password=12345678 --apply-log --redo-only /backup/ --incremental-dir=/incr01
  2. 170425 11:42:55 innobackupex: Starting the apply-log operation
  3. IMPORTANT: Please check that the apply-log run completes successfully.
  4. At the end of a successful apply-log run innobackupex
  5. prints "completed OK!".
  6. innobackupex version 2.4.6 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 8ec05b7)
  7. incremental backup from 2543884 is enabled.
  8. xtrabackup: cd to /backup/
  9. xtrabackup: This target seems to be already prepared with --apply-log-only.
  10. InnoDB: Number of pools: 1
  11. xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(2549924)
  12. xtrabackup: using the following InnoDB configuration for recovery:
  13. xtrabackup: innodb_data_home_dir = .
  14. xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
  15. xtrabackup: innodb_log_group_home_dir = /incr01/
  16. xtrabackup: innodb_log_files_in_group = 1
  17. xtrabackup: innodb_log_file_size = 8388608
  18. xtrabackup: Generating a list of tablespaces
  19. InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0
  20. xtrabackup: page size for /incr01//ibdata1.delta is 16384 bytes
  21. Applying /incr01//ibdata1.delta to ./ibdata1...
  22. ... ...
  23. 170425 11:43:09 [01] Copying /incr01/performance_schema/global_status.frm to ./performance_schema/global_status.frm
  24. 170425 11:43:09 [01] ...done
  25. 170425 11:43:09 [01] Copying /incr01/performance_schema/session_status.frm to ./performance_schema/session_status.frm
  26. 170425 11:43:09 [01] ...done
  27. 170425 11:43:09 [00] Copying /incr01//xtrabackup_info to ./xtrabackup_info
  28. 170425 11:43:09 [00] ...done
  29. 170425 11:43:10 completed OK!

2)關閉mysql服務,並將/var/lib/mysql/下的檔案刪除,假設資料被刪除。

  1. [[email protected] ~]#systemctl stop mysqld
  2. [[email protected] ~]#rm -rf /var/lib/mysql

3)恢復“完整備份+增量備份”

完成準備以後,最終仍然是/backup用來重建MySQL伺服器,但這種情況下需提前合併相關增量備份的資料

  1. [[email protected] ~]# innobackupex --user=root --password=12345678 --copy-back /backup/
  2. ... ...
  3. 170425 11:51:39 [01] Copying ./performance_schema/global_status.frm to /var/lib/mysql/performance_schema/glo.frm
  4. 170425 11:51:39 [01] ...done
  5. 170425 11:51:39 [01] Copying ./performance_schema/session_status.frm to /var/lib/mysql/performance_schema/seus.frm
  6. 170425 11:51:39 [01] ...done
  7. 170425 11:51:39 [01] Copying ./ib_buffer_pool to /var/lib/mysql/ib_buffer_pool
  8. 170425 11:51:39 [01] ...done
  9. 170425 11:51:39 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1
  10. 170425 11:51:39 [01] ...done
  11. 170425 11:51:39 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
  12. 170425 11:51:39 [01] ...done
  13. 170425 11:51:39 completed OK!

4)修改/var/lib/mysql/下檔案屬主與屬組,檢視資料:

恢復後,/var/lib/mysql下檔案屬組與屬主皆為root,需要更改為mysql

  1. [[email protected] ~]#chown -R mysql:mysql /var/lib/mysql
  2. [[email protected] ~]#systemctl start mysqld.service
  3. [[email protected] ~]#mysql -uroot -p12345678 -e "select * from db1.tb1"
  4. mysql: [Warning] Using a password on the command line interface can be insecure.
  5. +------+------+
  6. | id | name |
  7. +------+------+
  8. | 1 | bob |
  9. | 2 | bo |
  10. +------+------+