Mariabackup による MariaDB Server のバックアップ/リストア

MariaDB Server 10.1 で以下の機能が実装されたため,

MariaDB Server 10.1 以降 Percona XtraBackup との互換性が失われています。

この対策として,MariaDB Server 10.1.23 / 10.2.7 以降,XtraBackup を fork した Mariabackup が利用可能となっています。
本記事では Mariabackup に関してご説明いたします。

Mariabackup 概要

Mariabackup では以下の機能がサポートされています。

Mariabackup と XtraBackup の違い

Mariabackup は XtraBackup を fork しているため機能的にはほぼ同等ですが,Mariabackup と XtraBackup には以下のような違いがあります。

  • Percona XtraBackup は InnoDB redo log ファイルを xtrabackup_logfile にコピーするが, Mariabackup は ib_logfile0 を用いる。
  • XtraBackup の encrypted backup は Mariabackup でサポートされない。
  • mariabackup から innobackupex, への symbolic link はない。
  • --compact ,  --rebuild_indexes オプションはサポートされない。
  • --stream=tar オプションはサポートされない。
  • xbstream  は mbstream に名称変更されている。

最新の MariaDB Server に対して XtraBackup を利用することの問題点

MariaDB Server 10.3 を利用し,以下のように XtraBackup によってバックアップを行おうとすると,エラーとなります。

$ innobackupex ~/backup_to_dir --user=username --password=password
...
InnoDB: Unsupported redo log format. The redo log was created with MariaDB 10.3.13.

XtraBackup は MariaDB Server 10.3 の redo log をサポートしません(ここで,XtraBackup は古いバージョンのため,innobackupex コマンドを用いています)。
これに対して Mariabackup は新しい redo log フォーマットに対応しています。

Mariabackup を用いたバックアップ/リストア

テスト環境として以下の2台の MariaDB Server を用意し,古いバージョンの MariaDB Server 上でバックアップを実施,新しいバージョンの MariaDB server 上でリストアを行ってみます。

  • MariaDB Server 10.2.22 (バックアップ元) / CentOS 7
  • MariaDB Server 10.3.13 (リストア先) / CentOS 7

なお,mariababckup を利用するには,以下のRPMパッケージをインストールする必要があります。

MariaDB-backup

MariaDB Server 10.2 におけるバックアップ

データのバックアップ先(target dir)として,/tmp/backup を指定し,mariabackup によるバックアップを実行します。
なお,バックアップ元のサーバには,事前に A sample MySQL database から clone したデータがインポート済です。

