はじめに
バックアップやリストアをパラレルに実行できたりと、何かと便利な MySQL Shell ですが、過去の弊ブログでは、バックアップファイルの保存先に OCI のオブジェクトストレージを活用する方法をご紹介しました。
MySQL Shellリファレンスには、OCI のオブジェクトストレージだけではなく、Amazon Web Services S3 (以降 S3 と表記)に対してもテストされているということが記載されております。
ということで、S3にバックアップを保存し、そのバックアップからデータを復元できるか確認してみました。
検証サーバ
- Red Hat Enterprise Linux release 8.6
 - MySQL Server 8.0.31
 - MySQL Shell 8.0.31
 
検証
AWS CLIのインストール
まずは、サーバに AWS CLI をインストールします。
| 
					 1 2 3  | 
						# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" # unzip awscliv2.zip # ./aws/install  | 
					
AWS CLIの設定
インストール後は、CLIを使用する為の設定を行います。
| 
					 1 2 3 4 5  | 
						# aws configure AWS Access Key ID [None]: (略) AWS Secret Access Key [None]: (略) Default region name [None]: ap-northeast-1 (環境に合わせてご変更下さい) Default output format [None]: json  | 
					
念のため、設定された内容を確認しておきます。
| 
					 1 2 3 4 5 6 7 8  | 
						# cat ~/.aws/credentials [default] aws_access_key_id = (略) aws_secret_access_key = (略) # cat ~/.aws/config [default] region = ap-northeast-1 output = json  | 
					
S3 バケットの作成
CLI の設定が完了すれば、バックアップファイルを保存する S3バケット を作成します。
バケット名は mysqlsh としています。
| 
					 1 2  | 
						# aws s3 mb s3://mysqlsh make_bucket: mysqlsh  | 
					
