Docs Menu
Docs Home
/
MongoDB データベース ツール
/

mongorestore の例

項目一覧

  • アクセス制御による復元
  • コレクションの復元
  • Queryable Encryption が有効なコレクションの復元
  • ワイルド カードを使用したコレクションの復元
  • 復元中のコレクション名前空間の変更
  • データベースのコピー/クローン
  • アーカイブ ファイルから復元
  • 圧縮データから復元
  • 時系列コレクションの復元
  • AWS IAM 認証情報を使用して MongoDB Atlas クラスターに接続
  • コンシステントバックアップ ファイルの作成と復元
  • 詳細

このページでは mongorestoreの例を示します。

mongo shell ではなく、システム コマンドラインから mongorestore を実行します。

次の例では、 mongorestore/opt/backup/mongodump-2011-10-24から ホストmongodb1.example.net上のポート27017で実行されているmongodインスタンスに復元します。 --uri string ではユーザーのパスワードが省略され、 mongorestoreでパスワードの入力を求めるようにします。

mongorestore --uri="mongodb://user@mongodb1.example.net:27017/?authSource=admin" /opt/backup/mongodump-2011-10-24

または、 --host--port--username--authenticationDatabaseを使用してホスト、ポート、ユーザー名、認証データベースを指定することもできます。 --passwordを省略すると、 mongorestoreでパスワードの入力を求められます。

mongorestore --host=mongodb1.example.net --port=27017 --username=user --authenticationDatabase=admin /opt/backup/mongodump-2011-10-24

特定のコレクションを復元するには、コレクションの完全な名前空間( <database>.<collection> )を渡して、 --nsIncludeを使用します。

次の例では、 dump/ディレクトリにある対応するファイルから、データベースtest内のpurchaseordersという名前のコレクションを復元します。

mongorestore --nsInclude=test.purchaseorders dump/

mongorestore は、復元されたドキュメント数を含む結果が出力します。

2019-06-28T19:23:42.858-0400 preparing collections to restore from
2019-06-28T19:23:42.858-0400 reading metadata for test.purchaseorders from dump/test/purchaseorders.metadata.json
2019-06-28T19:23:42.893-0400 restoring test.purchaseorders from dump/test/purchaseorders.bson
2019-06-28T19:23:42.896-0400 restoring indexes for collection test.purchaseorders from metadata
2019-06-28T19:23:42.991-0400 finished restoring test.purchaseorders (6 documents, 0 failures)
2019-06-28T19:23:42.991-0400 6 document(s) restored successfully. 0 document(s) failed to restore.

dump/ ディレクトリに、指定された名前空間に対応するデータファイルが含まれていない場合、データは復元されません。

2019-07-08T14:39:57.121-0400. preparing collections to restore from
2019-07-08T14:39:57.121-0400 0 document(s) restored successfully. 0 document(s) failed to restore.

あるいは、 --db--collection.bsonファイルを使用して特定のコレクションを復元することもできます。

mongorestore --db=test --collection=purchaseorders dump/test/purchaseorders.bson
2019-06-30T12:21:44.777-0400 checking for collection data in dump/test/purchaseorders.bson
2019-06-30T12:21:44.779-0400 reading metadata for test.purchaseorders from dump/test/purchaseorders.metadata.json
2019-06-30T12:21:44.813-0400 restoring test.purchaseorders from dump/test/purchaseorders.bson
2019-06-30T12:21:44.881-0400 restoring indexes for collection test.purchaseorders from metadata
2019-06-30T12:21:44.987-0400 finished restoring test.purchaseorders (6 documents, 0 failures)
2019-06-30T12:21:44.987-0400 6 document(s) restored successfully. 0 document(s) failed to restore.

Queryable Encryption は暗号化されたコレクション内のドキュメントに__safeContent__フィールドを追加し、ドキュメント検証が無効になっていない限り、それらのドキュメントに対する挿入操作と更新操作をブロックします。 暗号化されたフィールドを含むコレクションを復元するには、 --bypassDocumentValidationを使用します。

mongodump は、Queryable Encryption が有効なコレクションの関連メタデータコレクションをエクスポートします。mongorestore を実行すると、これらのコレクションも復元されます。

mongorestore --db=test --collection=personaldata dump/test/personaldata.bson --bypassDocumentValidation

--nsIncludeと では、--nsExclude ワイルドカード としてアスタリスク を使用して、復元操作に含める、または除外する 名前空間 の指定がサポートされています。

