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

Backup and recovery of MongoDB

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

Share

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

MongoDB data backup

In Mongodb, we use the mongodump command to back up MongoDB data. This command exports all data to the specified directory.

The mongodump command can specify the server for rollover of the exported data magnitude through parameters.

Grammar

The mongodump command script syntax is as follows:

> mongodump-h dbhost-d dbname-o dbdirectory

●-h:

The address of the server where the MongDB is located, for example: 127.0.0.1. Of course, you can also specify the port number: 127.0.0.1 27017.

●-d:

Database instance that needs to be backed up, for example: test

●-o:

The location of the backup data, for example: C:\ data\ dump. Of course, this directory needs to be established in advance. After the backup is completed, the system automatically creates a test directory under the dump directory, in which the backup data of the database instance is stored.

Back up the entire database

[root@D2-LZY245 ~] # mongodump

2017-08-18T11:06:39.771+0800 writing admin.system.users to

2017-08-18T11:06:39.772+0800 done dumping admin.system.users (3 documents)

2017-08-18T11:06:39.772+0800 writing admin.system.version to

2017-08-18T11:06:39.773+0800 done dumping admin.system.version (2 documents)

2017-08-18T11:06:39.773+0800 writing hr.emps to

2017-08-18T11:06:39.773+0800 writing hr.emps_infos to

2017-08-18T11:06:39.773+0800 writing hr.inventory to

2017-08-18T11:06:39.773+0800 writing hr.emp to

2017-08-18T11:06:39.774+0800 done dumping hr.emps_infos (11 documents)

2017-08-18T11:06:39.774+0800 writing hr.shop to

2017-08-18T11:06:39.774+0800 done dumping hr.emps (11 documents)

2017-08-18T11:06:39.774+0800 writing hr.t_emps_job to

2017-08-18T11:06:39.775+0800 done dumping hr.emp (9 documents)

2017-08-18T11:06:39.775+0800 writing hr.dept to

2017-08-18T11:06:39.775+0800 done dumping hr.shop (7 documents)

2017-08-18T11:06:39.775+0800 writing hr.news to

2017-08-18T11:06:39.775+0800 done dumping hr.t_emps_job (6 documents)

2017-08-18T11:06:39.775+0800 writing hr.depts to

2017-08-18T11:06:39.775+0800 done dumping hr.news (4 documents)

2017-08-18T11:06:39.775+0800 writing hr.loc to

2017-08-18T11:06:39.776+0800 done dumping hr.dept (5 documents)

2017-08-18T11:06:39.776+0800 writing hr.t_emps_sex to

2017-08-18T11:06:39.776+0800 done dumping hr.depts (2 documents)

2017-08-18T11:06:39.776+0800 writing hr.info to

2017-08-18T11:06:39.776+0800 done dumping hr.loc (2 documents)

2017-08-18T11:06:39.776+0800 writing hr.phones to

2017-08-18T11:06:39.776+0800 done dumping hr.info (0 documents)

2017-08-18T11:06:39.776+0800 writing test.fs.chunks to

2017-08-18T11:06:39.777+0800 done dumping hr.t_emps_sex (2 documents)

2017-08-18T11:06:39.777+0800 writing test.fs.files to

2017-08-18T11:06:39.777+0800 done dumping hr.phones (0 documents)

2017-08-18T11:06:39.777+0800 done dumping test.fs.chunks (0 documents)

2017-08-18T11:06:39.777+0800 done dumping hr.inventory (10 documents)

2017-08-18T11:06:39.777+0800 done dumping test.fs.files (0 documents)

Backup result

[root@D2-LZY245 ~] # ll dump/

Total 4

Drwxrwxr-x 2 root root 124 Aug 18 11:06 admin

Drwxrwxr-x 2 root root 4096 Aug 18 11:06 hr

Drwxrwxr-x 2 root root 106 Aug 18 11:06 test

Delete a collection

> use hr

Switched to db hr

> db.dept.find ()

