备份与恢复

工具下载链接
https://www.mongodb.com/try/download/database-tools
本地链接
(Winodws版本):
mongodb-database-tools-windows-x86_64-100.9.0.zip
(Centos8版本):
2023-11-06T03:25:58.png

1.MongoImport和MongoExport的使用

1.1MongoExport 导入 json csv文件

Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示

[root@localhost mongodb]# ./bin/mongoexport --help
Export MongoDB data to CSV, TSV or JSON files.
 
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 ( <set name>/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
  -type 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
参数说明:

-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件


例如导出json格式:
mongoexport.exe -h 192.168.14.215:27017 --authenticationDatabase admin -u happy -p=xxx -d happy -c inventory -o inventory.dat

屏幕截图 2023-11-02 142632.png

例如导出csv格式(csv必须指定fields):
./bin/mongoexport  -h 127.0.0.1:27017 --authenticationDatabase admin -u admin -p=xxx  -d test -c students -type csv -f classid,name,age -o students_csv.dat

1.2MongoImport 导入 json csv文件

[root@localhost mongodb]# ./bin/mongoimport --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 ( <set name>/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 4MB.

参数说明:

-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列

例如导入json格式:
>mongoimport.exe -h 192.168.14.215:27017 --authenticationDatabase admin -u happy -p=xxxx -d happy -c inventory --file inventory.dat

导入前
QQ截图20231102143516.png
导入后
QQ截图20231102143944.png

例如导入csv格式:
./bin/mongoimport  -h 127.0.0.1:27017 --authenticationDatabase admin -u admin -p=xxxx -d test -c students --type csv --headerline --file students_csv

2.MongoDump和MongoRestore的使用

2.1MongoDump

general options:
      /help                                                 print usage
      /version                                              print the tool version and exit
      /config:                                              path to a configuration file

verbosity options:
  /v, /verbose:<level>                                      more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
      /quiet                                                hide all log output

connection options:
  /h, /host:<hostname>                                      mongodb host to connect to (setname/host1,host2 for replica sets)
      /port:<port>                                          server port (can also use --host hostname:port)

ssl options:
      /ssl                                                  connect to a mongod or mongos that has ssl enabled
      /sslCAFile:<filename>                                 the .pem file containing the root certificate chain from the certificate authority
      /sslPEMKeyFile:<filename>                             the .pem file containing the certificate and key
      /sslPEMKeyPassword:<password>                         the password to decrypt the sslPEMKeyFile, if necessary
      /sslCRLFile:<filename>                                the .pem file containing the certificate revocation list
      /sslFIPSMode                                          use FIPS mode of the installed openssl library
      /tlsInsecure                                          bypass the validation for server's certificate chain and host name

authentication options:
  /u, /username:<username>                                  username for authentication
  /p, /password:<password>                                  password for authentication
      /authenticationDatabase:<database-name>               database that holds the user's credentials
      /authenticationMechanism:<mechanism>                  authentication mechanism to use
      /awsSessionToken:<aws-session-token>                  session token to authenticate via AWS IAM

kerberos options:
      /gssapiServiceName:<service-name>                     service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
      /gssapiHostName:<host-name>                           hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)

namespace options:
  /d, /db:<database-name>                                   database to use
  /c, /collection:<collection-name>                         collection to use

uri options:
      /uri:mongodb-uri                                      mongodb uri connection string

query options:
  /q, /query:                                               query filter, as a v2 Extended JSON string, e.g., '{"x":{"$gt":1}}'
      /queryFile:                                           path to a file containing a query filter (v2 Extended JSON)
      /readPreference:<string>|<json>                       specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds: 123}')
      /forceTableScan                                       force a table scan (do not use $snapshot or hint _id). Deprecated since this is default behavior on WiredTiger

output options:
  /o, /out:<directory-path>                                 output directory, or '-' for stdout (default: 'dump')
      /gzip                                                 compress archive or collection output with Gzip
      /oplog                                                use oplog for taking a point-in-time snapshot
      /archive:<file-path>                                  dump as an archive to the specified path. If flag is specified without a value, archive is written to stdout
      /dumpDbUsersAndRoles                                  dump user and role definitions for the specified database
      /excludeCollection:<collection-name>                  collection to exclude from the dump (may be specified multiple times to exclude additional collections)
      /excludeCollectionsWithPrefix:<collection-prefix>     exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes)
  /j, /numParallelCollections:                              number of collections to dump in parallel
      /viewsAsCollections                                   dump views as normal collections with their produced data, omitting standard collections
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出路径
-q:指明导出数据的过滤条件
-o path (不归档的路径,二选1)
--gzip 压缩
--archive path(归档的路径+文件,二选1 )

导出gzip格式的压缩文件:

mongodump.exe --authenticationDatabase admin -h 192.168.14.215:27017 -u happy -p=xxx -d happy -c products --archive=products.archive --gzip

2.1MongoRestore

general options:
      /help                                                 print usage
      /version                                              print the tool version and exit
      /config:                                              path to a configuration file

verbosity options:
  /v, /verbose:<level>                                      more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
      /quiet                                                hide all log output

