Monogdb Data Backup Recovery and Data Import and Export

Source: Internet
Author: User
Tags mongodump mongorestore
### Backup mode and recovery mode: Back up mkdirbakmongodump of all databases backup mkidrbakmongodump-dadmin of a specific database set mkdirbakmongodump-dadmin-cstude

### Backup mode and recovery mode: Back up mkdir/bakmongodump of all databases backup mkidr/bakmongodump-dadmin of a specified database to back up a collection of mkdir/bakmongodump-dadmin-cstude

### Backup and recovery methods

Back up all databases

Mkdir/bak

Mongodump

Back up a specified database

Mkidr/bak

Mongodump-d admin

Back up a collection in a database

Mkdir/bak

Mongodump-d admin-c student

Restore all databases

Mongorestore -- drop # -- drop is used to prevent data duplication.

Restore A Database

Mongorestore-d dbname -- drop

Restore a set of databases

Mongorestore-d dbname-c student -- drop

Data Recovery from different machines

Mongorestore-h host-d dbname/path/sourcefile # note that the source file of the dump file should be used here

Mongodump

-H export source

-D name of the database to be exported

-O location of the database to be exported

Mongorestore

-D: database name used

-C: restore a table

### Export data

The export tool in Mongodb can export a collection to a file in JSON or CSV format. You can use parameters to specify the data items to be exported, or export data based on the specified conditions.

Mongoexport

Parameter description:

-H: Specifies the IP address of the Database Host.

-U: Specifies the database user name.

-P: Specifies the Database Password.

-D: Specifies the database name.

-C: Specifies the collection name.

-F: Specifies the columns to export.

-O: Specifies the file name to be exported.

-Q: specify the criteria for filtering the exported data.

### Data Import

Export Import

-Type indicates the format of the file to be imported.

-Headerline indicates not to import the first line

-File: Specifies the path of the file to be imported.

Parameter description:

-H: Specifies the IP address of the Database Host.

-U: Specifies the database user name.

-P: Specifies the Database Password.

-D: Specifies the database name.

-C: Specifies the collection name.

-F: Specifies the columns to import.

The CSV format is good. Mainstream databases support exporting to the CSV format. Therefore, this format is very helpful for heterogeneous data migration.



### Demo of backing up a database

Mkdir/bakcd/bak [root @ redis bak] # mongodump-h 192.168.58.30 -- port 27017-u root-pconnected to: 192.168.58.30: 27017 Enter password: Thu Jul 17 08:15:42. 996 all dbsThu Jul 17 08:15:43. 006 DATABASE: admin to dump/adminThu Jul 17 08:15:43. 015 admin. system. indexes to dump/admin/system. indexes. bsonThu Jul 08:15:43. 076 2 objectsThu Jul 17 08:15:43. 077 admin. system. users to dump/admin/system. users. bsonThu Jul 08:15:43. 077 1 objectsThu Jul 17 08:15:43. 077 Metadata for admin. system. users to dump/admin/system. users. metadata. jsonThu Jul 17 08:15:43. 078 DATABASE: guest to dump/guestThu Jul 17 08:15:43. 082 guest. system. indexes to dump/guest/system. indexes. bsonThu Jul 08:15:43. 088 3 objectsThu Jul 17 08:15:43. 088 guest. system. users to dump/guest/system. users. bsonThu Jul 08:15:43. 097 1 objectsThu Jul 17 08:15:43. 097 Metadata for guest. system. users to dump/guest/system. users. metadata. jsonThu Jul 17 08:15:43. 098 guest. student to dump/guest/student. bsonThu Jul 08:15:43. 103 26 objectsThu Jul 17 08:15:43. 103 Metadata for guest. student to dump/guest/student. metadata. json [root @ redis bak] # lltotal 4drwxr-xr-x 4 root 4096 Jul 17 08:15 dump [root @ redis bak] # cd dump/[root @ redis dump] # lsadmin guest [root @ redis dump] #


