Back up and restore a MongoDB Serverless instance using MongoDB tools

Updated at:

You can use the mongodump and mongorestore tools provided by MongoDB to back up and restore a MongoDB Serverless instance.

Background information

The Serverless console does not currently support backup and recovery. You must use the official MongoDB tools to perform these tasks.

Precautions

  • Ensure that version 4.2 of mongodump and mongorestore is installed. For more information, see the official Install MongoDB document.
  • Backups of a Serverless instance created using MongoDB tools are full logical backups. To ensure data consistency, stop all services and write operations to the source database before you start the backup.
  • Running the mongodump command overwrites historical backup files in the dump folder. If you previously used the mongodump command to back up the source database, move the backup files from the dump folder to another directory and ensure that the dump folder is empty.
  • Run the mongodump and mongorestore commands on the server where MongoDB is installed, not in the mongo shell environment.
  • You must have read and write permissions for the target database.
  • You must request a public endpoint. For more information, see (Optional) Request a public endpoint.

Back up a Serverless instance database using mongodump

  1. Log on to the MongoDB console.
  2. In the navigation pane on the left, click Serverless Instances.
  3. In the upper-left corner of the page, select the resource group and region where the instance is located.
  4. Click the ID of the target instance, or click Manage in the Actions column for the instance.
  5. In the navigation pane on the left of the instance details page, click Database Connections.
  6. Find and copy the public endpoint.
  7. On the local server where mongodump and mongorestore are installed, run the following command to back up the database.
    mongodump --host <mongodb_host>:3717 -u <username> --authenticationDatabase admin
    Note
    • <host>: The public endpoint of the Serverless instance.
    • <username>: The name of the database account for the Serverless instance. You can find the Account Name in the Connection Information section of the Basic Information page in the MongoDB console.

    Example:

    mongodump --host dds-t4n**********-pub.mongodb.singapore.rds.aliyuncs.com:3717 -u user12345 --authenticationDatabase admin
    Note If a connection error occurs, add the IP address of the local server to the whitelist of the Serverless instance. For more information, see Configure a whitelist.
  8. When prompted with Enter password:, enter the password for the database account. The database backup starts. If you forgot the password, you can reset it. For more information, see (Optional) Reset a password.
    Note Wait for the backup to complete. The data is backed up to the dump folder in the current directory.

Restore a Serverless instance database using mongorestore

  1. Log on to the MongoDB console.
  2. In the navigation pane on the left, click Serverless Instances.
  3. In the upper-left corner of the page, select the resource group and region where the instance is located.
  4. Click the ID of the target instance, or click Manage in the Actions column for the instance.
  5. In the navigation pane on the left of the instance details page, click Database Connections.
  6. Find and copy the public endpoint.
  7. On the local server where mongodump and mongorestore are installed, run the following command to import the backup data to the Serverless instance database.
    mongorestore --host <mongodb_host>:3717 -u <username> -d <database>  <dir> --authenticationDatabase admin
    Note
    • <mongodb_host>: The public endpoint of the Serverless instance.
    • <username>: The name of the database account for the Serverless instance. You can find the Account Name in the Connection Information section of the Basic Information page in the MongoDB console.
    • <database>: The database to which you want to import data. If the backup file contains multiple databases, repeat this step to import the other databases.
    • <dir>: The directory that contains the database backup file.

    Example:

    • Import the mongodbtest database from the backup file:
      mongorestore --host dds-t4n**********-pub.mongodb.singapore.rds.aliyuncs.com:3717 -u user12345 -d mongodbtest /dump/mongodbtest --authenticationDatabase admin
    • Import the test123 database from the backup file:
      mongorestore --host dds-t4n**********-pub.mongodb.singapore.rds.aliyuncs.com:3717 -u user12345 -d test123 /dump/test123 --authenticationDatabase admin
    Note If a connection error occurs, add the IP address of the local server to the whitelist of the Serverless instance. For more information, see Configure a whitelist.
  8. When prompted with Enter password:, enter the password for the database account. The data import starts. If you forgot the password, you can reset it. For more information, see (Optional) Reset a password.
    Note Upon completion of the recovery, the data is imported into the Serverless instance database.