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

How to realize backup and recovery in MongoDB

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to achieve backup and recovery in MongoDB. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

One mongoexport/mongoimport

[root@svnhost bin] # pwd

/ usr/local/mongodb/bin

[root@svnhost bin] #. / mongoexport-help

Export test01 table under chendb

[root@svnhost bin] #. / mongoexport-d chendb-c test01-o / mongobak/chendb_test01.bak

[root@svnhost bin] #. / mongoexport-d chendb-c test02-o / mongobak/chendb_test02.bak

View backup contents

[root@svnhost bin] # cat / mongobak/chendb_test01.bak

{"_ id": {"$oid": "5b8f418f1c1560b2bb03cb7b"}, "url": "www.sina.com"}

{"_ id": {"$oid": "5b8f41c51c1560b2bb03cb7c"}, "url": "ccc-10"}

.

{"_ id": {"$oid": "5b8f41d81c1560b2bb03cb90"}, "url": 19.0}

{"_ id": {"$oid": "5b8f41d81c1560b2bb03cb91"}, "url": 20.0}

{"_ id": {"$oid": "5b8f74e51c1560b2bb03cb92"}, "url": "www.baidu.com"}

Restore single tabl

Restore and rename the test01 table to test01aaa

[root@svnhost ~] # cd / usr/local/mongodb/bin/

[root@svnhost bin] #. / mongoimport-d chendb-c test01aaa / mongobak/chendb_test01.bak

2018-09-05T17:39:09.592+0800 connected to: localhost

2018-09-05T17:39:09.807+0800 imported 24 documents

MongoDB Enterprise > show collections

Test01

Test01aaa

Test02

View data

MongoDB Enterprise > db.test01aaa.find ()

{"_ id": ObjectId ("5b8f41c51c1560b2bb03cb7c"), "url": "ccc-10"}

{"_ id": ObjectId ("5b8f41c51c1560b2bb03cb7d"), "url": "ccc-11"}

{"_ id": ObjectId ("5b8f41c51c1560b2bb03cb7e"), "url": "ccc-12"}

.

{"_ id": ObjectId ("5b8f41d81c1560b2bb03cb90"), "url": 19}

{"_ id": ObjectId ("5b8f41d81c1560b2bb03cb91"), "url": 20}

{"_ id": ObjectId ("5b8f74e51c1560b2bb03cb92"), "url": "www.baidu.com"}

Delete table and simulate fault

MongoDB Enterprise > show collections

Test01

Test01aaa

Test02

MongoDB Enterprise > db.test02.drop ()

True

MongoDB Enterprise > db.test02.find ()

Restore table test02

[root@svnhost bin] #. / mongoimport-d chendb-c test02 / mongobak/chendb_test02.bak

2018-09-05T17:49:47.912+0800 connected to: localhost

2018-09-05T17:49:48.089+0800 imported 1 document

View data

MongoDB Enterprise > db.test02.find ()

{"_ id": ObjectId ("5b8f752f943b3cbaa33a5518"), "url": "www.baidu.com"}

Two mongodump/mongorestore

[root@svnhost bin] #. / mongodump-help

Back up the entire chendb library

Root@svnhost bin] #. / mongodump-d chendb-o / mongobak/db_backup/

2018-09-06T10:24:19.545+0800 writing chendb.test01 to

2018-09-06T10:24:19.545+0800 writing chendb.test02 to

2018-09-06T10:24:19.546+0800 writing chendb.test01aaa to

2018-09-06T10:24:19.547+0800 done dumping chendb.test01 (24 documents)

2018-09-06T10:24:19.563+0800 done dumping chendb.test02 (1 document)

2018-09-06T10:24:19.579+0800 done dumping chendb.test01aaa (24 documents)

View backup files

[root@svnhost bin] # unset LANG

[root@svnhost bin] # ll-rth / mongobak/db_backup/chendb/

Total 24K

