MongoDB Whole-Library backup and restore and a single collection backup and recovery method _mongodb

Source: Internet
Author: User
Tags json mongodb mongodb server server port mongodump mongorestore
The principle of mongodump.exe backup is to get the current server snapshot through a single query. The snapshot is written to disk, so it is not stored in real time, because the server will have data writes after the snapshot is taken, and in order to secure the backup, we can also use the Fsync lock to temporarily write the server data to the cache.

Efficient open source database (mongodb) Download address: http://www.jb51.net/softs/41751.html


Pre-backup Checks
> 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}
>



Whole Library Backup:
Mongodump-h dbhost-d Dbname-o dbdirectory
-h:mongdb the address of the server, for example: 127.0.0.1, of course, you can also specify the port number: 127.0.0.1:27017
-D: database instance that needs to be backed up, for example: test
-O: Backup data storage location, such as: C:\data\dump, of course, the directory needs to be established in advance, after the backup is completed, the system automatically set up a test directory in the Dump directory, which contains the database instance of the backup data.

Official description of mongodump (can be viewed through 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)
Whole library recovery:
mongorestore -h dbhost -d dbname –directoryperdb dbdirectory
-h: MongoDB server address
-d: the database instance to be restored, for example: test, of course, this name can also be different from the backup, such as test2
–Directoryperdb: The location of the backup data, for example: c: \ data \ dump \ test. Why add a test here instead of dump during backup, readers should check the tips by themselves!
–Drop: When restoring, delete the current data first, and then restore the backup data. In other words, after recovery, the data added and modified after backup will be deleted, use with caution!

The official description of mongorestore (can be viewed through 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
Single collection backup:
mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory
-h: server address where MongoDB is located
-d: need to restore the database instance
-c: collection to be restored
-f: fields that need to be exported (omitted for all fields)
-o: indicates the exported file name

Official description of mongoexport (can be viewed through 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)
Single collection recovery:
mongoimport -d dbhost -c collectionname –type csv –headerline –file
-type: specify the file format to be imported
-headerline: indicate that the first line is not imported because the first line is the column name
-file: specify the file path to be imported

Official description of mongoimport (availableView through 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.
Other import and export operations:

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

Parameter Description:

-d indicates the library used, in this case "my_mongodb"

-c indicates the table to be exported, in this case "user"

You can see that the table structure is implicitly created when the data is imported

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

Parameter Description:

-d indicates the library used, in this case "my_mongodb"

-c indicates the table to be exported, in this case "user"

-o indicates the name of the file to be exported, in this case "user.dat"

As you can see from the above, the export method uses the JSON style.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.