{"_ id": ObjectId ("599504e70184ff511bf02be1"), "deptno": 11, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be2"), "deptno": 12, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be3"), "deptno": 13, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e80184ff511bf02be4"), "deptno": 14, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599505240184ff511bf02be5"), "deptno": 16, "dname": "Finance Department", "loc": "Beijing"}

> db.dept.drop ()

True

> db.dept.find ()

>

MongoDB data recovery

Mongodb uses the mongorestore command to restore backed-up data.

Grammar

The mongorestore command script syntax is as follows:

> mongorestore-h-d dbname

●-- host,-h:

Server address where MongoDB resides. Default is: localhost:27017

●-- db,-d:

The database instance that needs to be restored, such as test. Of course, this name can be different from that of backup, such as test2.

●-drop:

When restoring, delete the current data first, and then restore the backed-up data. That is to say, after recovery, the data added and modified after backup will be deleted, so use it with caution.

●:

The last parameter of mongorestore, which sets the location of the backup data, for example: C:\ data\ dump\ test.

You cannot specify both the and-- dir options, and-- dir can also set the backup directory.

●-dir:

Specify the directory of the backup

Restore deleted collections

[root@D2-LZY245] # mongorestore-db=hr-collection=dept-dir=/root/dump/hr/dept.bson

2017-08-18T11:14:54.397+0800 checking for collection data in / root/dump/hr/dept.bson

2017-08-18T11:14:54.406+0800 reading metadata for hr.dept from / root/dump/hr/dept.metadata.json

2017-08-18T11:14:54.424+0800 restoring hr.dept from / root/dump/hr/dept.bson

2017-08-18T11:14:54.486+0800 no indexes to restore

2017-08-18T11:14:54.486+0800 finished restoring hr.dept (5 documents)

2017-08-18T11:14:54.486+0800 done

Delete database

> db.dropDatabase ()

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

> use hr

Switched to db hr

> show collections

>

Restore the deleted database

[root@D2-LZY245] # mongorestore-- db=hr-- dir=/root/dump/hr

2017-08-18T11:18:38.570+0800 the-db and-collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use-nsInclude instead

2017-08-18T11:18:38.570+0800 building a list of collections to restore from / root/dump/hr dir

2017-08-18T11:18:38.571+0800 reading metadata for hr.emps from / root/dump/hr/emps.metadata.json

2017-08-18T11:18:38.572+0800 reading metadata for hr.emp from / root/dump/hr/emp.metadata.json

2017-08-18T11:18:38.572+0800 reading metadata for hr.inventory from / root/dump/hr/inventory.metadata.json

2017-08-18T11:18:38.572+0800 reading metadata for hr.emps_infos from / root/dump/hr/emps_infos.metadata.json

2017-08-18T11:18:38.578+0800 restoring hr.inventory from / root/dump/hr/inventory.bson

2017-08-18T11:18:38.585+0800 restoring hr.emps from / root/dump/hr/emps.bson

2017-08-18T11:18:38.591+0800 restoring hr.emp from / root/dump/hr/emp.bson

2017-08-18T11:18:38.596+0800 restoring indexes for collection hr.inventory from metadata

2017-08-18T11:18:38.596+0800 no indexes to restore

2017-08-18T11:18:38.596+0800 finished restoring hr.emp (9 documents)

2017-08-18T11:18:38.596+0800 no indexes to restore

2017-08-18T11:18:38.596+0800 finished restoring hr.emps (11 documents)

2017-08-18T11:18:38.596+0800 restoring hr.emps_infos from / root/dump/hr/emps_infos.bson

2017-08-18T11:18:38.605+0800 no indexes to restore

2017-08-18T11:18:38.605+0800 finished restoring hr.emps_infos (11 documents)

2017-08-18T11:18:38.605+0800 finished restoring hr.inventory (10 documents)

2017-08-18T11:18:38.605+0800 reading metadata for hr.t_emps_job from / root/dump/hr/t_emps_job.metadata.json