-rw-r--r-- 1 root root 127 Sep 6 10:24 test01.metadata.json

-rw-r--r-- 1 root root 127 Sep 6 10:24 test02.metadata.json

-rw-r--r-- 1 root root 130 Sep 6 10:24 test01aaa.metadata.json

-rw-r--r-- 1 root root 914 Sep 6 10:24 test01.bson

-rw-r--r-- 1 root root 45 Sep 6 10:24 test02.bson

-rw-r--r-- 1 root root 914 Sep 6 10:24 test01aaa.bson

Back up all databases under MongoDB

[root@svnhost bin] #. / mongodump-o / mongobak/db_fullbackup/

2018-09-06T10:28:35.465+0800 writing admin.system.version to

2018-09-06T10:28:35.465+0800 done dumping admin.system.version (1 document)

2018-09-06T10:28:35.465+0800 writing chendb.test01 to

2018-09-06T10:28:35.465+0800 writing chendb.test01aaa to

2018-09-06T10:28:35.465+0800 writing chendb.test02 to

2018-09-06T10:28:35.466+0800 done dumping chendb.test01 (24 documents)

2018-09-06T10:28:35.466+0800 done dumping chendb.test02 (1 document)

2018-09-06T10:28:35.482+0800 done dumping chendb.test01aaa (24 documents)

Specify username, password and port for backup

[root@svnhost bin] #. / mongodump-d chendb-o / mongobak/newdbbackup-u cjc-p cjc-- port 27017

2018-09-06T16:37:45.209+0800 writing chendb.test01aaa to

2018-09-06T16:37:45.209+0800 writing chendb.test01bbb to

2018-09-06T16:37:45.209+0800 writing chendb.test01 to

2018-09-06T16:37:45.209+0800 writing chendb.test02 to

2018-09-06T16:37:45.250+0800 done dumping chendb.test02 (1 document)

2018-09-06T16:37:45.252+0800 done dumping chendb.test01bbb (24 documents)

2018-09-06T16:37:45.252+0800 done dumping chendb.test01aaa (24 documents)

2018-09-06T16:37:45.277+0800 done dumping chendb.test01 (24 documents)

Back up the test01 table in the chendb library

[root@svnhost bin] #. / mongodump-d chendb-c test01-o / mongobak/chendb_test01/

2018-09-06T10:42:39.354+0800 writing chendb.test01 to

2018-09-06T10:42:39.354+0800 done dumping chendb.test01 (24 documents)

View backup

[root@svnhost bin] # ll-rth / mongobak/chendb_test01/chendb/

Total 8.0K

-rw-r--r-- 1 root root 127 Sep 6 10:42 test01.metadata.json

-rw-r--r-- 1 root root 914 Sep 6 10:42 test01.bson

Restore single tabl

[root@svnhost bin] # pwd

/ usr/local/mongodb/bin

[root@svnhost bin] # mongorestore-d chendb-c test01bbb / mongobak/chendb_test01/chendb/test01.bson

2018-09-06T15:43:22.572+0800 checking for collection data in / mongobak/chendb_test01/chendb/test01.bson

2018-09-06T15:43:22.572+0800 reading metadata for chendb.test01bbb from / mongobak/chendb_test01/chendb/test01.metadata.json

2018-09-06T15:43:22.755+0800 restoring chendb.test01bbb from / mongobak/chendb_test01/chendb/test01.bson

2018-09-06T15:43:22.816+0800 no indexes to restore

2018-09-06T15:43:22.816+0800 finished restoring chendb.test01bbb (24 documents)

2018-09-06T15:43:22.816+0800 done

View data

MongoDB Enterprise > db.test01bbb.find ()

{"_ id": ObjectId ("5b8f41d81c1560b2bb03cb87"), "url": 10}

{"_ id": ObjectId ("5b8f41d81c1560b2bb03cb88"), "url": 11}

.

{"_ id": ObjectId ("5b8f41d81c1560b2bb03cb90"), "url": 19}

