mongoDB数据的备份和恢复

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/benben_2015/article/details/85636233

mongodump

概要

mongodump是一个用于备份数据库内容的实用程序,mongodump可以从mongodmongos实例中导出数据。mongodump可以是mongostore的备份策略的一部分,用于基于查询的部分备份。但是,使用mongodumpmongorestore作为备份策略对于分片群集和副本集可能会有问题。

从系统命令行运行mongodump,而不是mongo shell

使用

mongodump --db test --collection users运行后,默认会在你当前工作目录的dump文件夹下,生成一个名为test的文件夹。其中一个是bson数据,另一个是集合元数据,两个文件的格式都是json

——dump  
     |——test     
           |——users.bson    
           |——users.metadata

假设数据库test下,有集合userscompany。那么如果想排除一些,只转储剩下的集合。就需要用到参数--excludeCollection,例如:

mongodump --db test --excludeCollection=users

那么test文件夹中只会保存company.bsoncompany.metadata文件。
当要排除多个集合时,参数--excludeCollection就需要填写几次,例如:

mongodump --db test --excludeCollection=users --excludeCollection=company

之前说到,默认情况下,会将备份文件放到当前工作目录的dump目录下。但是如果你想重新设置一个目录,就需要用到--out参数。例如:

mongodump --db test --collection users --out ../mongodump2018-12-26

执行后,会在你当前目录的父目录下的mongodump2018-12-26文件夹中,生成备份文件。
如果需要在备份时对文件进行压缩,可以使用--gzip选项,那么生成的两个文件,将会是经过压缩的文件。例如:

mongodump --db test --collection users --gzip

如果想将数据保存成ARCHIVE文件,则需要使用--archive选项,例如:

mongodump --db test --collection users --archive=users20181226.archive

执行成功后,会在你当前目录下,生成一个users20181226.archive文件。但是这些文件如果需要恢复备份的数据,就要用到mongorestore

mongorestore

概要

mongorestore程序将数据从mongodump创建的二进制数据库备份恢复或加载到mongodmongos实例中。mongorestore可以创建一个新的数据库或者将数据添加到已经存在的数据库中。但是,mongorestore扮演的角色类似插入操作,而不是更新操作。即如果有相同内容,它也会进行插入,而不会重写文档。

从系统命令行运行mongodump,而不是mongo shell

特性

mongorestore会重新创建mongodump记录的索引。默认的索引是_id,所以不需要重新载入。但是当你载入的数据的默认索引有重复,那么就会出现错误。例如,上面生成的test20181226.archive文件加载到mongos中,会出现下面的错误:

D:\mongodb\bin>mongorestore --archive=test.20181226.archive
2018-12-26T12:36:14.110+0800    preparing collections to restore from
2018-12-26T12:36:14.129+0800    reading metadata for test.users from archive 'test.20181226.archive'
2018-12-26T12:36:14.129+0800    restoring test.users from archive 'test.20181226.archive'
2018-12-26T12:36:14.145+0800    error: multiple errors in bulk operation:  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('5c21fae76818ba7cb05df771') }  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('5c21fb136818ba7cb05df772') }
2018-12-26T12:36:14.145+0800    no indexes to restore
2018-12-26T12:36:14.145+0800    finished restoring test.users (2 documents)
2018-12-26T12:36:14.146+0800    done

正确执行后,会是下面的提示:

D:\mongodb\bin>mongorestore --archive=test.20181226.archive
2018-12-26T12:59:39.470+0800    preparing collections to restore from
2018-12-26T12:59:39.489+0800    reading metadata for test.users from archive 'test.20181226.archive'
2018-12-26T12:59:39.489+0800    restoring test.users from archive 'test.20181226.archive'
2018-12-26T12:59:39.495+0800    no indexes to restore2018-12-26T12:59:39.495+0800    finished restoring test.users (2 documents)
2018-12-26T12:59:39.495+0800    done

参考文章

  1. mongodump
  2. mongorestore

猜你喜欢

转载自blog.csdn.net/benben_2015/article/details/85636233