次の例では、指定された名前空間パターンに一致する、現在のディレクトリのdump/サブディレクトリ内のドキュメントを復元します。 --nsIncludeステートメントではtransactionsデータベース内のドキュメントのみを復元するように指定し、 --nsExcludeでは名前が_devで終わるコレクションを除外するようにmongorestoreに指示します。 mongorestoreは、ポート27017のローカルホスト インターフェースで実行されているmongodインスタンスにデータを復元します。

mongorestore --nsInclude='transactions.*' --nsExclude='transactions.*_dev' dump/

復元するコレクションの名前空間を変更するには、 --nsFrom--nsToオプションを使用します。

--nsFrom--nsToオプションは、ワイルド カードとしてアスタリスク を使用することサポートし、置換で使用する「ワイルドカード」変数をドル記号で区切るのをサポートしています。

mongodump を使用して dump/ ディレクトリにエクスポートしたデータベースdata を検討します。data データベースには以下のコレクションがあります。

  • sales_customer1

  • sales_customer2

  • sales_customer3

  • users_customer1

  • users_customer2

  • users_customer3

--nsFrom--nsToを使用すると、異なる名前空間にデータを復元できます。 次の操作

  • data データベース内の sales_<customerName> コレクションを <customerName> データベース内の sales コレクションに復元

  • users_<customerName> コレクションを <customerName> データベースの users コレクションに復元

mongorestore --nsInclude="data.*" --nsFrom="data.$prefix$_$customer$" --nsTo="$customer$.$prefix$"

MongoDB バージョン 4.2 以降では、非推奨の copydb コマンドと clone コマンドが削除されます。

別の方法として、ユーザーはmongodumpmongorestoremongorestoreオプション--nsFrom--nsToとともに)を使用できます。

たとえば、test データベースをデフォルト ポート 27017 で実行中のローカル インスタンスから同じインスタンス上の examples データベースにコピーするには、次の操作を実行します。

  1. mongodump を使用して、test データベースをアーカイブ mongodump-test-db にダンプします。

    mongodump --archive="mongodump-test-db" --db=test
  2. アーカイブから(データベース名を変更したうえで)復元するには、mongorestore--nsFrom および --nsToとともに使用します。

    mongorestore --archive="mongodump-test-db" --nsFrom="test.*" --nsTo="examples.*"

Tip

必要に応じて、URI またはホスト、ユーザー名、パスワード、認証データベースの指定など、追加のオプションを含めます。

アーカイブ ファイルから復元するには、新しい --archive オプションとアーカイブ ファイル名を指定して mongorestore を実行します。

mongorestore --archive=test.20150715.archive

アーカイブ ファイルから復元するには、新しい --archive オプションとアーカイブ ファイル名を指定して mongorestore を実行します。次の操作の例では、ファイル test.20150715.archive から test データベースを復元します。

mongorestore --archive=test.20150715.archive --nsInclude="test.*"

mongorestore は、 mongodumpによって作成された圧縮ファイルまたは圧縮アーカイブ ファイルから復元できます。

圧縮ファイルを含むダンプ ディレクトリから復元するには、 --gzipオプションを指定してmongorestoreを実行します。 たとえば、次の操作は、デフォルトのdumpディレクトリにある圧縮ファイルからtestデータベースを復元します。

mongorestore --gzip --nsInclude="test.*" dump/

圧縮されたアーカイブ ファイルから復元するには、 --gzipオプションと--archiveオプションを指定してmongorestoreを実行します。 次の操作は、アーカイブ ファイルtest.20150715.gzからtestデータベースを復元します。

mongorestore --gzip --archive=test.20150715.gz --nsInclude="test.*"

復元するコレクションの名前空間を変更するには、 --nsFrom } オプションと--nsToオプションを--gzipオプションとともに使用します。

mongorestore --gzip --nsFrom="data.$prefix$_$customer$" --nsTo="$customer$.$prefix$"

mongosh を使用して時系列コレクションを作成します。この例では、デフォルトの test データベースを使用します。

db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}
}
)

時系列ドキュメントをコレクションに挿入します。

db.weather.insertMany( [
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
"temp": 12
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T04:00:00.000Z"),
"temp": 11
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T08:00:00.000Z"),
"temp": 11
}
] )

