Backup and recovery of Mongo database

Time:2021-1-13

preface

A while ago, I used SSR to reconstruct the blog. I need to back up the old data. The previous method is to directly back up all the files in the Mongo / data directory… (yes, that’s what I did before ~. ~)

Recently, there are two ways to back up the Mongo database

As shown in the figure below, there is a blog database, and we will start to back it up

Backup and recovery of Mongo database

1. mongodump、mongorestore

Mongodump is a backup tool provided by mongodb. It can read data from mongodb database and generate bson file. Mongodump is suitable for backup and recovery of mongodb database with small amount of data, but not for large amount of data.

backups

The syntax is as follows. Note that there are several parameters:

  • -h: Mongdb server address, such as localhost:27017
  • -d: Backup database
  • -c: Backup data table
  • -o: Backup data storage location

mongodump -h dbhost -d dbname -o dbdirectory

We run the command directly:

#Export blog database to current directory
mongodump -h localhost:27017 -d blog -o ./

#You can also export specific tables
#Export the articles table of blog database to the current directory
mongodump -h localhost:27017 -c articles -d blog -o ./

Backup and recovery of Mongo database

After the execution, we click the blog directory and find that there are files with the suffix bson and JSON below. When we output a bson file, we find that it is garbled:

cat articles.bson
Backup and recovery of Mongo database

In fact, the data we back up is binary, which we can’t see directly. We need to combine the bsondump provided by Mongo

bsondump .\articles.bson
Backup and recovery of Mongo database

Emmm, you can view it normally

recovery

Similar to backup:

  • -h: Mongdb server address, such as localhost:27017
  • -d: Database to be recovered
  • -c: Data table to be recovered
  • < Path >: the last parameter of mongorestore, the location of backup data

>mongorestore -h <hostname><:port> -d dbname <path>

Let’s delete the blog database first, and then start the recovery

#Recover all tables of blog database
 mongorestore -h localhost:27017 -d blog ./

#Of course, you can restore only specific tables
#For example, restore only the articles table
mongorestore -h localhost:27017  -c articles  -d blog ./articles.bson

Backup and recovery of Mongo database

Execution, all data recovered~

2. mongoexport、mongoimport

Mongoexport tool in mongodb can export a collection to a file in JSON format or CSV format.

Mongoexport can only be exported one table at a time~

Backup and recovery of Mongo database

export

The use and parameters of mongoexport are basically the same as mongodump

  • -h: Mongdb server address, such as localhost:27017
  • -d: Backup database
  • -c: Backup data table
  • -o: Backup data storage location, you must specify the storage type, such as JSON, CSV
#Export articles table of blog database
mongoexport -h localhost:27017 -d blog -c articles -o  ./articles.json
mongoexport -h localhost:27017 -d blog -c articles -o  ./articles.csv

Backup and recovery of Mongo database

recovery

  • -h: Mongdb server address, such as localhost:27017
  • -d: Recovered database
  • -c: Recovered data table
  • < Path >: the last parameter of mongorestore, the location of backup data
#Import from JSON
mongoimport -h localhost:27017 -d blog -c articles ./articles.json
#Import from csvf
mongoimport -h localhost:27017 -d blog -c articles ./articles.csv

Backup and recovery of Mongo database

The recovery was successful~


END