このページでは 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.
ワイルド カードを使用したコレクションの復元
--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_customer1sales_customer2sales_customer3users_customer1users_customer2users_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 コマンドが削除されます。
別の方法として、ユーザーはmongodumpとmongorestore ( mongorestoreオプション--nsFromと--nsToとともに)を使用できます。
たとえば、test データベースをデフォルト ポート 27017 で実行中のローカル インスタンスから同じインスタンス上の examples データベースにコピーするには、次の操作を実行します。
mongodumpを使用して、testデータベースをアーカイブmongodump-test-dbにダンプします。mongodump --archive="mongodump-test-db" --db=test アーカイブから(データベース名を変更したうえで)復元するには、
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.bson と weather.metadata.json をディレクトリに追加します。
mongodump --db=test
mongorestore を使用してデータを mongorestore.weather 名前空間に復元します。
mongorestore --host localhost --port 27017 --nsFrom="test.*" --nsTo="mongorestore.*" dump/
注意
system.buckets.weather.bson ファイル単体では復元することはできません。そのような操作を試みるとエラーが発生します。
AWS IAM 認証情報を使用して MongoDB Atlas クラスターに接続
バージョン 100.1.0 の新機能。
Amazon Web Services IAM 認証情報 connection stringによる認証をサポートするように構成されたMongoDBmongorestore Atlasクラスターに接続するには、次のような から を提供します。
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に接続するには、この例に示すように、 と が使用されます。
Amazon Web Servicesセッション トークンを使用する場合 同様に、AWS_SESSION_TOKENauthMechanismProperties 値を次のとおり指定します。
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 環境変数を使用してプラットフォーム上で設定することもできます。MONGODB-AWS authentication mechanism: を使用すると、mongorestore は次の環境変数をチェックします。
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_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の実行中に発生する書き込み。
oplog オプションで mongodump を使用する
mongodump --oplog は、 mongodump出力ディレクトリの最上位にoplog.bsonという名前のファイルを作成します。 ファイルには、 mongodumpの実行中に発生する書込み操作が含まれています。 mongodumpが完了した後に発生する書込みは ファイルに記録されません。
mongodumpを使用してシャーディングされたクラスターをバックアップするには、「データベース ダンプを使用して自己管理型シャーディングされたクラスターをバックアップする 」を参照してください。
oplogRePlay オプションで mongorestore を使用する
oplog.bsonファイルから oplog エントリを復元するには、 mongorestore --oplogReplayを使用します。 データベースが最新で、 mongodumpの実行中に発生したすべての書き込みが含まれていることを確認するには、 mongodump --oplogをmongorestore --oplogReplayと併用します。
詳細
クラスター間の移行のためのmongosync ユーティリティ
Atlas でのデータの移行またはインポート
Atlas でのデータのバックアップ、復元、アーカイブ