It's written in front.
This article has already assumed that you have installed the MongoDB (2.6) and has opened the auth.
User
First we add the users needed to back up and recover the data, and this user needs to have ReadWrite and Useradmin permissions
Copy Code code as follows:
$ MONGO
$ use admin
$ db.auth ("admin", "youradminpasswd");
$ use backupdb
$ db.adduser ({User: "Backup", pwd: "passwd", Roles: ["ReadWrite", "Useradmin"]})
Backup
Note: This command is executed in the console
We use Mongodump for data backup (note: Mongodump does not back up the content in the local database).
Mongodump can be backed up by data in the following two ways:
Connect to Mongod or MONGOs
Direct access to data files
The tool can back up the entire server, a single database, or a single collection data, or only some of the data in the collection through a query statement.
If you execute mongodump directly without any parameters, it connects to the MongoDB instance on the local (127.0.0.1 or localhost) 27017 port and creates a backup named dump.
Copy Code code as follows:
$ mongodump--host mongodb.example.net--port 27017--db test--collection Some--username backup--password passwd
This will enable Mongodump to connect to the Mongod on the mongodb.example.net:27017 and back up somecollection in dbtest to the dump directory
Note: Starting with the Mongodb2.2 version, the data format used by Mongodump is incompatible with the Mongod instance of the previous version. So don't use the new version (>=2.2) Mongodump to back up old data.
Recovery
Data backed up using mongodump needs to be recovered using Mongorestore.
Mongorestore Data recovery methods and mongodump correspond to, but also divided into two kinds:
Connect to Mongod or MONGOs
Write directly to the data file
Mongorestore can either restore the entire backup or recover a portion of it.
Copy Code code as follows:
$ mongorestore--host mongodb.example.net--port 27017--db test--collection Some--username backup--password password/ Data/backup
The above data is recovered from the/data/backup, which restores only the TestDB somecollection to mongodb.example.net:27017. If you do not specify--host and--portoption, then Mongorestore will use localhost:27017 by default.
Note: When restoring, you must create new data to be restored on the database that you want to restore and the user--drop you belong to delete the original database in the library and restore the database up
This command is performed in the console