connection options:
  /h, /host:<hostname>                                      mongodb host to connect to (setname/host1,host2 for replica sets)
      /port:<port>                                          server port (can also use --host hostname:port)

ssl options:
      /ssl                                                  connect to a mongod or mongos that has ssl enabled
      /sslCAFile:<filename>                                 the .pem file containing the root certificate chain from the certificate authority
      /sslPEMKeyFile:<filename>                             the .pem file containing the certificate and key
      /sslPEMKeyPassword:<password>                         the password to decrypt the sslPEMKeyFile, if necessary
      /sslCRLFile:<filename>                                the .pem file containing the certificate revocation list
      /sslFIPSMode                                          use FIPS mode of the installed openssl library
      /tlsInsecure                                          bypass the validation for server's certificate chain and host name

authentication options:
  /u, /username:<username>                                  username for authentication
  /p, /password:<password>                                  password for authentication
      /authenticationDatabase:<database-name>               database that holds the user's credentials
      /authenticationMechanism:<mechanism>                  authentication mechanism to use
      /awsSessionToken:<aws-session-token>                  session token to authenticate via AWS IAM

kerberos options:
      /gssapiServiceName:<service-name>                     service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
      /gssapiHostName:<host-name>                           hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)

namespace options:
  /d, /db:<database-name>                                   database to use
  /c, /collection:<collection-name>                         collection to use

uri options:
      /uri:mongodb-uri                                      mongodb uri connection string

namespace options:
      /excludeCollection:<collection-name>                  DEPRECATED; collection to skip over during restore (may be specified multiple times to exclude additional collections)
      /excludeCollectionsWithPrefix:<collection-prefix>     DEPRECATED; collections to skip over during restore that have the given prefix (may be specified multiple times to exclude additional prefixes)
      /nsExclude:<namespace-pattern>                        exclude matching namespaces
      /nsInclude:<namespace-pattern>                        include matching namespaces
      /nsFrom:<namespace-pattern>                           rename matching namespaces, must have matching nsTo
      /nsTo:<namespace-pattern>                             rename matched namespaces, must have matching nsFrom

input options:
      /objcheck                                             validate all objects before inserting
      /oplogReplay                                          replay oplog for point-in-time restore
      /oplogLimit:<seconds>[:ordinal]                       only include oplog entries before the provided Timestamp
      /oplogFile:<filename>                                 oplog file to use for replay of oplog
      /archive:<filename>                                   restore dump from the specified archive file.  If flag is specified without a value, archive is read from stdin
      /restoreDbUsersAndRoles                               restore user and role definitions for the given database
      /dir:<directory-name>                                 input directory, use '-' for stdin
      /gzip                                                 decompress gzipped input

restore options:
      /drop                                                 drop each collection before import
      /dryRun                                               view summary without importing anything. recommended with verbosity
      /writeConcern:<write-concern>                         write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}'
      /noIndexRestore                                       don't restore indexes
      /convertLegacyIndexes                                 Removes invalid index options and rewrites legacy option values (e.g. true becomes 1).
      /noOptionsRestore                                     don't restore collection options
      /keepIndexVersion                                     don't update index version
      /maintainInsertionOrder                               restore the documents in the order of their appearance in the input source. By default the insertions will be performed in an arbitrary order. Setting this flag also enables
                                                            the behavior of --stopOnError and restricts NumInsertionWorkersPerCollection to 1.
  /j, /numParallelCollections:                              number of collections to restore in parallel
      /numInsertionWorkersPerCollection:                    number of insert operations to run concurrently per collection
      /stopOnError                                          halt after encountering any error during insertion. By default, mongorestore will attempt to continue through document validation and DuplicateKey errors, but with this option
                                                            enabled, the tool will stop instead. A small number of documents may be inserted after encountering an error even with this option enabled; use --maintainInsertionOrder to halt
                                                            immediately after an error
      /bypassDocumentValidation                             bypass document validation
      /preserveUUID                                         preserve original collection UUIDs (off by default, requires drop)
      /fixDottedHashIndex                                   when enabled, all the hashed indexes on dotted fields will be created as single field ascending indexes on the destination
 参数说明:
    -h:指明数据库宿主机的IP
    -u:指明数据库的用户名
    -p:指明数据库的密码
    -d:指明数据库的名字
    -c:指明collection的名字
    -dir path+*bson.gz (选择不归档的文件路径,2选1,之前导出是单个collection要加上*bson.gz ,多个不需要)
    --gzip 压缩
    --archive (选择归档的路径+文件,2选1) 

导入命令:

mongorestore.exe --authenticationDatabase admin -h 192.168.14.215:27017 -u happy -p=xxx -d happy -c products --archive=products.archive --gzip

导入前:
2023-11-02T07:35:19.png
导入后:
2023-11-02T07:37:32.png

新建新集合

var result = db.csmdR.find({"查询条件"})
while(result.hasNext()) db.新建表名.insert(result.next())

最后修改:2023 年 11 月 16 日
如果觉得我的文章对你有用,请随意赞赏