This topic describes how to restore the data of an ApsaraDB for MongoDB instance to user-created MongoDB databases by using physical backup. Before you start the data restoration, you must download physical backup files of the ApsaraDB for MongoDB instance in the ApsaraDB for MongoDB console.

Prerequisites

  • The ApsaraDB for MongoDB instance is a replica set instance.
  • The TDE feature is disabled for the ApsaraDB for MongoDB instance.
  • The storage engine of the ApsaraDB for MongoDB instance is WiredTiger or RocksDB. If the storage engine is TerarkDB, use logical backup to restore data to user-created MongoDB databases. For more information, see Restore data of an ApsaraDB for MongoDB instance to user-created MongoDB databases by using logical backup.
    Note If the storage engine is RocksDB, you must compile and install a MongoDB program for user-created MongoDB databases to support the RocksDB storage engine.
  • The version of the backup database is the same as that of the user-created MongoDB instance.

Database version requirements

MongoDB instance user-created MongoDB database
3.2 3.2 or 3.4
3.4 3.4
4.0 4.0
4.2 4.2

Formats of physical backup files

Physical backup file format File extension Description
tar compressed package .tar.gz ApsaraDB for MongoDB instances that were created before March 26, 2019 have physical backup files in .tar format.
xbstream file package _qp.xb ApsaraDB for MongoDB instances that were created on or after March 26, 2019 have physical backup files in xbstream format.
Note You must decompress the two types of packages in different ways. For more information, see Download and decompress a physical backup file.

Environment preparation

The following procedure uses an Alibaba Cloud ECS instance created from an image that contains Ubuntu 16.04 (64-bit). For more information, see Create an ECS instance.

Note
  • MongoDB of the required version is installed on the ECS instance. For more information about the installation procedure, see MongoDB official documentation.
  • The /path/to/mongo/data directory of the ECS instance is used for the user-created MongoDB databases. This directory is empty.

Download and decompress a physical backup file

  1. Download the physical backup file of an ApsaraDB for MongoDB instance. You can also use the wget command to download it.
  2. Copy the downloaded physical backup file to the /path/to/mongo/data/ directory.
  3. Decompress the physical backup file.
    • If the extension of the physical backup file is .tar.gz, for example, the file name is hins20190412.tar.gz, run the following commands to decompress the file:
      cd /path/to/mongo/data/
      tar xzvf hins20190412.tar.gz 
      Figure 1. Decompression result
      Decompression result
    • If the extension of the physical backup file is _qp.xb, for example, the file name is hins20190412_qp.xb, follow these steps:
      1. Install the percona-xtrabackup tool.
        apt-get update
        apt install percona-xtrabackup
      2. Visit QuickLZ to download the qpress tool package.
      3. Decompress the qpress tool package and install the tool.
        tar xvf qpress-11-linux-x64.tar
        chmod 775 qpress
        cp qpress /usr/bin
      4. Decompress the physical backup file. In this example, the file is hins20190412_qp.xb.
        cd /path/to/mongo/data/
        cat hins20190412_qp.xb | xbstream -x -v
        innobackupex --decompress --remove-original /path/to/mongo/data
        Figure 2. Decompression result
        Decompression result

Restore data to user-created MongoDB databases in standalone mode

  1. Create a mongod.conf configuration file in the /path/to/mongo directory.
    touch mongod.conf
  2. Customize the mongod.conf configuration file to meet the startup requirements of the MongoDB program.

    You can select a configuration template based on the storage engine of the ApsaraDB for MongoDB instance, and copy the selected configuration template to the mongod.conf configuration file. In this file, enable standalone startup and authorization.

    Note The configuration file sets the startup mode to the single node mode and enables the authentication function.
    • WiredTiger storage engine
      systemLog:
          destination: file
          path: /path/to/mongo/mongod.log
          logAppend: true
      security:
          authorization: enabled
      storage:
          dbPath: /path/to/mongo/data
          directoryPerDB: true
      net:
          port: 27017
          unixDomainSocket:
              enabled: false
      processManagement:
          fork: true
          pidFilePath: /path/to/mongo/mongod.pid
      Note By default, ApsaraDB for MongoDB uses the WiredTiger storage engine, with the directoryPerDB option enabled. Therefore, this option is set to true in the preceding configuration.
    • RocksDB
      systemLog:
          destination: file
          path: /path/to/mongo/logs/mongod.log
          logAppend: true
      security:
          authorization: enabled
      storage:
          dbPath: /path/to/mongo/data
              engine: rocksdb
      net:
          port: 27017
          unixDomainSocket:
              enabled: false
      processManagement:
          fork: true
          pidFilePath: /path/to/mongo/logs/mongod.pid
  3. Use the mongod.conf configuration file to start the MongoDB program.
    /usr/bin/mongod -f /path/to/mongo/mongod.conf
  4. In the ECS instance, log on to a user-created MongoDB database by using the mongo shell.
    mongo --host 127.0.0.1 -u <username> -p <password> --authenticationDatabase admin
    Parameter description:
    • <username>: a username of databases in the ApsaraDB for MongoDB instance. The initial username is root.
    • <password>: the password of the preceding account.

Start the MongoDB program in replica set mode

By default, the physical backup file of an ApsaraDB for MongoDB instance contains the replica set configuration of this instance. You must start the MongoDB program in standalone mode. Otherwise, user-created MongoDB databases may be inaccessible.

If you want to start the MongoDB program in replica set mode, you must restore data to user-created MongoDB databases in standalone mode first, and then follow these steps:

  1. In the ECS instance, log on to a user-created MongoDB database by using the mongo shell.
  2. Delete the original replica set configuration.
    use local
    db.system.replset.remove({})
  3. Shut down the MongoDB program.
    use admin
    db.shutdownServer()                   
  4. Add replication-related configurations to the mongod.conf configuration file in the /path/to/mongo/ directory. For more information about the commands used, visit Deploy a Replica Set.
  5. Use the mongod.conf configuration file to start the MongoDB program.
    /usr/bin/mongod -f /path/to/mongo/mongod.conf
  6. Add nodes to the replica set and initialize the replica set.
    Note This step uses an rs.initiate() command. For more information about the command, visit rs.initiate().