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 backup and recovery

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

Share

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

Mongodb provides two commands to back up (mongodump) and restore (mongorestore) data. The format of the data dump is BSON.

1.mongodump backup

Usage: bin/mongodump [options]

For example: back up the database of port 6666 to the / data/6666 directory on this machine

Bin/mongodump-- port 6666-- directoryperdb-o / data/6666

Connected to: 127.0.0.1:6666

All dbs

DATABASE: test to / data/6666/test

Test.system.indexes to / data/6666/test/system.indexes.bson

1 objects

Test.ints to / data/6666/test/ints.bson

4 objects

DATABASE: admin to / data/6666/admin

2.mongorestore recovery

Usage: bin/mongorestore [options] [directory or filename to restore from]

For example: delete the database test and then restore it from the backup directory

> show dbs

Admin (empty)

Local 4.201171875GB

Test 0.203125GB

> use test

Switched to db test

> db.dropDatabase ()

{"dropped": "test", "ok": 1}

> show dbs

Admin (empty)

Local 4.201171875GB

Restore using mongorestore

Bin/mongorestore-host 127.0.0.1 directoryperdb / data/6666/

Connected to: 127.0.0.1:6666

Wed Jul 6 16:37:33 / data/6666/test/ints.bson

Wed Jul 6 16:37:33 going into namespace [test.ints]

Wed Jul 6 16:37:33 4 objects found

Wed Jul 6 16:37:33 / data/6666/test/system.indexes.bson

Wed Jul 6 16:37:33 going into namespace [test.system.indexes]

Wed Jul 6 16:37:33 {name: "_ id_", ns: "test.ints", key: {_ id: 1}, v: 0}

Wed Jul 6 16:37:33 1 objects found

Check to see if the database is restored

> show dbs

Admin (empty)

Local 4.201171875GB

Test 0.203125GB

Mongorestore has a parameter-drop. Using this parameter, all the data in the collection will be deleted and then restored.

That is, if you have new data after backing up the data, then use-- drop, and the new data will not be seen.

Without this parameter, the new data still exists when the backup is restored.

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