MongoDB - Backups and Restores with the CLI

Find out how to back up and restore your Public Cloud Databases for MongoDB database using the CLI

Last updated December 13th, 2021

Objective

Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

This guide explains how to manually back up and restore your Public Cloud Database for MongoDB.

Requirements

Concept

Public Cloud Databases perform automatic daily backups of your databases. However, you might need to perform manual backup and restore operations to double-ensure data safety on your end, or for example in order to export and import data across database instances.

Instructions

Ensure you can connect to your database

Follow the guide MongoDB - Connect with CLI in order to ensure you can connect to your database. That step ensures IP filtering is set up properly and that you have a suitable service URI available.

Install the MongoDB Database Tools

Follow the MongoDB documentation to install the MongoDB Database Tools for your platform. You'll need the mongodump and mongorestore binaries in order to enact the rest of this procedure.

Perform a backup of your data

Back up your database to a file using:

$ mongodump --gzip --archive=path/to/backup.gz --readPreference=secondaryPreferred \
  --uri="mongodb://<username>:<password>@<hostname>/admin?tls=true"

If your cluster is e.g. a 3-node replica set, the command might look more like:

$ mongodump --gzip --archive=path/to/backup.gz --readPreference=secondaryPreferred \
  --uri="mongodb://<username>:<password>@<hostname node1>,<hostname node 2>,<hostname node 3>/admin?replicaSet=replicaset&tls=true"

The MongoDB user needs to have sufficient privileges to perform the backup operation. This can be ensured by giving this user the backup role.

This operation might take some time - from a few seconds to a few hours, depending on the amount of data your database holds and your network connection. Once it completes, the file path/to/backup.gz will hold a backup of your data.

Restore your data from a previous backup

This operation might clobber existing data in your database with the data from the backup you restore. Make sure this is what you actually want!

mongorestore merges data

After the restore operation, any data that was stored in your database which isn't also part of the backup file (e.g. other collections or other keys) will be left untouched: That is to say, mongorestore merges data from your backup file with preexisting data in the database.

Restore your database from a backup using:

$ mongorestore --gzip --archive=path/to/backup.gz \
  --uri="mongodb://<username>:<password>@<hostname>/admin?tls=true"

Again, if your cluster is e.g. a 3-node replica set, the command might look more like:

$ mongorestore --gzip --archive=path/to/backup.gz \
  --uri="mongodb://<username>:<password>@<hostname node1>,<hostname node 2>,<hostname node 3>/admin?replicaSet=replicaset&tls=true"

The MongoDB user needs to have sufficient privileges to perform the restore operation. This can be ensured by giving this user the restore role.

Again, depending on the volume of data and your network connection, this operation might take some time. Once it completes, any data contained in your backup file is present in your database.

We want your feedback!

We would love to help answer questions and appreciate any feedback you may have. Join our community of users on https://community.ovh.com/en/.

Are you on Discord? Connect to our channel at https://discord.gg/PwPqWUpN8G and interact directly with the team that builds our databases service!


¿Le ha resultado útil esta guía?

Si lo desea, también puede enviarnos sus sugerencias para ayudarnos a mejorar nuestra documentación.

Imágenes, contenido, estructura...: ayúdenos a mejorar nuestra documentación con sus sugerencias.

No podemos tratar sus solicitudes de asistencia a través de este formulario. Para ello, haga clic en "Crear un tíquet" .

¡Gracias! Tendremos en cuenta su opinión.


Otras guías que podrían interesarle...

OVHcloud Community

¡Acceda al espacio de la OVHcloud Community! Resuelva sus dudas, busque información, publique contenido e interactúe con otros miembros de la comunidad.

Discuss with the OVHcloud community

A partir del 1 de enero de 2015, con arreglo a la Directiva 2006/112/CE modificada, los precios IVA incluido pueden variar según el país de residencia del cliente (por defecto, los precios con IVA incluyen el IVA español vigente).