utamt engineer blog

アプリケーション開発について学んだことの備忘録です。

MongoDBのバックアップ/リストア

Node.jsからデータを投入

db.collection.initializeOrderedBulkOp()を使う。

const MongoClient = require("mongodb").MongoClient;

const url = "mongodb://user:password@127.0.0.1:27017/sample";  // db名: "sample"

const connectOption = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
}

MongoClient.connect(url, connectOption, (err, client) => {
  if (err) {
    return;
  }

  const db = client.db("sample");
  
  const bulk = db.collection("products").initializeOrderedBulkOp();   // collection名: "products"
  bulk.insert({ name: "pen", price: 120 });
  bulk.insert({ name: "note", price: 120 });
  bulk.insert({ name: "eraser", price: 100 });
  bulk.insert({ name: "paste", price: 180 });
  bulk.insert({ name: "ciseaux", price: 320 });
  bulk.insert({ name: "cellophanetape", price: 80 });
  bulk.execute((err, result) => {
    client.close();
  });
});

登録されたか確認する

$ mongo
...
> use sample
...
> db.auth({ user: "user", pwd: "password" })
...
> db.products.find()
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad569"), "name" : "pen", "price" : 120 }
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad56a"), "name" : "note", "price" : 120 }
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad56b"), "name" : "eraser", "price" : 100 }
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad56c"), "name" : "paste", "price" : 180 }
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad56d"), "name" : "ciseaux", "price" : 320 }
{ "_id" : ObjectId("5ebb8d1e24bd920f0a3ad56e"), "name" : "cellopharentape", "price" : 80 }

bson形式でバックアップ

バックアップ

$ mongodump -u user -p password -d sample -o /home/vagrant/workspace/mongo01/backup
2020-05-13T15:24:24.439+0900	writing sample.products to 
2020-05-13T15:24:24.458+0900	done dumping sample.products (6 documents)

リストア

$ mongorestore -u user1 --authenticationDatabase sample --nsInclude sample.* /home/vagrant/workspace/mongo01/backup
Enter password:

2020-05-13T15:26:39.243+0900	preparing collections to restore from
2020-05-13T15:26:39.250+0900	reading metadata for sample.products from /home/vagrant/workspace/mongo01/backup/sample/products.metadata.json
2020-05-13T15:26:39.290+0900	restoring sample.products from /home/vagrant/workspace/mongo01/backup/sample/products.bson
2020-05-13T15:26:39.293+0900	no indexes to restore
2020-05-13T15:26:39.294+0900	finished restoring sample.products (6 documents, 0 failures)
2020-05-13T15:26:39.295+0900	6 document(s) restored successfully. 0 document(s) failed to restore.

アーカイブ形式でバックアップ

バックアップ

$ mongodump -u user1 -p password -d sample --gzip --archive=/home/vagrant/workspace/mongo01/backup/archive.dump
2020-05-13T15:37:17.867+0900	writing sample.products to archive '/home/vagrant/workspace/mongo01/backup/archive.dump'
2020-05-13T15:37:17.873+0900	done dumping sample.products (6 documents)

リストア

$ mongorestore -u user1 --authenticationDatabase sample --drop --gzip --archive=/home/vagrant/workspace/mongo01/backup/archive.dump
Enter password:

2020-05-13T15:39:20.584+0900	preparing collections to restore from
2020-05-13T15:39:20.595+0900	reading metadata for sample.products from archive '/home/vagrant/workspace/mongo01/backup/archive.dump'
2020-05-13T15:39:20.635+0900	restoring sample.products from archive '/home/vagrant/workspace/mongo01/backup/archive.dump'
2020-05-13T15:39:20.638+0900	no indexes to restore
2020-05-13T15:39:20.638+0900	finished restoring sample.products (6 documents, 0 failures)
2020-05-13T15:39:20.638+0900	6 document(s) restored successfully. 0 document(s) failed to restore.