2017-08-18T11:18:38.605+0800 reading metadata for hr.shop from / root/dump/hr/shop.metadata.json

2017-08-18T11:18:38.606+0800 reading metadata for hr.dept from / root/dump/hr/dept.metadata.json

2017-08-18T11:18:38.606+0800 reading metadata for hr.t_emps_sex from / root/dump/hr/t_emps_sex.metadata.json

2017-08-18T11:18:38.610+0800 restoring hr.t_emps_job from / root/dump/hr/t_emps_job.bson

2017-08-18T11:18:38.616+0800 restoring hr.dept from / root/dump/hr/dept.bson

2017-08-18T11:18:38.638+0800 restoring hr.t_emps_sex from / root/dump/hr/t_emps_sex.bson

2017-08-18T11:18:38.639+0800 restoring hr.shop from / root/dump/hr/shop.bson

2017-08-18T11:18:38.639+0800 no indexes to restore

2017-08-18T11:18:38.639+0800 finished restoring hr.dept (5 documents)

2017-08-18T11:18:38.641+0800 no indexes to restore

2017-08-18T11:18:38.641+0800 finished restoring hr.t_emps_job (6 documents)

2017-08-18T11:18:38.641+0800 reading metadata for hr.depts from / root/dump/hr/depts.metadata.json

2017-08-18T11:18:38.651+0800 reading metadata for hr.news from / root/dump/hr/news.metadata.json

2017-08-18T11:18:38.652+0800 restoring hr.depts from / root/dump/hr/depts.bson

2017-08-18T11:18:38.657+0800 restoring hr.news from / root/dump/hr/news.bson

2017-08-18T11:18:38.660+0800 no indexes to restore

2017-08-18T11:18:38.660+0800 finished restoring hr.t_emps_sex (2 documents)

2017-08-18T11:18:38.661+0800 reading metadata for hr.loc from / root/dump/hr/loc.metadata.json

2017-08-18T11:18:38.661+0800 restoring indexes for collection hr.shop from metadata

2017-08-18T11:18:38.661+0800 no indexes to restore

2017-08-18T11:18:38.661+0800 finished restoring hr.depts (2 documents)

2017-08-18T11:18:38.661+0800 reading metadata for hr.phones from / root/dump/hr/phones.metadata.json

2017-08-18T11:18:38.661+0800 restoring indexes for collection hr.news from metadata

2017-08-18T11:18:38.666+0800 restoring hr.loc from / root/dump/hr/loc.bson

2017-08-18T11:18:38.670+0800 finished restoring hr.shop (7 documents)

2017-08-18T11:18:38.670+0800 reading metadata for hr.info from / root/dump/hr/info.metadata.json

2017-08-18T11:18:38.674+0800 finished restoring hr.news (4 documents)

2017-08-18T11:18:38.679+0800 restoring hr.phones from / root/dump/hr/phones.bson

2017-08-18T11:18:38.684+0800 no indexes to restore

2017-08-18T11:18:38.684+0800 finished restoring hr.loc (2 documents)

2017-08-18T11:18:38.687+0800 restoring hr.info from / root/dump/hr/info.bson

2017-08-18T11:18:38.687+0800 restoring indexes for collection hr.phones from metadata

2017-08-18T11:18:38.697+0800 finished restoring hr.phones (0 documents)

2017-08-18T11:18:38.697+0800 no indexes to restore

2017-08-18T11:18:38.697+0800 finished restoring hr.info (0 documents)

2017-08-18T11:18:38.697+0800 done

> show collections

Dept

Depts

Emp

Emps

Emps_infos

Info

Inventory

Loc

News

Phones

Shop

T_emps_job

T_emps_sex

> db.dept.find ()

{"_ id": ObjectId ("599504e70184ff511bf02be1"), "deptno": 11, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be2"), "deptno": 12, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be3"), "deptno": 13, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e80184ff511bf02be4"), "deptno": 14, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599505240184ff511bf02be5"), "deptno": 16, "dname": "Finance Department", "loc": "Beijing"}

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