mdb102# time mariabackup --backup --target-dir /tmp/backup --user=root
[00] 2019-03-03 17:28:26 Connecting to MySQL server host: localhost, user: root, password: not set, port: not set, socket: not set
[00] 2019-03-03 17:28:26 Using server version 10.2.22-MariaDB
[00] 2019-03-03 17:28:26 mariabackup based on MariaDB server 10.2.22-MariaDB Linux (x86_64)
[00] 2019-03-03 17:28:26 uses posix_fadvise().
[00] 2019-03-03 17:28:26 cd to /var/lib/mysql/
[00] 2019-03-03 17:28:26 open files limit requested 0, set to 1024
[00] 2019-03-03 17:28:26 mariabackup: using the following InnoDB configuration:
[00] 2019-03-03 17:28:26 innodb_data_home_dir =
[00] 2019-03-03 17:28:26 innodb_data_file_path = ibdata1:12M:autoextend
[00] 2019-03-03 17:28:26 innodb_log_group_home_dir = ./
[00] 2019-03-03 17:28:26 InnoDB: Using Linux native AIO
2019-03-03 17:28:26 140678729349312 [Note] InnoDB: Number of pools: 1
[00] 2019-03-03 17:28:26 mariabackup: Generating a list of tablespaces
2019-03-03 17:28:26 140678729349312 [Warning] InnoDB: Allocated tablespace ID 1 for mysql/innodb_table_stats, old maximum was 0
[00] 2019-03-03 17:28:26 >> log scanned up to (292475043)
[01] 2019-03-03 17:28:26 Copying ibdata1 to /tmp/backup/ibdata1
[01] 2019-03-03 17:28:26         ...done
[01] 2019-03-03 17:28:27 Copying ./mysql/innodb_table_stats.ibd to /tmp/backup/mysql/innodb_table_stats.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./mysql/innodb_index_stats.ibd to /tmp/backup/mysql/innodb_index_stats.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./mysql/gtid_slave_pos.ibd to /tmp/backup/mysql/gtid_slave_pos.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/employees.ibd to /tmp/backup/employees/employees.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/departments.ibd to /tmp/backup/employees/departments.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/dept_manager.ibd to /tmp/backup/employees/dept_manager.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/dept_emp.ibd to /tmp/backup/employees/dept_emp.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/titles.ibd to /tmp/backup/employees/titles.ibd
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./employees/salaries.ibd to /tmp/backup/employees/salaries.ibd
[01] 2019-03-03 17:28:27         ...done
[00] 2019-03-03 17:28:27 >> log scanned up to (292475043)
[00] 2019-03-03 17:28:27 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
[00] 2019-03-03 17:28:27 Executing FLUSH TABLES WITH READ LOCK...
[00] 2019-03-03 17:28:27 Starting to backup non-InnoDB tables and files
[01] 2019-03-03 17:28:27 Copying ./mysql/db.frm to /tmp/backup/mysql/db.frm
[01] 2019-03-03 17:28:27         ...done
[01] 2019-03-03 17:28:27 Copying ./mysql/db.MYI to /tmp/backup/mysql/db.MYI
[01] 2019-03-03 17:28:27         ...done
...中略...
[01] 2019-03-03 17:28:28 Copying ./mysql/index_stats.frm to /tmp/backup/mysql/index_stats.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./mysql/index_stats.MYI to /tmp/backup/mysql/index_stats.MYI
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./mysql/index_stats.MYD to /tmp/backup/mysql/index_stats.MYD
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./mysql/gtid_slave_pos.frm to /tmp/backup/mysql/gtid_slave_pos.frm
[01] 2019-03-03 17:28:28         ...done
[00] 2019-03-03 17:28:28 Writing test/db.opt
[00] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./performance_schema/db.opt to /tmp/backup/performance_schema/db.opt
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/db.opt to /tmp/backup/employees/db.opt
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/employees.frm to /tmp/backup/employees/employees.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/departments.frm to /tmp/backup/employees/departments.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/dept_manager.frm to /tmp/backup/employees/dept_manager.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/dept_emp.frm to /tmp/backup/employees/dept_emp.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/titles.frm to /tmp/backup/employees/titles.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/salaries.frm to /tmp/backup/employees/salaries.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/dept_emp_latest_date.frm to /tmp/backup/employees/dept_emp_latest_date.frm
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying ./employees/current_dept_emp.frm to /tmp/backup/employees/current_dept_emp.frm
[01] 2019-03-03 17:28:28         ...done
[00] 2019-03-03 17:28:28 Finished backing up non-InnoDB tables and files
[01] 2019-03-03 17:28:28 Copying aria_log_control to /tmp/backup/aria_log_control
[01] 2019-03-03 17:28:28         ...done
[01] 2019-03-03 17:28:28 Copying aria_log.00000001 to /tmp/backup/aria_log.00000001
[01] 2019-03-03 17:28:28         ...done
[00] 2019-03-03 17:28:28 Waiting for log copy thread to read lsn 292475043
[00] 2019-03-03 17:28:28 >> log scanned up to (292475043)
[00] 2019-03-03 17:28:28 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
[00] 2019-03-03 17:28:28 The latest check point (for incremental): '292475034'
mariabackup: Stopping log copying thread.[00] 2019-03-03 17:28:28 >> log scanned up to (292475043)

[00] 2019-03-03 17:28:29 >> log scanned up to (292475043)
[00] 2019-03-03 17:28:29 Executing UNLOCK TABLES
[00] 2019-03-03 17:28:29 All tables unlocked
[00] 2019-03-03 17:28:29 Copying ib_buffer_pool to /tmp/backup/ib_buffer_pool
[00] 2019-03-03 17:28:29         ...done
[00] 2019-03-03 17:28:29 Backup created in directory '/tmp/backup/'
[00] 2019-03-03 17:28:29 Writing backup-my.cnf
[00] 2019-03-03 17:28:29         ...done
[00] 2019-03-03 17:28:29 Writing xtrabackup_info
[00] 2019-03-03 17:28:29         ...done
[00] 2019-03-03 17:28:29 Redo log (from LSN 292475034 to 292475043) was copied.
[00] 2019-03-03 17:28:29 completed OK!

real    0m2.293s
user    0m0.034s
sys     0m0.578s

/tmp/backup にデータがバックアップされますので,

/tmp/backup:
[root@mdb102 backup]# ls -lah
-rw-r-----.  1 root root  16K Mar  3 17:28 aria_log.00000001
-rw-r-----.  1 root root   52 Mar  3 17:28 aria_log_control
-rw-r-----.  1 root root  297 Mar  3 17:28 backup-my.cnf
drwx------.  2 root root 4.0K Mar  3 17:28 employees
-rw-r-----.  1 root root  938 Mar  3 17:28 ib_buffer_pool
-rw-r-----.  1 root root  12M Mar  3 17:28 ibdata1
-rw-r-----.  1 root root 2.5K Mar  3 17:28 ib_logfile0
drwx------.  2 root root 4.0K Mar  3 17:28 mysql
drwx------.  2 root root   20 Mar  3 17:28 performance_schema
drwx------.  2 root root   20 Mar  3 17:28 test
-rw-r-----.  1 root root  105 Mar  3 17:28 xtrabackup_checkpoints
-rw-r-----.  1 root root  431 Mar  3 17:28 xtrabackup_info