ターミナルから mongodump を使用して時系列コレクションを dump/test ディレクトリにエクスポートします。このコマンドは system.buckets.weather.bsonweather.metadata.json をディレクトリに追加します。

mongodump --db=test

mongorestore を使用してデータを mongorestore.weather 名前空間に復元します。

mongorestore --host localhost --port 27017 --nsFrom="test.*" --nsTo="mongorestore.*" dump/

注意

system.buckets.weather.bson ファイル単体では復元することはできません。そのような操作を試みるとエラーが発生します。

バージョン 100.1.0 の新機能

Amazon Web ServicesIAM MongoDB Atlas認証情報 による認証をサポートするように構成済みの connection stringクラスターに接続するには、mongorestore は、次のように提供します。

mongorestore 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' <other options>

AtlasAmazon Web Servicesこの方法で IAM 認証情報を使用してMONGODB-AWSauthentication mechanism $externalauthSourceに接続するには、この例に示すように、 と が使用されます。

AWS セッショントークンを使用する場合 同様に、AWS_SESSION_TOKEN authMechanismProperties 値を次のとおり指定します。

mongorestore 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>' <other options>

注意

AWS アクセスキー ID、シークレット アクセス キー、またはセッション トークンに次の文字が含まれている場合。

: / ? # [ ] @

上記文字はパーセント エンコーディングを使用して変換される必要があります。

あるいは、次のように、 、 、 --usernameオプションを使用して、Amazon Web Services IDstring--passwordアクセスキー--awsSessionToken 、シークレットアクセスキー、およびオプションのセッショントークンをそれぞれ接続 の外部で提供することもできます。

mongorestore 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' --username <aws access key id> --password <aws secret access key> --awsSessionToken <aws session token> <other options>

コマンドライン パラメーターとして指定する場合、これら 3 つのオプションにパーセント エンコーディングは必要ありません。

標準のAmazon Web Services IAM 環境変数 を使用してプラットフォーム上でこれらの認証情報を設定することもできますmongorestoreを使用すると、MONGODB-AWSauthentication mechanism は次の環境変数をチェックします。

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_SESSION_TOKEN

設定されている場合、これらの認証情報を接続stringや明示的なオプションで指定する必要はありません。

注意

Amazon Web Services の環境変数を使用してこれらの値を指定する場合、これらの認証情報に対応する明示的オプションまたは接続文字列 オプションと混在させることはできません。アクセス キー ID シークレット アクセス キー(および使用する場合はセッション トークンも)の環境変数を使用、または代わりに明示的オプションもしくは接続文字列 オプションをそれぞれを指定します。

次の例では、bash shell でこれらの環境変数を設定しています。

export AWS_ACCESS_KEY_ID='<aws access key id>'
export AWS_SECRET_ACCESS_KEY='<aws secret access key>'
export AWS_SESSION_TOKEN='<aws session token>'

他の shell で環境変数を設定する構文は異なります。詳細については、ご使用のプラットフォームのドキュメントを参照してください。

次のコマンドで、これらの環境変数が設定されているかどうかを確認できます。

env | grep AWS

設定が完了すると、次の例では、これらの環境変数を使用して MongoDB Atlas クラスターに接続します。

mongorestore 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' <other options>

oplogエントリを使用して一貫性のあるmongodumpバックアップ ファイルを作成するには、 mongodump --oplogオプションを使用します。 バックアップ ファイルからデータを復元するには、 mongorestore --oplogReplayオプションを使用します。

oplogには、データベース書込み (write) 操作の履歴が含まれます。

mongodump 出力:

  • コレクション ドキュメント、メタデータ、オプション。

  • インデックスの定義。

  • --oplogが指定されている場合に、 mongodumpの実行中に発生する書き込み。

mongodump --oplog は、 mongodump出力ディレクトリの最上位にoplog.bsonという名前のファイルを作成します。 ファイルには、 mongodumpの実行中に発生する書込み操作が含まれています。 mongodumpが完了した後に発生する書込みは ファイルに記録されません。

mongodumpを使用してシャーディングされたクラスターをバックアップするには、「データベース ダンプを使用して自己管理型シャーディングされたクラスターをバックアップする 」を参照してください。

oplog.bsonファイルから oplog エントリを復元するには、 mongorestore --oplogReplayを使用します。 データベースが最新で、 mongodumpの実行中に発生したすべての書き込みが含まれていることを確認するには、 mongodump --oplogmongorestore --oplogReplayと併用します。

戻る

動作