Updated on 2023-05-23 GMT+08:00

Restoring a Replica Set Backup to an On-Premises Database

To restore a DB instance backup file to an on-premises database, you can only use databases on Linux.

This section uses the Linux operating system as an example to describe how to restore the downloaded backup file of a replica set instance to your on-premises databases. For details about how to download backup files, see Downloading Backup Files.

Precautions

  • MongoDB client 3.4 has been installed on your on-premises MongoDB database.

Procedure

  1. Log in to the server on which the on-premises databases are deployed.

    Assume that /path/to/mongo is the directory for restoration, and /path/to/mongo/data is the directory for storing the backup file.

  2. Before the restoration, ensure that the /path/to/mongo/data directory is empty.

    cd /path/to/mongo/data/

    rm -rf *

  3. Copy and paste the downloaded backup file package to /path/to/mongo/data/ and decompress it.

    lz4 -d xxx_.tar.gz |tar -xC /path/to/mongo/data/

  4. Create the mongod.conf configuration file in /path/to/mongo.

    touch mongod.conf

  5. Start the database in single-node mode.

    1. Modify the mongod.conf file to meet the backup startup configuration requirements.

      The following is a configuration template for backup startup:

      systemLog:
          destination: file
          path: /path/to/mongo/mongod.log
          logAppend: true
      security:
          authorization: enabled
      storage:
          dbPath: /path/to/mongo/data
          directoryPerDB: true
          engine: wiredTiger
          wiredTiger:
              collectionConfig: {blockCompressor: snappy}
              engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
              indexConfig: {prefixCompression: true}
      net:
          http:
              enabled: false
          port: 27017
          bindIp: xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx
          unixDomainSocket:
              enabled: false
      processManagement:
          fork: true
          pidFilePath: /path/to/mongo/mongod.pid

      bindIp indicates the IP address bound to the database. This field is optional. If it is not specified, your local IP address is bound by default.

    2. Run the mongod.conf command to start the database.

      /usr/bin/mongod -f /path/to/mongo/mongod.conf

      /usr/bin/ is the directory that stores the mongod file of the installed MongoDB client.

    3. After the database is started, log in to the database using mongo shell to verify the restoration result.

      mongo --host <DB_HOST> -u <DB_USER> -p <PASSWORD> --authenticationDatabase admin

      • DB_HOST is the IP address bound to the database.
      • DB_USER is the database user. The default value is rwuser.
      • PASSWORD is the password for the database user, which is the password used for backing up the DB instance.

Starting the Database in Replica Set Mode

By default, the physical backup of the DDS DB instance contains the replica set configuration of the original DB instance. You need to start the database in single-node mode. Otherwise, the database cannot be accessed.

If you want to start the database in replica set mode, perform step 5 and then perform the following steps:

  1. Log in to the database using mongo shell.
  2. Remove the original replica set configuration.

    use local

    db.system.replset.remove({})

  3. Stop the database process.

    use admin

    db.shutdownServer()

  4. Add the replication configuration in the mongod.conf file in the /path/to/mongo/ directory. For details about the command usage, see Deploy a Replica Set.
  5. Run the mongod.conf command to start the database.

    /usr/bin/mongod -f /path/to/mongo/mongod.conf

    /usr/bin/ is the directory that stores the mongod file of the installed MongoDB client.

  6. Add the replica set members and initialize the replica set.

    Use the rs.initiate() command to perform the preceding step. For details, see rs.initiate().