侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MongoDB整库备份与还原以及单个collection备份、恢复方法

数据库  /  管理员 发布于 5年前   216

mongodump.exe备份的原理是通过一次查询获取当前服务器快照,并将快照写入磁盘中,因此这种方式保存的也不是实时的,因为在获取快照后,服务器还会有数据写入,为了保证备份的安全,同样我们还是可以利用fsync锁使服务器数据暂时写入缓存中。

高效开源数据库(mongodb)下载地址:///softs/41751.html

备份前的检查
> show dbs
MyDB 0.0625GB
admin (empty)
bruce 0.0625GB
local (empty)
test 0.0625GB
> use MyDB
switched to db MyDB
> db.users.find()
{ "_id" : ObjectId("4e290aa39a1945747b28f1ee"), "a" : 1, "b" : 1 }
{ "_id" : ObjectId("4e2cd2182a65c81f21566318"), "a" : 3, "b" : 5 }
>

整库备份:
mongodump -h dbhost -d dbname -o dbdirectory
-h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-d:需要备份的数据库实例,例如:test
-o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

mongodump的官方说明(可通过mongodump --help查看):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 ( /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)

整库恢复:
mongorestore -h dbhost -d dbname Cdirectoryperdb dbdirectory
-h:MongoDB所在服务器地址
-d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
Cdirectoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!
Cdrop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

mongorestore的官方说明(可通过mongorestore --help查看):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 ( /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

单个collection备份:
mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory
-h: MongoDB所在服务器地址
-d: 需要恢复的数据库实例
-c: 需要恢复的集合
-f: 需要导出的字段(省略为所有字段)
-o: 表示导出的文件名

mongoexport的官方说明(可通过mongoexport --help查看): --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 ( /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) -f [ --fields ] arg    comma separated list of field names e.g. -f  name,age --fieldFile arg      file with fields names - 1 per line -q [ --query ] arg    query filter, as a JSON string --csv           export to csv instead of json -o [ --out ] arg     output file; if not specified, stdout is used --jsonArray        output to a json array rather than one object per  line -k [ --slaveOk ] arg (=1) use secondaries for export if available, default  true --forceTableScan     force a table scan (do not use $snapshot)

单个collection恢复:
mongoimport -d dbhost -c collectionname Ctype csv Cheaderline Cfile
-type: 指明要导入的文件格式
-headerline: 批明不导入第一行,因为第一行是列名
-file: 指明要导入的文件路径

mongoimport的官方说明(可通过mongoimport --help查看): --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 ( /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) -f [ --fields ] arg   comma separated list of field names e.g. -f name,age --fieldFile arg     file with fields names - 1 per line --ignoreBlanks     if given, empty fields in csv and tsv will be ignored --type arg       type of file to import. default: json (json,csv,tsv) --file arg       file to import from; if not specified stdin is used --drop         drop collection first --headerline      CSV,TSV only - use first line as headers --upsert        insert or update objects that already exist --upsertFields arg   comma-separated fields for the query part of the upsert. You should make sure this is indexed --stopOnError      stop importing at first error rather than continuing --jsonArray       load a json array, not one item per line. Currently limited to 16MB.

其他导入与导出操作:

1. mongoimport -d my_mongodb -c user user.dat

参数说明:

-d 指明使用的库, 本例中为” my_mongodb”

-c 指明要导出的表, 本例中为”user”

可以看到导入数据的时候会隐式创建表结构

2. mongoexport -d my_mongodb -c user -o user.dat

参数说明:

-d 指明使用的库, 本例中为” my_mongodb”

-c 指明要导出的表, 本例中为”user”

-o 指明要导出的文件名, 本例中为”user.dat”

从上面可以看到导出的方式使用的是JSON 的样式.


  • 上一条:
    MongoDB常用命令小结
    下一条:
    MongoDB为用户设置访问权限
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客