Mongodb database data import/export backup tutorial

Source: Internet
Author: User
Tags mongodb mongodb server server port mongorestore

Mongodb database backup and recovery in windows

I can talk about data backup to the c: \ data \ dump directory, first create this path. Go to the bin directory of mongodb.

Mine is:

C: \ Program Files \ mongodb \ bin

The backup script is:

// Backup
Export dump-h 127.0.0.1: 27017-d test-o c: \ data \ dump

The recovery script is:

// Restore
Mongorestore-h 127.0.0.1: 27017-d test -- directoryperdb c: \ data \ dump \ test

Explain the commands used

-H: Address of the MongoDB server
-D: The database instance to be restored, for example, test. Of course, this name can be different from that during backup, for example, test2.
-O: The storage location of the backup data, for example, c: \ data \ dump. Of course, this directory must be created in advance. After the backup is complete, the system automatically creates a test directory under the dump directory, which stores the backup data of the database instance.
-- Directoryperdb: the location where the backup data is located, for example, c: \ data \ dump \ test. Why should we add another test instead of dump during backup? Read the prompt on your own!
-- Drop: delete the current data and restore the backup data. That is to say, after the Restoration, all the data added and modified after the backup will be deleted. Use it with caution!

Original explanation:

-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
Default)
-U [-- username] arg username
-P [-- password] arg password
-- AuthenticationDatabase arg user source (defaults to dbname)
-- Authenticationmechanic arg (= MONGODB-CR)
Authentication mechanism
-- Dbpath arg directly access mongod database files
In the given path, instead
Connecting to a mongod server-needs
To lock the data directory, so cannot
Be used if a primary D is currently
Accessing the same path
-- Directoryperdb each db is in a separate directly
(Relevant only if dbpath specified)
-- Journal enable journaling (relevant only if
Dbpath specified)
-D [-- db] arg database to use
-C [-- collection] arg collection to use (some commands)
-- Objcheck validate object before inserting
(Default)
-- Noobjcheck don't validate object before inserting
-- Filter arg filter to apply before inserting
-- Drop each collection before import
-- OplogReplay replay oplog for point-in-time restore
-- OplogLimit arg include oplog entries before
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

Backup and Recovery of mongodb databases in linux

In linux, we can create an automatic backup script, which can be set for scheduled tasks or manual backup. I manually backed up the data.

First, create an sh command, which is placed under home.

Vim/home/describeifen. sh

Enter the following content:

#! /Bin/bash
Shijie = 'date + % Y % m % d % H'
Back‑dbfile‑mongodb‑shijie.tar.gz
Cd/home/mongoDbback/
/Usr/local/mongo/bin/mongodump-h 127.0.0.1 -- port 27017-u mongo-p 123456-d my_mongodb-o my_mongodb_dump/
Tar czf $ backmongodbFile my_mongodb_dump/
Rm my_mongodb_dump-rf

Explanation:

The folder for storing backups is/home/mongoDbback/

-U is the database name username-p is the password-d is the database name, which is similar to window. You can refer to the above windows explanation.

Just run the backup.

./Describeifen. sh
You can.

Database recovery:

/Usr/local/mongo/bin/mongorestore-d my_mongodb my_mongodb_dump/my_mongodb/* points to each file
/Usr/local/mongo/bin/mongorestore-h 127.0.0.1 -- port 27017 -- drop -- directoryperdb my_mongodb_dump/my_mongodb points to a directory
If you have any questions, try the window statement:

/Usr/local/mongo/bin/mongorestore-h 127.0.0.1: 27017-d test -- drop -- directoryperdb my_mongodb_dump/my_mongodb

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.