{"_ id": ObjectId ("5b8f74e51c1560b2bb03cb92"), "url": "www.baidu.com"}

Restore a single database and rename it to chendbaaa

[root@svnhost bin] # mongorestore-d chendbaaa / mongobak/db_backup/chendb

2018-09-06T15:52:04.586+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

2018-09-06T15:52:04.586+0800 building a list of collections to restore from / mongobak/db_backup/chendb dir

2018-09-06T15:52:04.587+0800 reading metadata for chendbaaa.test01 from / mongobak/db_backup/chendb/test01.metadata.json

2018-09-06T15:52:04.587+0800 reading metadata for chendbaaa.test01aaa from / mongobak/db_backup/chendb/test01aaa.metadata.json

2018-09-06T15:52:04.756+0800 restoring chendbaaa.test01aaa from / mongobak/db_backup/chendb/test01aaa.bson

2018-09-06T15:52:04.940+0800 restoring chendbaaa.test01 from / mongobak/db_backup/chendb/test01.bson

2018-09-06T15:52:04.944+0800 no indexes to restore

2018-09-06T15:52:04.944+0800 finished restoring chendbaaa.test01aaa (24 documents)

2018-09-06T15:52:04.944+0800 no indexes to restore

2018-09-06T15:52:04.944+0800 finished restoring chendbaaa.test01 (24 documents)

2018-09-06T15:52:04.944+0800 reading metadata for chendbaaa.test02 from / mongobak/db_backup/chendb/test02.metadata.json

2018-09-06T15:52:05.098+0800 restoring chendbaaa.test02 from / mongobak/db_backup/chendb/test02.bson

2018-09-06T15:52:05.102+0800 no indexes to restore

2018-09-06T15:52:05.102+0800 finished restoring chendbaaa.test02 (1 document)

2018-09-06T15:52:05.102+0800 done

View the database

MongoDB Enterprise > show dbs

Admin 0.000GB

Chendb 0.000GB

Chendbaaa 0.000GB

Config 0.000GB

Local 0.000GB

MongoDB Enterprise > use chendbaaa

Switched to db chendbaaa

MongoDB Enterprise > show collections

Test01

Test01aaa

Test02

MongoDB Enterprise > db.test02.find ()

{"_ id": ObjectId ("5b8f752f943b3cbaa33a5518"), "url": "www.baidu.com"}

Three automatic backups

Backup scheduled task

[root@cjcos ~] # crontab-l

01 03 * / dbbackup/mongodb/mongo_bak.sh

Backup script

Back up all databases under instances 27017, 27018, 27019

And automatically delete all backup files at the beginning of 2020 and 3 days ago

[root@cloud3 ~] # cat / dbbackup/mongodb/mongo_bak.sh

#! / bin/bash

DATE= `date +% Yee% masked% d`

Mkdir-p / dbbackup/mongodb/$DATE/ {27017pm 27018pm 27019}

/ kingdee/mongodb/bin/mongodump-o / dbbackup/mongodb/$DATE/27017-- port 27017

/ kingdee/mongodb/bin/mongodump-o / dbbackup/mongodb/$DATE/27018-- port 27018

/ kingdee/mongodb/bin/mongodump-o / dbbackup/mongodb/$DATE/27019-- port 27019

Find / dbbackup/mongodb-mtime + 3-name "2020*"-exec rm-rf {}\

View backup files

[root@cjcos ~] # cd / dbbackup/mongodb/

[root@cjcos mongodb] # du-sh *

32G 2020_03_21

32G 2020_03_22

32G 2020_03_23

4.0K mongo_bak.sh

Restore by backing up files

Restore chendb

[root@svnhost bin] # pwd

/ usr/local/mongodb/bin

[root@cjcos bin] # mongorestore-d chendbccc / dbbackup/mongodb/2020_03_23/27017/chendb

After reading the above, do you have any further understanding of how to achieve backup and recovery in MongoDB? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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