[摘要]一般来讲多节点的Mongo集群使用了replset几乎就无需进行数据备份了,monodb的副本集能够对故障进行自动恢复。关于mongodb的备份可以采用MMS(MongoDB Management Service) ,如果使用了tokumx的enterprise subscription版本的话可以使用hot-bakup feature(虽然我一直没有测试成功过),不过在临时做数据迁移的时候,可以使用mongo提供的mongodump/mongorestore。
mongodb的备份操作
./bin/mongodump --host <host:port> -d <database> -o <output directory> ./bin/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 --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --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 each db is in a separate directly (relevant only if dbpath specified) -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 deprecated
mongodb的恢复操作
.bin/mongorestore --host <host:port> -drop -d <collection> <directory or filename to restore from> ./bin/mongorestore --help Import BSON files into MongoDB. usage: /usr/local/mongodb/bin/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 --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --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 each db is in a separate directly (relevant only if dbpath specified) -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) --objcheck validate object before inserting (default) --noobjcheck don't validate object before inserting --filter arg filter to apply before inserting --drop drop each collection before import. RECOMMENDED, since only non-existent collections are eligible for the bulk load optimization. --oplogReplay deprecated --oplogLimit arg deprecated --keepIndexVersion deprecated --noOptionsRestore don't restore collection options --noIndexRestore don't restore indexes --w arg (=0) minimum number of replicas per write. WARNING, setting w > 1 prevents the bulk load optimization.
实际例子:
./bin/mongodump --host 192.168.100.101:27017 -d mydb -o /root/databadkup/backup connected to: 192.168.100.101:27017 Thu Mar 20 23:17:08.415 DATABASE: mydb to /tmp/databadkup/backup/mydb Thu Mar 20 23:17:08.416 mydb.system.indexes to /tmp/databadkup/backup/mydb/system.indexes.bson Thu Mar 20 23:17:08.417 56 objects Thu Mar 20 23:17:08.418 mydb.sequence to /tmp/databadkup/backup/mydb/sequence.bson Thu Mar 20 23:17:08.418 14 objects Thu Mar 20 23:17:08.418 Metadata for mydb.sequence to /tmp/databadkup/backup/mydb/sequence.metadata.json Thu Mar 20 23:17:08.419 mydb.user to /tmp/databadkup/backup/mydb/user.bson Thu Mar 20 23:17:08.419 13 objects Thu Mar 20 23:17:08.419 Metadata for mydb.user to /tmp/databadkup/backup/mydb/user.metadata.json Thu Mar 20 23:17:08.420 mydb.role to /tmp/databadkup/backup/mydb/role.bson Thu Mar 20 23:17:08.420 2 objects Thu Mar 20 23:17:08.420 Metadata for mydb.role to /tmp/databadkup/backup/mydb/role.metadata.json Thu Mar 20 23:17:08.421 mydb.menu to /tmp/databadkup/backup/mydb/menu.bson Thu Mar 20 23:17:08.421 21 objects Thu Mar 20 23:17:08.421 Metadata for mydb.menu to /tmp/databadkup/backup/mydb/menu.metadata.json Thu Mar 20 23:17:08.421 mydb.cp to /tmp/databadkup/backup/mydb/cp.bson Thu Mar 20 23:17:08.422 4 objects Thu Mar 20 23:17:08.422 Metadata for mydb.cp to /tmp/databadkup/backup/mydb/cp.metadata.json Thu Mar 20 23:17:08.422 mydb.category to /tmp/databadkup/backup/mydb/category.bson Thu Mar 20 23:17:08.422 14 objects Thu Mar 20 23:17:08.422 Metadata for mydb.category to /tmp/databadkup/backup/mydb/category.metadata.json Thu Mar 20 23:17:08.423 mydb.type to /tmp/databadkup/backup/mydb/type.bson Thu Mar 20 23:17:08.423 176 objects Thu Mar 20 23:17:08.424 Metadata for mydb.type to /tmp/databadkup/backup/mydb/type.metadata.json Thu Mar 20 23:17:08.424 mydb.channel to /tmp/databadkup/backup/mydb/channel.bson Thu Mar 20 23:17:08.424 14 objects Thu Mar 20 23:17:08.424 Metadata for mydb.channel to /tmp/databadkup/backup/mydb/channel.metadata.json Thu Mar 20 23:17:08.424 mydb.poster to /tmp/databadkup/backup/mydb/poster.bson Thu Mar 20 23:17:08.425 63 objects Thu Mar 20 23:17:08.425 Metadata for mydb.poster to /tmp/databadkup/backup/mydb/poster.metadata.json Thu Mar 20 23:17:08.425 mydb.special to /tmp/databadkup/backup/mydb/special.bson Thu Mar 20 23:17:08.426 46 objects Thu Mar 20 23:17:08.426 Metadata for mydb.special to /tmp/databadkup/backup/mydb/special.metadata.json Thu Mar 20 23:17:08.426 mydb.component to /tmp/databadkup/backup/mydb/component.bson Thu Mar 20 23:17:08.427 30 objects Thu Mar 20 23:17:08.427 Metadata for mydb.component to /tmp/databadkup/backup/mydb/component.metadata.json Thu Mar 20 23:17:08.427 mydb.searchRecommend to /tmp/databadkup/backup/mydb/searchRecommend.bson Thu Mar 20 23:17:08.427 6 objects Thu Mar 20 23:17:08.427 Metadata for mydb.searchRecommend to /tmp/databadkup/backup/mydb/searchRecommend.metadata.json Thu Mar 20 23:17:08.427 mydb.searchWord to /tmp/databadkup/backup/mydb/searchWord.bson Thu Mar 20 23:17:08.428 11 objects Thu Mar 20 23:17:08.428 Metadata for mydb.searchWord to /tmp/databadkup/backup/mydb/searchWord.metadata.json Thu Mar 20 23:17:08.428 mydb.mydbTemp to /tmp/databadkup/backup/mydb/mydbTemp.bson Thu Mar 20 23:17:08.435 1014 objects Thu Mar 20 23:17:08.435 Metadata for mydb.mydbTemp to /tmp/databadkup/backup/mydb/mydbTemp.metadata.json Thu Mar 20 23:17:08.436 mydb.region to /tmp/databadkup/backup/mydb/region.bson Thu Mar 20 23:17:08.436 79 objects Thu Mar 20 23:17:08.436 Metadata for mydb.region to /tmp/databadkup/backup/mydb/region.metadata.json Thu Mar 20 23:17:08.436 mydb.typemydbMapping to /tmp/databadkup/backup/mydb/typemydbMapping.bson Thu Mar 20 23:17:08.437 0 objects Thu Mar 20 23:17:08.437 Metadata for mydb.typemydbMapping to /tmp/databadkup/backup/mydb/typemydbMapping.metadata.json Thu Mar 20 23:17:08.437 mydb.mydb to /tmp/databadkup/backup/mydb/mydb.bson Thu Mar 20 23:17:11.003 Collection File Writing Progress: 729900/1175062 62% (objects) Thu Mar 20 23:17:11.790 1175062 objects Thu Mar 20 23:17:11.790 Metadata for mydb.mydb to /tmp/databadkup/backup/mydb/mydb.metadata.json Thu Mar 20 23:17:11.790 mydb.mydbGroup to /tmp/databadkup/backup/mydb/mydbGroup.bson Thu Mar 20 23:17:11.850 23519 objects Thu Mar 20 23:17:11.850 Metadata for mydb.mydbGroup to /tmp/databadkup/backup/mydb/mydbGroup.metadata.json Thu Mar 20 23:17:11.851 mydb.favorite to /tmp/databadkup/backup/mydb/favorite.bson Thu Mar 20 23:17:11.864 10209 objects Thu Mar 20 23:17:11.864 Metadata for mydb.favorite to /tmp/databadkup/backup/mydb/favorite.metadata.json Thu Mar 20 23:17:11.864 mydb.assemble to /tmp/databadkup/backup/mydb/assemble.bson Thu Mar 20 23:17:11.865 43 objects Thu Mar 20 23:17:11.865 Metadata for mydb.assemble to /tmp/databadkup/backup/mydb/assemble.metadata.json Thu Mar 20 23:17:11.865 mydb.batchBase to /tmp/databadkup/backup/mydb/batchBase.bson Thu Mar 20 23:17:11.865 5 objects Thu Mar 20 23:17:11.865 Metadata for mydb.batchBase to /tmp/databadkup/backup/mydb/batchBase.metadata.json Thu Mar 20 23:17:11.865 mydb.resultMailBase to /tmp/databadkup/backup/mydb/resultMailBase.bson Thu Mar 20 23:17:11.866 13 objects Thu Mar 20 23:17:11.866 Metadata for mydb.resultMailBase to /tmp/databadkup/backup/mydb/resultMailBase.metadata.json Thu Mar 20 23:17:11.866 mydb.regionmydbMapping to /tmp/databadkup/backup/mydb/regionmydbMapping.bson Thu Mar 20 23:17:11.866 0 objects Thu Mar 20 23:17:11.866 Metadata for mydb.regionmydbMapping to /tmp/databadkup/backup/mydb/regionmydbMapping.metadata.json .bin/mongorestore --host 192.168.100.101:29071 -drop -d mydb /tmp/databadkup/backup/* connected to: 192.168.100.101:29071 Thu Mar 20 23:23:25.851 /tmp/databadkup/backup/mydb/mydb.bson Thu Mar 20 23:23:25.851 going into namespace [mydb.mydb] Thu Mar 20 23:23:25.851 dropping Thu Mar 20 23:23:25.884 Created collection mydb.mydb with options: { "create" : "mydb" } Thu Mar 20 23:23:28.014 Progress: 15371566/863926684 1% (bytes) Thu Mar 20 23:23:31.031 Progress: 36047973/863926684 4% (bytes) Thu Mar 20 23:23:34.036 Progress: 56005099/863926684 6% (bytes) Thu Mar 20 23:23:37.026 Progress: 76426827/863926684 8% (bytes) Thu Mar 20 23:23:40.025 Progress: 96315768/863926684 11% (bytes) Thu Mar 20 23:23:43.042 Progress: 116548478/863926684 13% (bytes) Thu Mar 20 23:23:46.046 Progress: 136633276/863926684 15% (bytes) Thu Mar 20 23:23:49.013 Progress: 155826118/863926684 18% (bytes) Thu Mar 20 23:23:52.004 Progress: 176092864/863926684 20% (bytes) Thu Mar 20 23:23:55.019 Progress: 196686920/863926684 22% (bytes) Thu Mar 20 23:23:58.003 Progress: 213984567/863926684 24% (bytes) Thu Mar 20 23:24:01.012 Progress: 234161090/863926684 27% (bytes) Thu Mar 20 23:24:04.002 Progress: 252841525/863926684 29% (bytes) Thu Mar 20 23:24:07.027 Progress: 273761711/863926684 31% (bytes) Thu Mar 20 23:24:10.012 Progress: 292586032/863926684 33% (bytes) Thu Mar 20 23:24:13.004 Progress: 313932237/863926684 36% (bytes) Thu Mar 20 23:24:16.007 Progress: 333937050/863926684 38% (bytes) Thu Mar 20 23:24:19.000 Progress: 353311050/863926684 40% (bytes) Thu Mar 20 23:24:22.014 Progress: 374792448/863926684 43% (bytes) Thu Mar 20 23:24:25.007 Progress: 395856496/863926684 45% (bytes) Thu Mar 20 23:24:28.000 Progress: 415115672/863926684 48% (bytes) Thu Mar 20 23:24:31.005 Progress: 432907770/863926684 50% (bytes) Thu Mar 20 23:24:34.045 Progress: 454031303/863926684 52% (bytes) Thu Mar 20 23:24:37.012 Progress: 474161581/863926684 54% (bytes) Thu Mar 20 23:24:40.011 Progress: 493167341/863926684 57% (bytes) Thu Mar 20 23:24:43.002 Progress: 513367410/863926684 59% (bytes) Thu Mar 20 23:24:46.013 Progress: 533715626/863926684 61% (bytes) Thu Mar 20 23:24:49.006 Progress: 554497196/863926684 64% (bytes) Thu Mar 20 23:24:52.042 Progress: 573524567/863926684 66% (bytes) Thu Mar 20 23:24:55.005 Progress: 591610162/863926684 68% (bytes) Thu Mar 20 23:24:58.049 Progress: 612660990/863926684 70% (bytes) Thu Mar 20 23:25:01.121 Progress: 631485366/863926684 73% (bytes) Thu Mar 20 23:25:04.033 Progress: 651438578/863926684 75% (bytes) Thu Mar 20 23:25:07.003 Progress: 671837860/863926684 77% (bytes) Thu Mar 20 23:25:10.053 Progress: 689582678/863926684 79% (bytes) Thu Mar 20 23:25:13.014 Progress: 708138604/863926684 81% (bytes) Thu Mar 20 23:25:16.012 Progress: 729091065/863926684 84% (bytes) Thu Mar 20 23:25:19.038 Progress: 749942417/863926684 86% (bytes) Thu Mar 20 23:25:22.024 Progress: 770132450/863926684 89% (bytes) Thu Mar 20 23:25:25.010 Progress: 791027243/863926684 91% (bytes) Thu Mar 20 23:25:28.013 Progress: 812111239/863926684 94% (bytes) Thu Mar 20 23:25:31.004 Progress: 831830162/863926684 96% (bytes) Thu Mar 20 23:25:34.006 Progress: 847154350/863926684 98% (bytes) 11852061 objects found Thu Mar 20 23:25:36.421 Creating index: { key: { _id: 1 }, unique: true, ns: "mydb.mydb", name: "_id_", clustering: true } Thu Mar 20 23:25:36.584 Creating index: { key: { name: 1 }, ns: "mydb.mydb", name: "name_1" } Thu Mar 20 23:26:04.478 Creating index: { key: { typeIds: 1 }, ns: "mydb.mydb", name: "typeIds_1" } Thu Mar 20 23:26:53.921 Creating index: { key: { order: -1 }, ns: "mydb.mydb", name: "order_-1" } Thu Mar 20 23:27:00.360 Creating index: { key: { categoryId: 1, merge: 1, status: 1 }, ns: "mydb.mydb", name: "categoryId_1_merge_1_status_1" } Thu Mar 20 23:27:11.219 /tmp/databadkup/backup/mydb/assemble.bson Thu Mar 20 23:27:11.219 going into namespace [mydb.assemble] Thu Mar 20 23:27:11.219 dropping Thu Mar 20 23:27:11.386 Created collection mydb.assemble with options: { "create" : "assemble" } 43 objects found Thu Mar 20 23:27:11.387 Creating index: { key: { _id: 1 }, unique: true, ns: "mydb.assemble", name: "_id_", clustering: true } Thu Mar 20 23:27:11.388 /tmp/databadkup/backup/mydb/searchWord.bson ...
查询导入之后的总条数:
TokuMX mongo shell v1.4.0-mongodb-2.4.9 connecting to: 192.168.100.101:29071/test myset:PRIMARY> use mydb; switched to db mydb myset:PRIMARY> db.mydb.count(); 11852061 myset:PRIMARY> show dbs; admin (empty) local 3.109619140625GB myset 1.205718994140625GB