ユーザ用ツール

サイト用ツール


mongodb:dump_restore
  • テスト環境にバックアップ元、バックアップ先の二つのサービスを起動する
# /usr/bin/mongod --journal --port 27017 --logappend --logpath /data/mongo1/logs/mongod.log --pidfilepath /data/mongo1/logs/mongod.pid --dbpath /data/mongo1/db --fork
# /usr/bin/mongod --journal --port 27018 --logappend --logpath /data/mongo2/logs/mongod.log --pidfilepath /data/mongo2/logs/mongod.pid --dbpath /data/mongo2/db --fork
  • バックアップ元にログインする
# mongo --port 27017
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27017/test
> show dbs
local   (empty)
testdb  (empty)
  • テストデータを投入する
> use testdb
switched to db testdb
> for(var i=1;i<=150;i++) db.testdata.save({entryId:i, createdDatetime:new Date(), name:"test0"+i});
> db.testdata.findOne()
{
        "_id" : ObjectId("5109be7579ee8df58e8feca6"),
        "entryId" : 1,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.080Z"),
        "name" : "test01"
}
> db.testdata.count()
150
> use test002
switched to db test002
> for(var i=1;i<=150;i++) db.t002data.save({entryId:i, createdDatetime:new Date(), name:"test0"+i});
> db.t002data.findOne()
{
        "_id" : ObjectId("5109c85279ee8df58e8fed3c"),
        "entryId" : 1,
        "createdDatetime" : ISODate("2013-01-31T01:26:42.006Z"),
        "name" : "test01"
}
> db.t002data.count()
150
  • バックアップを実行する
# mongodump --help
Export MongoDB data to BSON files.

options:
  --help                   produce help message
  -v [ --verbose ]         be more verbose (include multiple times for more 
                           verbosity e.g. -vvvvv)
  --version                print the program's version and exit
  -h [ --host ] arg        mongo host to connect to ( <set name>/s1,s2 for 
                           sets)
  --port arg               server port. Can also use --host hostname:port
  --ipv6                   enable IPv6 support (disabled by default)
  -u [ --username ] arg    username
  -p [ --password ] arg    password
  --dbpath arg             directly access mongod database files in the given 
                           path, instead of connecting to a mongod  server - 
                           needs to lock the data directory, so cannot be used 
                           if a mongod is currently accessing the same path
  --directoryperdb         if dbpath specified, each db is in a separate 
                           directory
  --journal                enable journaling
  -d [ --db ] arg          database to use
  -c [ --collection ] arg  collection to use (some commands)
  -o [ --out ] arg (=dump) output directory or "-" for stdout
  -q [ --query ] arg       json query
  --oplog                  Use oplog for point-in-time snapshotting
  --repair                 try to recover a crashed database
  --forceTableScan         force a table scan (do not use $snapshot)
  • host:port のみの指定で、その環境のDB丸ごとバックアップする

実行時のカレントディレクトリに dumpディレクトリを作成し、その下にバックアップファイル作成

# cd /data/mongo1/
# mongodump -h localhost:27017 
connected to: localhost:27017
all dbs
DATABASE: test002    to     dump/test002
    test002.t002data to dump/test002/t002data.bson
             150 objects
    Metadata for test002.t002data to dump/test002/t002data.metadata.json
DATABASE: testdb     to     dump/testdb
    testdb.testcol to dump/testdb/testcol.bson
             2 objects
    Metadata for testdb.testcol to dump/testdb/testcol.metadata.json
    testdb.testdata to dump/testdb/testdata.bson
             150 objects
    Metadata for testdb.testdata to dump/testdb/testdata.metadata.json
You have new mail in /var/spool/mail/root
  • リストアを実行する
# mongorestore --help
Import BSON files into MongoDB.

usage: mongorestore [options] [directory or filename to restore from]
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more 
                          verbosity e.g. -vvvvv)
  --version               print the program's version and exit
  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)
  --port arg              server port. Can also use --host hostname:port
  --ipv6                  enable IPv6 support (disabled by default)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod database files in the given 
                          path, instead of connecting to a mongod  server - 
                          needs to lock the data directory, so cannot be used 
                          if a mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate 
                          directory
  --journal               enable journaling
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  --objcheck              validate object before inserting
  --filter arg            filter to apply before inserting
  --drop                  drop each collection before import
  --oplogReplay           replay oplog for point-in-time restore
  --oplogLimit arg        exclude oplog entries newer than provided timestamp 
                          (epoch[:ordinal])
  --keepIndexVersion      don't upgrade indexes to newest version
  --noOptionsRestore      don't restore collection options
  --noIndexRestore        don't restore indexes
  --w arg (=1)            minimum number of replicas per write
  • 環境によってはバックアップデータの指定に「dbpath」オプションを指定するとエラーになる
# mongorestore -h localhost:27018 --drop --dbpath /data/mongo1/dump
[tools] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/data/mongo1/dump/journal"
[tools] ERROR: don't know what to do with file [dump]
  • その場合は、「dbpath」オプションを指定せずにバックアップデータを指定する
# mongorestore -h localhost:27018 --drop /data/mongo1/dump
connected to: localhost:27018
/data/mongo1/dump/testdb/testdata.bson
    going into namespace [testdb.testdata]
     dropping
150 objects found
    Creating index: { key: { _id: 1 }, ns: "testdb.testdata", name: "_id_" }
/data/mongo1/dump/testdb/testcol.bson
    going into namespace [testdb.testcol]
     dropping
2 objects found
    Creating index: { key: { _id: 1 }, ns: "testdb.testcol", name: "_id_" }
/data/mongo1/dump/test002/t002data.bson
    going into namespace [test002.t002data]
     dropping
150 objects found
    Creating index: { key: { _id: 1 }, ns: "test002.t002data", name: "_id_" }
  • リストアが正しく行われたことを確認
# mongo --port 27018
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27018/test
> show dbs
local   (empty)
test002 0.203125GB
testdb  (empty)
> use testdb
switched to db testdb
> show collections
system.indexes
testcol
testdata
> db.testdata.count()
150
> db.testdata.findOne()
{
        "_id" : ObjectId("5109be7579ee8df58e8feca6"),
        "entryId" : 1,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.080Z"),
        "name" : "test01"
}
  • DBを指定してのバックアップ&リストア
# mongodump -h localhost:27017 -d testdb -o /data/mongo1/dump2/
# mongorestore -h localhost:27018 -d testdb --drop /data/mongo1/dump2/testdb
  • DB、コレクションを指定してのバックアップ&リストア
# mongodump -h localhost:27017 -d test002 -c t002data -o /data/mongo1/dump3/
# mongorestore -h localhost:27018 -d test002 -c t002data --drop /data/mongo1/dump3/test002/t002data.bson
mongodb/dump_restore.txt · 最終更新: 2013/01/31 05:36 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki