Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb Export Import

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Databases are divided into cold backup and hot backup, the so-called cold backup is to stop the library, and then copy the data; hot backup is to use the corresponding tools to do backup while the database is running. Each library has its own hot backup tool, and mongodb is no exception.

The backup of mongodb mainly includes hot backup and cold backup.

First, cold standby:

Stop the mongodb service and directly copy the data directory; when you start mongodb, you can specify the database storage directory you set up with-- dbpath.

Second, hot standby:

Use mongodump to do MongoDB libraries or table-level hot backups.

Meet mongodump.

[mongo@tstdb-25-220mongodb] $bin/mongodump-- helpUsage: mongodump Export the content of a running server into .bson files.Specify a database with-d and a collection with-c to only dump that database or collection.See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information.general options:-- help print usage-- version Print the tool version and exitverbosity options:-v -- verbose= more detailed log output (include multiple times for more verbosity, e.g.-vvvvv, or specify a numeric value, e.g.-- verbose=N)-- quiet hide all log outputconnection options:-h -- host= mongodb host to connect to (setname/host1,host2 for replica sets)-- port= server port (can also use-- host hostname:port) authentication options:-u,-- username= username for authentication-p -- password= password for authentication-- authenticationDatabase= database that holds the user's credentials-- authenticationMechanism= authentication mechanism to usenamespace options:-- d,-- db= database to use-c,-- collection= collection to usequery options:-Q -- query= query filter, as a JSON string, e.g. '{x: {$gt:1}'-- queryFile= path to a file containing a query filter (JSON)-- readPreference= | specify either a preference name or a preference json object-- forceTableScan force a table scanoutput options:-o,-- out= output directory Or'- 'for stdout (defaults to' dump')-- gzip compress archive our collection output with Gzip-- repair try to recover documents from damaged data files (not supported by all storage engines)-- oplog Use oplog for taking a point-in-time snapshot-- archive= dump as an archive to the specified path. If flag is specified without a value Archive is written to stdout-dumpDbUsersAndRoles dump user and role definitions for the specified database-excludeCollection= collection to exclude from the dump (may be specified multiple times to exclude additional collections)-excludeCollectionsWithPrefix= exclude all collections from the dump that have the given prefix (may be specified multiple times To exclude additional prefixes)-j -- numParallelCollections= number of collections to dump in parallel (4 by default)

Take a look at the export process

[mongo@tstdb-25-220 mongodb] $bin/mongodump-h 172.16.25.220-d sykdb-o / usr/local/mongodb/backup2016-10-31T09:41:03.521+0800 writing sykdb.system.indexes to 2016-10-31T09:41:03.522+0800 done dumping sykdb.system.indexes (2 documents) 2016-10-31T09:41:03.523+0800 writing sykdb.system.profile to 2016-10-31T09:41:03.523+0800 writing sykdb.sykdb to 2016-10-31T09:41 03.523 0800 writing sykdb.table_syk to 2016-10-31T09:41:03.524+0800 done dumping sykdb.system.profile (2 documents) 2016-10-31T09:41:03.524+0800 done dumping sykdb.sykdb (2 documents) 2016-10-31T09:41:03.524+0800 done dumping sykdb.table_syk (1 document)

The exported file is as follows:

[mongo@tstdb-25-220mongodb] $cd backup/ [Mongo @ tstdb-25-220backup] $ll total dosage 4drwxr-xr-x. 2 mongo dbmon 4096 October 31 09:41 sykDB [Mongo @ tstdb-25-220 backup] $cd sykdb/ [tstdb-25-220 sykdb] $ll total usage 28 RWQ. 1 mongo dbmon 75 October 31 09:41 sykdb.bson-rw-r--r--. 1 mongo dbmon 83 October 31 09:41 sykdb.metadata.json-rw-r--r--. 1 mongo dbmon 136 October 31 09:41 system.indexes.bson-rw-r--r--. 1 mongo dbmon 1529 October 31 09:41 system.profile.bson-rw-r--r--. 1 mongo dbmon 55 October 31 09:41 system.profile.metadata.json-rw-r--r--. 1 mongo dbmon 36 October 31 09:41 table_syk.bson-rw-r--r--. 1 mongo dbmon 87 October 31 09:41 table_syk.metadata.json

Let's take a look at import (export and import can be thought of as backup and recovery, all backups are done for recovery)

[mongo@tstdb-25-220mongodb] $bin/mongorestore-- helpUsage: mongorestore Restore backups generated with mongodump to a running server.Specify a database with-d to restore a single database from the target directory Or use-d and-c to restore a single collection from a single .bson file.See http://docs.mongodb.org/manual/reference/program/mongorestore/ for more information.general options:-- help print usage-- version print the tool version and exitverbosity options:-v -- verbose= more detailed log output (include multiple times for more verbosity, e.g.-vvvvv, or specify a numeric value, e.g.-- verbose=N)-- quiet hide all log outputconnection options:-h -- host= mongodb host to connect to (setname/host1,host2 for replica sets)-- port= server port (can also use-- host hostname:port) authentication options:-u,-- username= username for authentication-p -- password= password for authentication-- authenticationDatabase= database that holds the user's credentials-- authenticationMechanism= authentication mechanism to usenamespace options:-d,-- db= database to use-c -collection= collection to useinput options:-objcheck validate all objects before inserting-oplogReplay replay oplog for point-in-time restore-oplogLimit= [: ordinal] only include oplog entries before the provided Timestamp-archive= restore dump from the specified archive file. If flag is specified without a value, archive is read from stdin-restoreDbUsersAndRoles restore user and role definitions for the given database-dir= input directory Use'- 'for stdin-- gzip decompress gzipped inputrestore options:-- drop drop each collection before import-- writeConcern= write concern options e.g.-- writeConcern majority,-- writeConcern' {w: 3, wtimeout: 500, fsync: true J: true}'(defaults to 'majority')-noIndexRestore don't restore indexes-- noOptionsRestore don't restore collection options-- keepIndexVersion don't update index version-- maintainInsertionOrder Preserve order of documents during restoration-j -numParallelCollections= number of collections to restore in parallel (4 by default)-numInsertionWorkersPerCollection= number of insert operations to run concurrently per collection (1 by default)-stopOnError stop restoring if an error is encountered on insert (off by default)-bypassDocumentValidation bypass document validation

-- oplogReplay this parameter, please note that the export and import of mongodb can do basic point-in-time recovery.

Let's insert one line again and then test it:

Moe:PRIMARY > db.sykdb.insert ({"job": "dba"})

Then import:

[mongo@tstdb-25-220mongodb] $bin/mongorestore-h 172.16.25.220-d sykdb backup/sykdb2016-10-31T10:28:21.049+0800 building a list of collections to restore from backup/sykdb dir2016-10-31T10:28:21.051+0800 reading metadata for sykdb.sykdb from backup/sykdb/sykdb.metadata.json2016-10-31T10:28:21.051+0800 reading metadata for sykdb.table_syk from backup/sykdb/table_syk.metadata.json2016-10-31T10:28:21.051+0800 Reading metadata for sykdb.system.profile from backup/sykdb/system.profile.metadata.json2016-10-31T10:28:21.051+0800 restoring sykdb.sykdb from backup/sykdb/sykdb.bson2016-10-31T10:28:21.051+0800 no indexes to restore2016-10-31T10:28:21.052+0800 finished restoring sykdb.system.profile (0 documents) 2016-10-31T10:28:21.052+0800 restoring sykdb.table_syk from backup/sykdb/table_syk.bson2016-10-31T10:28:21.086+0800 Restoring indexes for collection sykdb.sykdb from metadata2016-10-31T10:28:21.086+0800 restoring indexes for collection sykdb.table_syk from metadata2016-10-31T10:28:21.086+0800 finished restoring sykdb.sykdb (2 documents) 2016-10-31T10:28:21.086+0800 finished restoring sykdb.table_syk (1 document) 2016-10-31T10:28:21.087+0800 done

Check to see if the data has come back.

Moe:PRIMARY > db.sykdb.find () .limit; {"_ id": ObjectId ("5816ac26d37a70860b091c1f"), "job": "dba"} {"_ id": ObjectId ("5813041a56c55d778812e689"), "name": "syk"} {"_ id": ObjectId ("5813065356c55d778812e68a"), "loc": "beijing"}

Introduce a pair of commands: mongoexport/mongoimport

[mongo@tstdb-25-220mongodb] $bin/mongoexport-h 172.16.25.220-d sykdb-c table_syk-o table_syk.dat2016-10-31T11:43:40.893+0800 connected to: 172.16.25.2202016-10-31T11:43:40.894+0800 exported 1 record

View the exported file:

[mongo@tstdb-25-220mongodb] $strings table_syk.dat {"_ id": {"$oid": "581313a856c55d778812e68b"}, "name": "syk"} [mongo@tstdb-25-220mongodb] $file table_syk.dat table_syk.dat: ASCII text

It seems that this tool is equivalent to logical export in other databases.

Empty the watch

Moe:PRIMARY > db.table_syk.drop ()

Import

[mongo@tstdb-25-220mongodb] $bin/mongoimport-h 172.16.25.220-d sykdb-c table_syk table_syk.dat2016-10-31T11:45:22.499+0800 connected to: 172.16.25.2202016-10-31T11:45:22.540+0800 imported 1 document

The data is back.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report