※ 予め S3 への書き込み、読み込みが行えるようポリシーを付与しておく必要があります。
バックアップ
ここまでで準備は完了です。
それでは、早速 MySQL Shell でログインし、 util.dumpInstance を使用してバックアップを S3 に保存してみます。
OCI のオブジェクトストレージでは、osBucketName や osNamespace を設定しましたが、S3 を対象とする場合は、以下のリファレンスのオプションを指定するようです。
- MySQL Shell Reference / 11.5 Instance Dump Utility, Schema Dump Utility, and Table Dump Utility
(その他、同リファレンスページにスレッド数等の指定オプションがあり、S3 を対象としても指定可能ですので、適宜、オプションを付与して下さい。) 
試しに、MySQL のサンプルデータベースとなる sakila をロードしたMySQLサーバに対して、 第一引数には保存先となるバケット内のフォルダ名を、s3BucketName に保存先のバケット名を指定して実行します。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  | 
						 MySQL  localhost:33060+ ssl  JS > util.dumpInstance('sakila', {s3BucketName: "mysqlsh"}) Acquiring global read lock Global read lock acquired Initializing - done 1 out of 5 schemas will be dumped and within them 16 tables, 7 views, 6 routines, 6 triggers. 1 out of 4 users will be dumped. Gathering information - done All transactions have been started Locking instance for backup Global read lock has been released Writing global DDL files Writing users DDL Running data dump using 4 threads. NOTE: Progress information uses estimated values and may not be accurate. Writing schema metadata - done Writing DDL - done Writing table metadata - done Starting data dump 101% (47.27K rows / ~46.69K rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed Dump duration: 00:00:01s Total duration: 00:00:01s Schemas dumped: 1 Tables dumped: 16 Uncompressed data size: 3.03 MB Compressed data size: 715.30 KB Compression ratio: 4.2 Rows written: 47268 Bytes written: 715.30 KB Average uncompressed throughput: 2.79 MB/s Average compressed throughput: 657.86 KB/s  | 
					
正常終了しましたので、S3 のバケットを確認します。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12  | 
						# aws s3 ls s3://mysqlsh --recursive 2022-11-08 17:29:11       1338 sakila/@.done.json 2022-11-08 17:29:10        753 sakila/@.json 2022-11-08 17:29:10        240 sakila/@.post.sql 2022-11-08 17:29:10        240 sakila/@.sql 2022-11-08 17:29:10       1946 sakila/@.users.sql 2022-11-08 17:29:10       1886 sakila/sakila.json 2022-11-08 17:29:10      11331 sakila/sakila.sql 2022-11-08 17:29:10        672 sakila/sakila@actor.json 2022-11-08 17:29:10        902 sakila/sakila@actor.sql 2022-11-08 17:29:11       1904 sakila/sakila@actor@@0.tsv.zst ・・・(略)  | 
					
問題なく指定したバケットのフォルダ内にバックアップオブジェクトが保存されているようです。
次は、同一サーバの sakila データベースを削除して、先程のバックアップをロードしてみます。
ついでに、ロードする際に必要な設定 local_infile=ON を設定しておきます。
| 
					 1 2 3 4 5 6  | 
						 MySQL  localhost:33060+ ssl  JS > \sql Switching to SQL mode... Commands end with ;  MySQL  localhost:33060+ ssl  SQL > drop database sakila; Query OK, 23 rows affected (0.1979 sec)  MySQL  localhost:33060+ ssl  SQL > SET GLOBAL local_infile=ON; Query OK, 0 rows affected (0.0003 sec)  | 
					
それでは util.loadDump を使用してロードしてみます。
バックアップ時と同様に S3 のバケット名と保存先フォルダを指定しておきます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  | 
						 MySQL  localhost:33060+ ssl  SQL > \js Switching to JavaScript mode...  MySQL  localhost:33060+ ssl  JS > util.loadDump("sakila", {s3BucketName: "mysqlsh"}) Loading DDL and Data from AWS S3 bucket=mysqlsh, prefix='sakila' using 4 threads. Opening dump... Target is MySQL 8.0.31. Dump was produced from MySQL 8.0.31 Fetching dump data from remote location... Listing files - done Scanning metadata - done Checking for pre-existing objects... Executing common preamble SQL Executing DDL - done Executing view DDL - done Starting data load 100% (3.03 MB / 3.03 MB), 2.02 MB/s, 12 / 16 tables done Recreating indexes - done Executing common postamble SQL 16 chunks (47.27K rows, 3.03 MB) for 16 tables in 1 schemas were loaded in 4 sec (avg throughput 2.02 MB/s) 0 warnings were reported during the load.  MySQL  localhost:33060+ ssl  JS >  | 
					
正常にロードが完了したようなので、実際のテーブルとデータを確認してみましょう。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  | 
						 MySQL  localhost:33060+ ssl  JS > \sql Switching to SQL mode... Commands end with ;  MySQL  localhost:33060+ ssl  SQL > show tables from sakila; +----------------------------+ | Tables_in_sakila           | +----------------------------+ | actor                      | | actor_info                 | | address                    | | category                   | | city                       | | country                    | | customer                   | | customer_list              | | film                       | | film_actor                 | | film_category              | | film_list                  | | film_text                  | | inventory                  | | language                   | | nicer_but_slower_film_list | | payment                    | | rental                     | | sales_by_film_category     | | sales_by_store             | | staff                      | | staff_list                 | | store                      | +----------------------------+ 23 rows in set (0.0015 sec)  MySQL  localhost:33060+ ssl  SQL > SELECT * FROM sakila.actor LIMIT 5; +----------+------------+--------------+---------------------+ | actor_id | first_name | last_name    | last_update         | +----------+------------+--------------+---------------------+ |        1 | PENELOPE   | GUINESS      | 2006-02-15 04:34:33 | |        2 | NICK       | WAHLBERG     | 2006-02-15 04:34:33 | |        3 | ED         | CHASE        | 2006-02-15 04:34:33 | |        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 | |        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 | +----------+------------+--------------+---------------------+  | 
					
問題なく、ロードされているようです。
まとめ
MySQL Shell のユーティリティには、パラレルでデータのダンプ、ロードを行える機能があり、大いに活用できるツールであると思います。
また、バックアップのストレージとして、OCI のオブジェクトストレージだけでなく、S3 を使用することも可能ですので、機会があれば、触ってみて下さい。


		
		
			
			
			
			
			
			
			