preface

A few days ago, I used SSR to reconstruct the blog, and needed to back up the old data before. The previous approach was to directly back up all the files under the mongo/data directory…… (Yeah, THAT’s what I did before.)

On recent research, there are two ways to back up a Mongo database

As shown below, there is a blog database, which we will start to back up

1. The mongodump and mongorestore

Mongodump is an official backup tool provided by MongoDB, which can read data from the MongoDB database and generate BSON files. Mongodump is suitable for backing up and restoring MongoDB databases with a small amount of data, but not for backing up large amounts of data.

The backup

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

  • -h: address of the server where MongDB resides, for example, localhost:27017
  • -d: indicates the backup database
  • -c: indicates the backup data table
  • -o: indicates the location where backup data is stored

mongodump -h dbhost -d dbname -o dbdirectory

Let’s just run the command:

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

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

After the execution, we click on the blog directory to find the following suffix bson, JSON file, output a Bson file look, found is garbled:

cat articles.bson

In fact, our backup data is binary, we can not directly view the data, we need to combine with the bsondump of Mongo:

bsondump .\articles.bson

Emmm, you can view it now

restore

Similar to backup:

  • -h: address of the server where MongDB resides, for example, localhost:27017
  • -d: indicates the database to be recovered
  • -c: indicates the data table to be restored
  • : mongoRestore The last parameter, the location of the backup data

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

Let’s delete the blog database and start restoring it

Restore all tables in the blog database
 mongorestore -h localhost:27017 -d blog .\

You can also restore only specific tables
For example, restore only the articles table
mongorestore -h localhost:27017  -c articles  -d blog .\articles.bson
Copy the code

All data has been recovered ~

2. Mongoexport, mongoimport

The Mongoexport tool in Mongodb can export a collection as a JSON or CSV file.

Mongoexport can only be exported one table by one,…… .

export

Mongoexport’s usage and parameters are basically the same as mongodump’s:

  • -h: address of the server where MongDB resides, for example, localhost:27017
  • -d: indicates the backup database
  • -c: indicates the backup data table
  • -o: stores the backup data. The storage type, such as JSON or CSV, must be specified
Select articles from blog databasemongoexport -h localhost:27017 -d blog -c articles -o ./articles.json mongoexport -h localhost:27017 -d blog -c articles  -o ./articles.csvCopy the code

restore

  • -h: address of the server where MongDB resides, for example, localhost:27017
  • -d: the database to be restored
  • -c: indicates the data table to be restored
  • : mongoRestore The last parameter, the location of the 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
Copy the code

Recovery succeeded ~


END