MongoDB备份原理与fsync锁机制
mongodump.exe 执行备份的核心机制是发起一次查询以获取服务器的当前状态快照,并将其持久化存储至磁盘。由于在捕获快照期间服务器仍可能接收新写入,因此该方式并非绝对实时。为确保备份数据的完整性与安全性,建议结合 fsync 锁指令,强制将内存中的数据刷入缓存后再进行备份操作。
高效开源数据库 MongoDB 的官方下载地址:https://www.jb51.net/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:指定 MongoDB 服务器地址,如 127.0.0.1,也可包含端口号 127.0.0.1:27017。 -d:指定需要备份的目标数据库实例名称,例如 test。 -o:指定备份文件的存储目录,如 c:\data\dump。需提前创建该目录,备份完成后系统会自动在 dump 目录下生成一个与数据库同名的子目录,其中存放具体的备份数据文件。
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。此处路径结构需对应备份时的层级。 –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:指定需要导出的集合(Collection)名称。 -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:指定导入文件的格式类型(如 csv、json 等)。 -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 导入数据: mongoimport -d my_mongodb -c user user.dat 参数解析: -d:指定目标数据库,本例中为 “my_mongodb”。 -c:指定目标集合,本例中为 “user”。 注意:在执行导入操作时,如果指定的集合不存在,系统会自动隐式创建该表结构。
2. 使用 mongoexport 导出数据: mongoexport -d my_mongodb -c user -o user.dat 参数解析: -d:指定源数据库,本例中为 “my_mongodb”。 -c:指定源集合,本例中为 “user”。 -o:指定输出文件名,本例中为 “user.dat”。 从上述命令可见,默认情况下 mongoexport 输出的数据格式为 JSON 样式。