This topic describes how to use redis-port to restore data. You can restore the data of an ApsaraDB for Redis instance in the ApsaraDB for Redis console. You can run the script that is provided in this topic to download a backup of the instance. The script uses redis-port to restore data from the backup.

Prerequisites

  • The engine version of the source ApsaraDB for Redis instance is Redis 2.8 or 4.0.
  • The engine version of the destination ApsaraDB for Redis instance is 2.8, 4.0, or 5.0.

Background information

redis-port is an open source tool that can be used to migrate data between Redis databases. The tool can run multiple data migration tasks at the same time. In this example, redis-port is used in restore mode to restore the data of an ApsaraDB for Redis instance to a specified ApsaraDB for Redis instance.

Prerequisites

Notice The script provided in this example requires you to provide an AccessKey pair to complete the authentication before you can obtain the backup file of the instance. To avoid disclosing the AccessKey pair of your Alibaba Cloud account, we recommend that you create a Resource Access Management (RAM) user, authorize the RAM user, and create an AccessKey pair for the RAM user.
  1. Create a RAM user and grant the AliyunKvstoreFullAccess permission to the RAM user. This permission allows the RAM user to manage ApsaraDB for Redis instances.
    1. Log on to the RAM console.
    2. Create a RAM user.
    3. In the left-side navigation pane, choose Identities > Users.
    4. On the Users page, find the RAM user to which you want to grant permissions and click Add Permissions in the Actions column.
      Figure 1. Add permissions
      Add permissions
    5. In the dialog box that appears, enter AliyunKvstoreFullAccess in the search box and click the policy name to add the policy to the Selected section.
      Figure 2. Grant the AliyunKvstoreFullAccess permission
      Grant the AliyunKvstoreFullAccess permission
    6. Click OK.
    7. Click Complete.
  2. Create an AccessKey pair for the RAM user. For more information, see Create an AccessKey pair.

Procedure

  1. Perform the following operations based on the location where redis-port is installed:
    Notice
    • The ApsaraDB for Redis instances that are mentioned in the following table refer to the source and destination ApsaraDB for Redis instances. Perform the following operations on the source and destination ApsaraDB for Redis instances.
    • We recommend that you install redis-port on Elastic Compute Service (ECS) instances. You can connect to the source and destination ApsaraDB for Redis instances through a virtual private cloud (VPC) to achieve lower network latency and higher security.
    Location where redis-port is installed Operation
    ECS instances
    1. Make sure that the ECS instance and the ApsaraDB for Redis instance belong to the same VPC. They share the same VPC ID in the Basic Information section.
      Note
    2. Obtain the internal IP address of the ECS instance. For more information, see How do I query IP addresses of ECS instances.
    3. Add the internal IP address of the ECS instance to the whitelist of the ApsaraDB for Redis instance. For more information, see Configure an IP whitelist.
    On-premises machine
    1. By default, an ApsaraDB for Redis instance provides only an internal endpoint. You must apply for a public endpoint when you want to connect to an ApsaraDB for Redis instance over the Internet. For more information, see Apply for a public endpoint.
    2. Run the curl ipinfo.io |grep ip command on the on-premises machine to obtain the public IP address. The returned result is shown in the following figure.View public IP address results
    3. Add the public IP address of the on-premises machine to the whitelist of the ApsaraDB for Redis instance. For more information, see Configure an IP whitelist.
  2. Log on to the device where redis-port is installed. The device may be an ECS instance or an on-premises machine.
    Note In this example, an ECS instance that runs the Ubuntu 16.04.6 operating system is used.
  3. Run the following command to install the core library of OpenAPI Explorer:
    apt-get update
    apt install python-pip -y
    pip install --upgrade "pip < 21.0"
    pip install aliyun-python-sdk-core
  4. Run the following command to download the script package:
    wget 'https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/120287/cn_zh/1618924922413/redis-rdb-auto-restore.tar'
    Note The script package contains the script rdb_restore.py, redis-port, and other files. After you can run the script, the script automatically downloads the specified instance backup file and runs redis-port to restore the backup to the specified ApsaraDB for Redis instance.
  5. Run the following command to decompress the script package:
    tar -xvf redis-rdb-auto-restore.tar
  6. Run the following command to switch to the directory of the decompressed script package and modify the configuration file:
    cd redis-rdb-auto-restore
    vim config.json
    Note After you run the preceding command, the system opens an editor. Enter a to use the editing mode.
    Table 1. Parameters
    Parameter Description Example
    acesskeyID Specify the AccessKey ID and AccessKey secret of the RAM user. For more information, see Prerequisites. LTAI5tBHRFKq************
    acesskeySecret SoA0hZtZJ7Lszgy7aL************
    regionid The ID of the region where the source ApsaraDB for Redis instance is deployed. For more information about the mappings between region IDs and region names, see Regions and zones. cn-hangzhou
  7. To save the configuration file and exit the editor, press Esc, enter :wq, and press Enter.
  8. Run the following command to launch redis-port to migrate data:
    python rdb_restore.py -c r-bp1q4xidqiyqpq**** -d 2021-04-19 -t r-bp1q4xidqiyqpq****.redis.rds.aliyuncs.com -p 6379 -a redistest:Pawd123456 -b 9382****2,9382****3 -t r-bp1ky7j6qc7umk****.redis.rds.aliyuncs.com -p 6379 -a Pawd123456
    Table 2. Parameters in rdb_restore.py
    Parameter Required Description Example
    -c Yes The ID of the source ApsaraDB for Redis instance. SoA0hZtZJ7Lszgy7aL************
    -d Yes The timestamp of the backup that you want to download. The timestamp is in the yyyy-MM-dd format. 2021-04-19
    -t Yes The endpoint that is used to connect to the destination ApsaraDB for Redis instance. For more information, see View endpoints.
    • If the ECS instance is connected to the ApsaraDB for Redis instance through a VPC, you must obtain the internal endpoint of the ApsaraDB for Redis instance.
    • If you connect an on-premises machine to the ApsaraDB for Redis instance over the Internet, you must obtain the public endpoint of the ApsaraDB for Redis instance.
    r-bp1q4xidqiyqpq****.redis.rds.aliyuncs.com
    -p Yes The port of the destination ApsaraDB for Redis instance. The default port is 6379. 6379
    -a Yes The account that has the read and write permissions and the password of the destination ApsaraDB for Redis instance. Separate the account and password with a colon (:). For more information about how to create an account, see Create and manage database accounts. redistest:Pawd123456
    -o No The directory where the downloaded backup of the source instance is saved. The default directory is /root/. /root/
    -b No The ID of the backup of the source instance. If multiple backups are created, you can use this parameter to specify the backup that you want to download.
    Note
    938261111,938262222
    -h No Queries help information about a command. None
    After you run the program, the operational log is printed. If the data is restored, restore: rdb done appears.
    Figure 3. The output that is returned by redis-port
    The output that is returned by redis-port