tar + gzip 等でアーカイブし,リストア先サーバへコピーします。

MariaDB Server 10.3 へのリストア

リストア先のサーバでは,datadir システム変数(デフォルト値: /var/lib/mysql)で示されるデータディレクトリにあるデータを事前に削除しておく必要があります。datadir にデータが存在する場合,以下のようなエラーが発生します。

mariabackup --copy-back --target-dir /tmp/backup
[00] 2019-03-03 18:43:20 mariabackup based on MariaDB server 10.3.13-MariaDB Linux (x86_64)
[00] 2019-03-03 18:43:20 Original data directory /var/lib/mysql is not empty!

バックアップ元からコピーしたデータを target dir に展開し,datadir にあるデータを削除します。

# cd /tmp/
# tar xvf /root/backup190303.tgz
# systemctl stop mariadb
# cd /var/lib/mysql
# rm -fr *

実際のリストアの前に --prepare オプションによりデータの一貫性のチェックを行います。

mdb103# mariabackup --prepare --target-dir /tmp/backup
[00] 2019-03-03 16:34:54 mariabackup based on MariaDB server 10.3.13-MariaDB Linux (x86_64)
[00] 2019-03-03 16:34:54 cd to /tmp/backup/
[00] 2019-03-03 16:34:54 This target seems to be not prepared yet.
[00] 2019-03-03 16:34:54 mariabackup: using the following InnoDB configuration for recovery:
[00] 2019-03-03 16:34:54 innodb_data_home_dir = .
[00] 2019-03-03 16:34:54 innodb_data_file_path = ibdata1:12M:autoextend
[00] 2019-03-03 16:34:54 innodb_log_group_home_dir = .
[00] 2019-03-03 16:34:54 InnoDB: Using Linux native AIO
[00] 2019-03-03 16:34:54 Starting InnoDB instance for recovery.
[00] 2019-03-03 16:34:54 mariabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
2019-03-03 16:34:54 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-03-03 16:34:54 0 [Note] InnoDB: Uses event mutexes
2019-03-03 16:34:54 0 [Note] InnoDB: Compressed tables use zlib 1.2.7
2019-03-03 16:34:54 0 [Note] InnoDB: Number of pools: 1
2019-03-03 16:34:54 0 [Note] InnoDB: Using SSE2 crc32 instructions
2019-03-03 16:34:54 0 [Note] InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
2019-03-03 16:34:54 0 [Note] InnoDB: Completed initialization of buffer pool
2019-03-03 16:34:54 0 [Note] InnoDB: page_cleaner coordinator priority: -20
2019-03-03 16:34:54 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=292475034
[00] 2019-03-03 16:34:54 Last binlog file , position 0
[00] 2019-03-03 16:34:54 completed OK!

最後に completed OK! と表示されていれば問題ありません。

ここで,--copy-back オプションを付与して mariabackup を実行し,データをリストアします。

mdb103# time mariabackup --copy-back --target-dir /tmp/backup
[00] 2019-03-03 18:30:00 mariabackup based on MariaDB server 10.3.13-MariaDB Linux (x86_64)
[01] 2019-03-03 18:30:00 Copying ibdata1 to /var/lib/mysql/ibdata1
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/innodb_table_stats.ibd to /var/lib/mysql/mysql/innodb_table_stats.ibd
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/innodb_index_stats.ibd to /var/lib/mysql/mysql/innodb_index_stats.ibd
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/gtid_slave_pos.ibd to /var/lib/mysql/mysql/gtid_slave_pos.ibd
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/db.frm to /var/lib/mysql/mysql/db.frm
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/db.MYI to /var/lib/mysql/mysql/db.MYI
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./mysql/db.MYD to /var/lib/mysql/mysql/db.MYD
[01] 2019-03-03 18:30:00         ...done
...中略...
[01] 2019-03-03 18:30:00 Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
[01] 2019-03-03 18:30:00         ...done
[01] 2019-03-03 18:30:00 Copying ./xtrabackup_binlog_pos_innodb to /var/lib/mysql/xtrabackup_binlog_pos_innodb
[01] 2019-03-03 18:30:00         ...done
[00] 2019-03-03 18:30:00 completed OK!

real    0m0.656s
user    0m0.003s
sys     0m0.625s

