All Products
Search
Document Center

ApsaraDB for MongoDB:Restore the instance data to a self-managed MongoDB database by using logical backup

Last Updated:Nov 01, 2024

This topic describes how to use mongorestore to restore logical backup files of an ApsaraDB for MongoDB instance to a self-managed MongoDB database.

Prerequisites

  • The instance uses local SSDs.

  • The version of the self-managed MongoDB database is the same as the database version of the ApsaraDB for MongoDB instance. This ensures compatibility.

Background information

MongoDB provides a set of backup and recovery tools: mongodump and mongorestore. Logical backups of ApsaraDB for MongoDB instances are generated by using mongodump. You can use mongorestore to restore logical backups to self-managed MongoDB databases.

Precautions

  • The mongorestore tool of an earlier version is not supported in MongoDB of a later version. We recommend that you select a mongorestore version that is compatible with your MongoDB version. For more information, see mongorestore.

  • If the self-managed MongoDB database uses the sharded cluster architecture, you must set the <hostname> parameter in the command to import the backup data to the endpoint of the mongos node in the self-managed MongoDB database.

  • If the self-managed MongoDB database uses the sharded cluster architecture, you must add --nsExclude="config.*" to the command. Otherwise, an error may occur during data restoration.

  • When you restore data from a sharded cluster instance to a self-managed MongoDB database, you must download the backup data of each shard node in the sharded cluster instance and import the backup data to the self-managed MongoDB database. If the sharded cluster instance contains orphaned documents, dirty data may occur in the self-managed database. If you restore the backup files of multiple shards to the same database of the sharded cluster architecture, you add the drop parameter only for the backup files of the first shard.

Preparations

Download a version of MongoDB that is the same as the database version of the ApsaraDB for MongoDB instance and install the MongoDB version on the client where the self-managed MongoDB database resides. The client can be an on-premises server or an Elastic Compute Service (ECS) instance. For more information about the installation method, see Install MongoDB.

Procedure

  1. Log on to the ApsaraDB for MongoDB console.

  2. In the left-side navigation pane, click Replica Set Instances or Sharded Cluster Instances based on the instance type.

  3. In the upper-left corner of the page, select the resource group and region to which the instance belongs.

  4. Click the ID of an instance, or click More icon in the Actions column corresponding to the instance and select Manage.

  5. Back up a database.

    1. In the upper-right corner of the instance details page, click Back up Instance.

    2. In the Back up Instance panel, set Backup Method to Logical Backup.

    3. Click OK. Then, wait for the backup to complete.

  6. Download the backup file. For more information about the download method, see Download backup files.

  7. Copy the downloaded backup file to the client where the self-managed MongoDB database resides. The client is equipped with the mongorestore tool.

  8. Run the following command to import the backup data to the self-managed MongoDB database:

    mongorestore -h <hostname> --port <server port> -u <username> -p <password> --drop --gzip --archive=<backupfile> -vvvv --stopOnError

    You must specify the following parameters:

    • <hostname>: the address of the server where the self-managed MongoDB database resides. Enter 127.0.0.1 for the local host.

      If the self-managed MongoDB database uses the sharded cluster architecture, you must set this parameter to the endpoint of the mongos node in the self-managed MongoDB database.

    • <server port>: the port used to connect to the self-managed MongoDB database.

    • <username>: the account used to log on to the self-managed MongoDB database. Make sure that the account has permissions on all databases. We recommend that you use the root account.

    • <password>: the account password used to log on to the self-managed MongoDB database.

    • <backupfile>: the name of the downloaded logical backup file.

    You can add the following parameters to the command:

    • --drop: deletes the collection before the backup file is restored.

      Note

      If you restore the backup files of multiple shards to the same database of the sharded cluster architecture, you add the drop parameter only for the backup files of the first shard.

    • --gzip: decompresses backup data that is in the GZIP format.

      Note

      This parameter is supported from MongoDB 3.1.4. For more information, see mongo-tools.

    • -vvvv: specifies how detailed the backup data is. The greater the number of v, the more detailed the backup data is.

    • --stopOnError: stops the data import in the event of an error.

    • -- nsExclude: does not restore the matched data sets. Example: --nsExclude="config.*".

    Example:

    mongorestore -h 127.0.0.1 --port 27017 -u root -p ******** --drop --gzip --archive=hins1111_data_20190710.ar -vvvv --stopOnError