2 / 4
Aug 2024

Hi,

What is the proper way of restoring the backup of a single collection downloaded locally? I can see the files structure for specific collection are the following:

  • *.json.gz files
  • metadata.json

The metada.json file looks like an output of the mongodump command while the .json.gz files look like they are from mongoexport.

What should be the recommended way to restore such structure?

Hi there!

Since there wasn’t a reply to your message, I tried to run it by our AI bot. Here is the response I got :slight_smile:


Based on the file structure you’ve described, it appears you have a backup that combines elements from different MongoDB backup methods. The .json.gz files are likely from mongoexport, while the metadata.json file is typically associated with mongodump output. To restore this backup, we’ll need to use a combination of mongoimport and mongorestore. 1

Here’s a step-by-step procedure to restore your backup:

1. Prepare the Data

First, you need to extract the .json.gz files:

bash

gunzip -r <downloadFolder>

2. Import the Collection Data

Use mongoimport to import the JSON files into your MongoDB instance:

bash

mongoimport --uri "<connectionString>" --mode=upsert -d <database_name> -c <collection_name> --file <path_to_json_file> --type json

Repeat this command for each .json file in your backup.

3. Restore Indexes and Other Metadata

Use mongorestore to restore the metadata, which includes index information:

bash

mongorestore --uri "<connectionString>" --db <database_name> --collection <collection_name> <path_to_metadata.json>

1 2

4. Verify the Restoration

After completing these steps, connect to your MongoDB instance and verify that the collection has been restored correctly, including all documents and indexes.

Important Considerations:

  • The --mode=upsert option in the mongoimport command allows handling of duplicate documents from the archive. 1
  • Make sure you have sufficient permissions to perform these operations on the target database.
  • If you’re restoring to a replica set or sharded cluster, ensure you’re connecting to the primary node or a mongos instance.
  • Be cautious when restoring to an existing database/collection, as this process may overwrite existing data. Consider restoring to a temporary database first for verification before replacing your production data.
  • If you encounter any issues with index recreation, you may need to manually create the indexes using the information from the metadata.json file.

By following these steps, you should be able to restore your single collection backup successfully. Remember to adjust the database name, collection name, and file paths according to your specific backup files and target MongoDB instance. 1 2

Sources


Import Archive from S3
Docs Home
MongoDB Atlas
Backup, Restore, and Archive

Restore a Collection from Queryable Legacy Backup
MongoDB Atlas
Backup, Restore, and Archive
Legacy Backups (Deprecated)

Thanks for the response Joel.

Although, I’m afraid the documentation bot is wrong here. In the 3rd part it suggests to run mongorestore with the path to metadata.json file. But AFAIK mongorestore doesn’t support the JSON input but only BSON format, is that correct?

You are correct. I guess the AI aren’t going to take over the world just yet :smiley:

The metadata.json would have been produced by mongodump. I should hold information about the collections and indexes if I am not mistaken.

You should run the mongorestore tool on the folder that contains those files.

Say your .gz. and metadata.json files are in the dump/ folder, run

mongorestore <connection-string> dump/