MongoDB Backup and Recovery

Source: Internet
Author: User
Tags file copy mongodb mongodb port number mongodump mongorestore

Transferred from: https://www.cnblogs.com/shilin000/p/5248177.html

Https://www.cnblogs.com/timelesszhuang/p/5906068.html

There are two ways to back up MongoDB:

1. Direct copy of all files under the data directory

2. Using the Mongodump method

3. Master-slave replication: http://www.cnblogs.com/huangxincheng/archive/2012/03/04/2379755.html (This article details the master-slave replication)

The following one by one shows:

1. Copy all files directly under the Data directory:

This is used to back up the entire MongoDB database and cannot back up a single database or collection or sub-collection

You must prevent the data file from changing during the copy process. Therefore, you need to lock the database to prevent data writes.

Db.fsynclock ()

The above command blocks the write operation and flushes dirty data to disk to ensure consistent data.
Then, copy the data file to the backup directory

Cp-r/data/db/*/backup

After the file copy is complete, the database is unlocked and the write operation is allowed

Db.fsyncunlock ()

Note: When executing db.fsynclock () and Db.fsyncunlock (), you cannot close the current shell window, otherwise you may not be able to connect and you will need to restart the Mongod service.

When recovering, make sure the Mongod is not running, empty the data directory, copy the backed up data to the data directory, and then start the Mongod

Cp-r/backup/*/data/db/mongod-f mongod.conf

2. Use the Mongodump method:

Do not use Fsynclock with Mongodump, if the database is locked, Mongodump will hang forever.

Using a mongodump backup is slow, and there are some problems with backing up the copy set, as you'll say later. However, it is a good way to back up a single database, collection, or sub-collection.

    • First database backup: Mongodump-h IP--port Port-u user name-p password-d database-o file exists path
Mongodump-h 127.0.0.1-u admin-p xxx-  d blog-o '/home/timeless/desktop/mongodump '--authenticationdatabase admin
Note The--authenticationdatabase parameter makes an authentication database otherwise prompts for an error:
Failed:error connecting to DB Server:server returned error on SASL authentication step:authentication Failed.   Prompt for authentication failure
#./mongodump--helpoptions:--help Show Help Information-v [--verbose] print out more information, such as time and so on-VVVVV--version print version information-h [--host] arg specifies the connected MongoDB host, set to &L when copying the set                                T;set NAME>/S1,S2--port arg Specifies the MongoDB port number, so you can specify--host hostname:port--ipv6 Enable support IPV6 support-u [--username] arg user name-P [--password] arg password-                                        -authenticationdatabase arg user source (defaults to dbname)--authenticationmechanism Arg (=MONGODB-CR) Authentication mechanism--dbpath ARG directly accesses the Mongod database file instead of connecting to Mon Godb server. The data directory needs to be locked if Mongod is currently accessing the same path that will not be available.   In other words, the Mongod runtime cannot use--dbpath,mongod without running the case can be directly specified--dbpath--DIRECTORYPERDB each db a separate directory, need to specify DBPath --journal enable journaling, you need to specify dbpath-d [--DB] arg to specify the database  -c [--collection] arg specifies the collection-o [--out] arg (=dump) specifies the output directory, "-" indicates standard output-Q [--query] arg JSON query--oplog uses Oplog to produce point-in-time snapshots--repair attempts to restore Complex crash database--forcetablescan forced table scan, do not use $snapshot

Commonly used is: Mongodump-h host-u user name-p password-d database-C collection-O backup directory

Use Mongorestore to restore MongoDB when recovering

#./mongorestore--help//Same part parameter meaning participate in the above mongodump-v [--verbose]--version                                  -h [--host] arg--port arg--ipv6            -U [--username] arg-p [--password] arg--authenticationdatabase arg                        --authenticationmechanism Arg (=MONGODB-CR)--dbpath arg--directoryperdb --journal-d [--db] arg-c [--collection] arg--objcheck validating an object before inserting, enabled by default--n                                Oobjcheck does not validate object before inserting--filter arg before inserting filter--drop Remove all documents before inserting--oplogreplay apply Oplog--oploglimit arg include oplog on recovery                                         Entries before the provided Timestamp (Seconds[:ordinal]) During tHe oplog replay;  The ordinal value is optional--keepindexversion don ' t upgrade                      Indexes to newest version--nooptionsrestore don ' t restore collection options--noindexrestore Don ' t restore indexes--w ARG (=0) minimum number of replicas per write

Restore the entire MongoDB database:

Mongorestore-p 27017 dump/

Revert to a specific library and collection:

Mongorestore--db ttlsa_com--collection posts Dump/old_ttlsa_com/old_posts.bson

Note: The Mongodump and Mongorestore versions are best matched.

MongoDB Backup and Recovery

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.