バックアップ時は 2.3秒 ほど時間がかかっていましたが,リストアは 0.7 秒 ほどで完了しています。
リストアした直後はデータファイル/ディレクトリの owner が mariabackup を実行したユーザになっていますので,mysql:mysql に変更し,mariadb service を起動します。

# cd /var/lib/mysql
# chown -R mysql:mysql .
# systemctl start mariadb

リストアしたデータの確認

A sample MySQL database の test_employees_sha.sql を用いてバックアップしたデータが正常にリストアされているか確認します。
なお,test_employees_sha.sql は BLACKHOLE ストレージエンジンを用いていますので,双方の MariaDB Server 上で

INSTALL SONAME 'ha_blackhole';

を実行し,BLACKHOLE を利用可能にしておく必要があります。

バックアップ元での確認

まず,test_db ディレクトリにて,employees データベースに対して test_employees_sha.sql を実行します。

mdb102# cd test_db
mdb102# mysql employees
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [employees]> source test_employees_sha.sql
Database changed
+----------------------+
| INFO                 |
+----------------------+
| TESTING INSTALLATION |
+----------------------+
...中略
+--------------+------------------+------------------------------------------+
| table_name   | found_records    | found_crc                                |
+--------------+------------------+------------------------------------------+
| employees    |           300024 | 4d4aa689914d8fd41db7e45c2168e7dcb9697359 |
| departments  |                9 | 4b315afa0e35ca6649df897b958345bcb3d2b764 |
| dept_manager |               24 | 9687a7d6f93ca8847388a42a6d8d93982a841c6c |
| dept_emp     |           331603 | d95ab9fe07df0865f592574b3b33b9c741d9fd1b |
| titles       |           443308 | d12d5f746b88f07e69b9e36675b6067abb01b60e |
| salaries     |          2844047 | b5a1785c27d75e33a4173aaa22ccf41ebd7d4a9f |
+--------------+------------------+------------------------------------------+

+--------------+---------------+-----------+
| table_name   | records_match | crc_match |
+--------------+---------------+-----------+
| employees    | OK            | ok        |
| departments  | OK            | ok        |
| dept_manager | OK            | ok        |
| dept_emp     | OK            | ok        |
| titles       | OK            | ok        |
| salaries     | OK            | ok        |
+--------------+---------------+-----------+

+------------------+
| computation_time |
+------------------+
| 00:00:10         |
+------------------+

+---------+--------+
| summary | result |
+---------+--------+
| CRC     | OK     |
| count   | OK     |
+---------+--------+

すべてのテーブルのレコード数,チェックサムが一致しています。

リストア先での確認

同様にリストア先でもレコード数,チェックサムの確認を実施します。

mdb103# cd /root/test_db
mdb103# mysql employees
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.13-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [employees]> INSTALL SONAME 'ha_blackhole';
Query OK, 0 rows affected (0.001 sec)

MariaDB [employees]> source test_employees_sha.sql
Database changed
+----------------------+
| INFO                 |
+----------------------+
| TESTING INSTALLATION |
+----------------------+
1 row in set (0.000 sec)
...中略
+--------------+------------------+------------------------------------------+
| table_name   | found_records    | found_crc                                |
+--------------+------------------+------------------------------------------+
| employees    |           300024 | 4d4aa689914d8fd41db7e45c2168e7dcb9697359 |
| departments  |                9 | 4b315afa0e35ca6649df897b958345bcb3d2b764 |
| dept_manager |               24 | 9687a7d6f93ca8847388a42a6d8d93982a841c6c |
| dept_emp     |           331603 | d95ab9fe07df0865f592574b3b33b9c741d9fd1b |
| titles       |           443308 | d12d5f746b88f07e69b9e36675b6067abb01b60e |
| salaries     |          2844047 | b5a1785c27d75e33a4173aaa22ccf41ebd7d4a9f |
+--------------+------------------+------------------------------------------+

+--------------+---------------+-----------+
| table_name   | records_match | crc_match |
+--------------+---------------+-----------+
| employees    | OK            | ok        |
| departments  | OK            | ok        |
| dept_manager | OK            | ok        |
| dept_emp     | OK            | ok        |
| titles       | OK            | ok        |
| salaries     | OK            | ok        |
+--------------+---------------+-----------+

+------------------+
| computation_time |
+------------------+
| 00:00:08         |
+------------------+

+---------+--------+
| summary | result |
+---------+--------+
| CRC     | OK     |
| count   | OK     |
+---------+--------+

正常にリストアされていることが確認できました。

まとめ

Percona XtraBackup から fork された MariaDB Server 用バックアップツール,Mariabackup の基本的な利用方法について解説させて頂きました。
なお,MariaDB ColumnStore 専用の columnstoreBackup / columnstoreRestore に関しましては,BSL 1.1 でライセンスされていますので,ご留意願います。