### Restore a database

  • Because all files under the Backup Directory are specified at the beginning, an error is reported.

  • [Root @ localhost ~] # [Root @ localhost ~] # Mongorestore-d admin/* ERROR: too implements positional optionsImport BSON files into MongoDB.
  • Then I changed one and reported an error again.

  • [Root @ localhost ~] # Mongorestore-d admin/couldn 'tconnect to [127.0.0.1] couldn 'tconnect to server 127.0.0.1: 27017
  • The above error is reported because the local ip address is written in the configuration file.

  • [Root @ localhost ~] # Mongorestore-d admin/-h 192.168.58.10 connected to: 192.168.58.10Thu Jul 17 09:08:33. 873 admin/system. users. bsonThu Jul 09:08:33. 873 going into namespace [admin. system. users] 1 objects foundThu Jul 17 09:08:33. 902 Creating index: {key: {_ id: 1}, ns: "admin. system. users ", name:" _ id _ "} Thu Jul 17 09:08:34. 043 Creating index: {key: {user: 1, userSource: 1}, unique: true, ns: "admin. System. users ", name:" user_usersource_1 "} [root @ localhost ~] #


    ### Check whether the import is correct

    [Root @ localhost ~] # Mongo 192.168.58.10MongoDB shell version: 2.4.6connecting to: 192.168.58.10/testWelcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, seeQuestions? Try the support group> show dbslThu Jul 17 09:12:34. 806 don't know how to show [dbsl] at src/mongo/shell/utils. js: 847> show dbs; admin0.203125GBguest0.203125GBlocal0.078125GB> use guestswitched to db guest> show collections; studentsystem. indexessystem. users> db. student. find () {"_ id": ObjectId ("53c769621d184866a15043ec"), "name": "zhuima"} {"_ id": ObjectId ("53c769861d184866a15043ed "), "x": 1 }{ "_ id": ObjectId ("53c769861d184866a15043ee"), "x": 2 }{ "_ id": ObjectId ("53c769861d184866a15043ef "), "x": 3 }{ "_ id": ObjectId ("53c769861d184866a15043f0"), "x": 4 }{ "_ id": ObjectId ("53c769861d184866a15043f1 "), "x": 5 }{ "_ id": ObjectId ("53c769861d184866a15043f2"), "x": 6 }{ "_ id": ObjectId ("53c769861d184866a15043f3 "), "x": 7 }{ "_ id": ObjectId ("53c769861d184866a15043f4"), "x": 8 }{ "_ id": ObjectId ("53c769861d184866a15043f5 "), "x": 9} {"_ id": ObjectId ("53c769861d184866a15043f6"), "x": 10 }{ "_ id": ObjectId ("53c769861d184866a15043f7 "), "x": 11} {"_ id": ObjectId ("53c769861d184866a15043f8"), "x": 12} {"_ id": ObjectId ("53c769861d184866a15043f9 "), "x": 13 }{ "_ id": ObjectId ("53c769861d184866a15043fa"), "x": 14 }{ "_ id": ObjectId ("53c769861d184866a15043fb "), "x": 15 }{ "_ id": ObjectId ("53c769861d184866a15043fc"), "x": 16 }{ "_ id": ObjectId ("53c769861d184866a15043fd "), "x": 17} {"_ id": ObjectId ("53c769861d184866a15043fe"), "x": 18} {"_ id": ObjectId ("53c769861d184866a15043ff "), "x": 19} Type "it" for more>



    ### User management

    Add superuser

    Use admin

    Db. addUser ('name', 'Password ')

    Add read-only users

    Use dbname

    Db. addUser ('name', 'Password', true)

    Add normal user

    Use dbname

    Db. addUser ('name', 'Password') cannot be a common user. This is only for a database with permissions.

    Delete a user

    Use dbname

    Db. system. users. remove ('name', 'Password ')

    Change User Password

    Use dbname

    Db. addUser ('name', 'Password ')

    Switch the user (you must first go to the corresponding database, except for the root account)

    Use dbname

    Db. auth ('name', 'Password ')

    Find users

    Use dbname

    Db. system. users. find ()


    ### Monitor mongodb

    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.