mongodb--Data Backup and restore

Source: Internet
Author: User
Tags mongodb mongodb server port number mongorestore
Data backup:

Open a command prompt and go to the installed MongoDB Bin directory:

The location is as follows:



Input command:mongodump-h 127.0.0.1:27017-d runoob-o D:\mongo\data\dump

which

-H:MONGDB the server address, for example: 127.0.0.1, you can also specify a port number

-D: A database instance that needs to be backed up, for example: Runoob (database name, which can back up all collections under that database)

-O: The data storage location of the backup, for example: D:\mongo\data\dump, after the backup is completed, the system automatically establishes a Runoob directory in the Dump directory, which contains the backup data of the database instance.

The following is a sketch of the execution process:


After viewing the backup, do you have the contents of the sample database to be stored under the Dump folder:


Yes, as we'd like. The data is backed up below the dump folder. Data recovery:

Scenario 1: Directly copy the files in the dump to the data directory and restart the MongoDB service.

Scenario 2: Using the Mongorestore command for data recovery

The first scenario is relatively simple, and we introduce the second scenario, which takes the form of a command

To facilitate testing, we first delete the database

Entering commands in the MongoDB shell

installed, would zero-out data files
MongoDB shell version:3.0.4
connecting to:test
> Use Runoob
swit Ched to DB Runoob
>
> Db.collections
runoob.collections
> Show Collections
col
Flatjson
Leveljson
postcollection
posts
runoob
system.indexes
users
> Db.dropdatabase ()
{"Dropped": "Runoob", "OK": 1}
> show Collections
>

Database deletion succeeded

Restore with backed-up data

Also open a command prompt, go to the installed MongoDB Bin directory, and then enter the command

mongorestore-h 127.0.0.1:27017-d runoob D:\mongo\data\dump\runoob

-h:mongodb server Address

-D: A database instance that needs to be restored, for example: twang, which can also be different from the time of backup, such as Test2

--directoryperdb: Where to back up your data, for example: D:\mongo\data\dump\runoob

Note: There is no--directoryperdb, try to add the command, error, data recovery failed. Remove to recover the data successfully

Execution Process Silhouette:


View Show collections in the shell, and all data collections are restored successfully.

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.