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.0.0.1 -d wlwdb -o D:\MongoDB\data\dump

可以查看该备份目录:

mongodump --help

mongodump的官方说明:
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)

2整库恢复:

mongorestore -h dbhost -d dbname –directoryperdb dbdirectory

-h:MongoDB所在服务器地址

-d:需要恢复的数据库实例,例如:wlwdb(可以和备份时候的不一样)

–directoryperdb:备份数据所在位置,例如:D:\MongoDB\data\dump\wlwdb

–drop:恢复的时候,先删除当前数据,然后恢复备份的数据。

eg:

mongorestore -h 127.0.0.1 -d wlwdb –directoryperdb D:\MongoDB\data\dump\wlwdb

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

3单个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)

4单个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.
时间: 2024-08-15 06:53:26

MongoDB之整库备份还原单表collection备份还原的相关文章

oracle 11g expdp 备份解决空表无法备份

之前做oracle 备份用的都是exp,但exp在11g上存在一个问题,就是无法导出空表. 最近做oracle 数据迁移,需要将空表一同导出,经过搜索,找到了expdb 于是有了此文. 此文仅作记录其中问题,以及我个人对expdp 的理解 在使用expdp impdp之前,需要先建立目录对象,并赋予用户权限.这是因为expdp impdp只能通过DIRECTORY对象关系,将数据存入系统目录. 注:红色部分要替换成实际值 expdp 导出 创建DIRECTORY对象和OS PATH映射,并赋予权

mysql 针对单张表的备份与还原

A.MySQL 备份工具xtrabackup 的安装 1. percona 官方xtrabackup 的二进制版本:二进制版本解压就能用了. 2. 解压xtrabackup & 创建连接 tar -xzvf percona-xtrabackup-2.3.4-Linux-x86_64.tar.gz -C /usr/local/ ln -s /usr/local/percona-xtrabackup-2.3.4 /usr/local/xtrabackup 3. 设置PATH环境变量 export P

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

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

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

备份前的检查> show dbsMyDB 0.0625GBadmin (empty)bruce 0.0625GBlocal (empty)test 0.0625GB> use MyDBswitched to db MyDB> db.users.find(){ "_id" : ObjectId("4e290aa39a1945747b28f1ee"), "a" : 1, "b" : 1 }{ "_id&

Oracle 整库备份还原

http://www.mamicode.com/info-detail-2481866.html sql语句 system用户登陆 查看表空间和存放位置 select t1.name,t2.name from v$tablespace t1,v$datafile t2 where t1.ts# = t2.ts#; 查看所有表空间的大小 select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespa

从MySQL全库备份中恢复某个库和某张表【转】

从MySQL全库备份中恢复某个库和某张表 一.全库备份-A [[email protected] backup]#mysqldump -uroot -p123456 --default-character-set=utf8 --single-transaction --extended-insert=false --hex-blob --master-data=2 --log-error=/tmp/test.err --routines --triggers --events --quick -

[Mysql]备份同库中一张表的历史记录 insert into ..select

需求 如今有个这么一个需求.mysql中有个表.数据增长的非常快.可是呢这个数据有效期也就是1个月,一个月曾经的记录不太重要了,可是又不能删除.为了保证这个表的查询速度,须要一个简单的备份表,把数据倒进去. 代码 于是我写了一个小脚本,用来做定时任务.把这个表某段时间的数据备份到备份表中.核心就是个简单的sql. 原始表radius 备份的表为 radius2015 #!/usr/bin/python2.7 # -*- coding: utf-8 -*- #python2.7x #authror

《懒人Shell脚本》之八——定期备份Mysql数据库表的实现

0.背景 实际开发环境中,前端程序需要在给定时间段内,将数据更新/插入到mysql指定的库表中.随着数据量的增多,基础库表基数的增长,每更新一次都会有5s左右的卡顿. 改进方案一:批量更新,累计数10条或者100条进行一次更新入库操作: 改进方案二:将当前日期前1个月之前的数据进行备份操作,并删除当前库表中1个月前的数据.经测定,该方法一定程度提高了访问效率.根因:基础表基数少,查询效率相对提高. 1.库表的定时备份总结 步骤1:备份Mysql指定数据库中的制定库表. 使用 mysqldump,

sqoop 整库导入数据hive

1.整库导入 sqoop  import-all-tables --connect jdbc:mysql://ip:3306/dbname  --username user --password password --hive-database abc  -m 10  --create-hive-table  --hive-import --hive-overwrite import-all-tables :导入所有表 --connect :连接的url地址 --username:mysql用户