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

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

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 –type csv –headerline –file
-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 的样式.

http://www.cr173.com/html/20275_1.html

时间: 2024-08-29 21:24:24

MongoDB整库备份与还原以及单个collection备份、恢复的相关文章

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

mongodb数据库维护离不开必要的备份.恢复操作,而且一般不会出错,所以我们在使用的时候大部分时候使用备份和恢复操作就可以了 mongodump.exe备份的原理是通过一次查询获取当前服务器快照,并将快照写入磁盘中,因此这种方式保存的也不是实时的,因为在获取快照后,服务器还会有数据写入,为了保证备份的安全,同样我们还是可以利用fsync锁使服务器数据暂时写入缓存中. 高效开源数据库(mongodb)下载地址:http://www.jb51.net/softs/41751.html 备份前的检查

MySQL备份和还原系列一:备份类型

一.mysql备份类型 1.按照mysql服务器状态 cold    离线备份,读.写操作均中止 warm    仅可执行读操作 hot     读.写操作不受影响 2.按照数据一致性 consistent inconsistent 3.按照备份数据格式 logical     备份sql语句,在恢复的时候执行备份的sql语句实现数据库数据的重现 physical    文件系统层面直接拷贝数据文件,但真正备份的时候自然不是cp这么简单 4.数据存储方式 full            完全备份

MongoDB之整库备份还原单表collection备份还原

cd D:\MongoDB\bin 1整库备份: mongodump -h dbhost -d dbname -o dbdirectory -h:MongDB所在服务器地址,例如:127.0.0.1:27017 -d:需要备份的数据库实例,例如:wlwdb -o:备份的数据存放位置,例如:D:\MongoDB\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个wlwdb目录,这个目录里面存放该数据库实例的备份数据. eg: mongodump -h 127

mysql备份与还原

防伪码:志向不过是记忆的奴隶,生气勃勃地降生,但却很难成长. 一.mysqldump备份结合binlog日志恢复 MySQL 备份一般采取全库备份加日志备份的方式,例如每天执行一次全备份,每小时执行一 次二进制日志备份.这样在 MySQL 故障后可以使用全备份和日志备份将数据恢复到最后一个 二进制日志备份前的任意位置或时间. 1.binlog介绍 mysql的二进制日志记录着该数据库的所有增删改的操作日志(前提是要在自己的服务器上 开启binlog),还包括了这些操作的执行时间.为了显示这些二进

实现Mysql 备份与还原

前言:本篇主要讲 1.使用Mysqldump结合binlog日志备份与恢复 2.使用xtrabackup进行Mysql数据库备份与恢复 ################# mysqldump备份结合binlog日志恢复功能介绍 ##################### MySQL备份一般采取全库备份加日志备份的方式,例如每天执行一次全备份,每小时执行一次二进制日志备份.这样在MySQL故障后可以使用全备份和日志备份将数据恢复到最后一个二进制日志备份前的任意位置或时间. 1.binlog介绍 m

dump和restore备份和还原备份命令

继续记录自己的学习笔记,昨天学习的是备份和还原 课程大纲 1 备份概述 2 dump和restore命令 要做实验前请先查看是否安装了dump服务包 命令是:rmp -q dump 如果没有请安装 yum -y install dump 我们实验的目录如下 Linux 系统需要备份的数据 /root /home /vra/spool/mail /etc/ 安装服务的数据 apache需要备份的数据 1配置文件 2网页主目录 3日志文件 mysql需要备份的数据 1源码包安装的mysql:/usr

SQL Server 备份和还原全攻略

原文:SQL Server 备份和还原全攻略 一.知识点 完全备份: 备份全部选中的文件夹,并不依赖文件的存档属性来确定备份那些文件.(在备份过程中,任何现有的标记都被清除,每个文件都被标记为已备份,换言之,清除存档属性).完全备份也叫完整备份. 差异备份: 差异备份是针对完全备份:备份上一次的完全备份后发生变化的所有文件.(差异备份过程中,只备份有标记的那些选中的文件和文件夹.它不清除标记,即:备份后不标记为已备份文件,换言之,不清除存档属性). 增量备份: 增量备份是针对于上一次备份(无论是

达梦7备份与还原

达梦7备份与还原 前言 本文档介绍了达梦7的备份与还原相关操作.详细的文档,可以查看达梦安装目录下的文件doc/special/DM7备份与还原.pdf 基础概念 备份集 这个概念频繁出现在文档和命令中,一定要先熟悉. 备份集用来存放备份过程中产生的备份数据及备份信息.一个备份集对应了一次完整的备份*. 一般情况下, 一个备份集就是一个目录, 备份集包含一个或多个备份片文件,以及一个备份元数据文件. 下图展示了一个备份集 # tree db_full_2020_02_25 db_full_202

mysql备份和还原

MySQL 数据备份与还原 一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先查出需要备份的表的结构,再在文本文件中生成一个CREATE语句.然后,将表中的所有记录转换成一条INSERT语句.然后通过这些语句,就能够创建表并插入数据. 1.备份一个数据库 mysqldump基本语法: mysqldump -u username -p dbname