Migrating and Backing up with Alibaba Cloud ApsaraDB for Redis

In this tutorial, we’ll show you how to get started with Alibaba Cloud ApsaraDB for Redis. In particular, we will be exploring how to migrate and back up a local database with Alibaba Cloud ApsaraDB for Redis.

What Is ApsaraDB for Redis?

Alibaba Cloud’s ApsaraDB for Redis is a key-value pair store that offers in-memory caching and high-speed access to applications hosted on the cloud.

The ApsaraDB stack is part of Alibaba Cloud’s wider suite of database services. Alibaba Cloud’s ApsaraDB includes support for RDS, Memcache, MongoDB and Redis. Alibaba Cloud’s ApsaraDB frees you from common system admin tasks associated with managing large web application in-memory caches and corporate databases.

ApsaraDB for Redis includes an automatic backup system as standard.

Already Have a Redis Setup?

You may have a Redis database setup already. If so, and you wish to migrate to Alibaba Cloud’s ApsaraDB for Redis, first check that your Redis installation is working.

Start Redis with redis-server

Run the command line interface with redis-cli and check the server is running with a ping.

set and get a test key-value pair and save it to the backup file.

You can also connect to a Redis instance using client software such as Jedis for Java, for example.

Backup File

ApsaraDB for Redis allows you to migrate with the EOF file. Make sure you set appendonly on your Redis database with the following command:

config set appendonly yes

Create an Alibaba Cloud ECS Instance

Now, let’s setup Redis on Alibaba Cloud’s ApsaraDB for Redis. First, we have to create an Alibaba Cloud ECS Instance on which to attach the Redis service. Let’s create an Ubuntu 16.04 OS in our favorite region.

Don’t forget to set a password so you can connect to the instance.

Agree to terms and Create Instance.

Setup Ubuntu OS and Redis on the ECS

Let’s install everything we need on the Ubuntu 16.04 ECS instance. First, we need to do the requisite Ubuntu updates:

apt-get update
apt-get upgrade
apt-get dist-upgrade

Then let’s install Redis on the ECS instance:

apt install redis-tools 

Create an Alibaba Cloud ApsaraDB for Redis Instance

Now you can create an ApsaraDB for Redis instance that is connected to the default VPC and ECS which we just created.

Pick the Redis version you require.

Notice that Alibaba Cloud auto-populates the VPC/vSwitch details with the defaults just created. When you’re ready, click Buy Now, then agree to terms and Activate.

Connect to ApsaraDB Redis

Now let’s connect to the ApsaraDB for Redis instance on the ECS instance we have just created. First, we have to ssh into the ECS instance.

Next, in the ApsaraDB for Redis details, we have to whitelist a network CIDR block in order to access the Redis instance.

The default whitelist blocks all access with 127.0.0.1.

For the tutorial’s purposes, let’s modify the whitelist setting to allow access from anywhere. Remember, in the real world you will want to whitelist just a section of a VPC CIDR block for Redis.

After the whitelist has been set, the Connection Address appears on the ApsaraDB for Redis Instance Information tab. Make a note of it.

Now, from the terminal connected to the ECS instance, you can access the ApsaraDB for Redis instance with the following command:

redis-cli -h [Connection Address] -a [password]

For example:

Migrate Redis to Alibaba Cloud

In a terminal window, cd to the local directory in which your appendonly.aof file resides and run the following scp command:

scp appendonly.aof root@47.90.244.142:/home

Now, find the copied appendonly.aof file in the ECS instance.

Pipe the appendonly.aof to the ApsaraDB for Redis instance with the following command in the ECS terminal.

redis-cli -h [Connection Address] -p 6379 -a [password] --pipe < appendonly.aof

Now, login to your ApsaraDB for Redis service and with the get command, check the data by getting something you set on the database locally. You will see that your data has been successfully migrated over to the Alibaba Cloud.

Jedis

You can also connect to your Alibaba Cloud ApsaraDB for Redis instance with Jedis for Java in Eclipse. Before you can access the Redis instance in the cloud you have to open some ports with network configuration changes on the ECS instance.

First, install rinetd on the Ubuntu instance (the details given here will be different for other operating systems).

wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd
sed -i 's/65536/65535/g' rinetd.c
mkdir /usr/man&&make&&make install

Next, add the following configuration details to the rinetd configuration file:

nano /etc/rinetd.conf
0.0.0.0 6379 r-7go1a73cf051c984.redis.rds.aliyuncs.com 6379
logfile /var/log/rinetd.log

Then, start the rinetd service:

rinetd

Check whether rinetd is running and working properly.

Also, you have to make sure to open the Redis port in the Security Group details of the ECS instance. Go to the ECS Management Console Security Groups and select your Security Group. Click Configure Rules, then Add Security Group Rules.

Open port range 6379/6379 to the IP range you wish to have access to your Redis instance. We have used 0.0.0.0/0 for the tutorial’s purposes.

Now you can code directly to the Redis instance from your local development environment.

For Jedis, create a new class and add the appropriate configuration details to match your cloud instance.

Backing Up

As with all the other Alibaba Cloud ApsaraDB services, in the ApsaraDB for Redis instance details, you can set backup configurations for your Redis instance.

Summary

To summarize, we first made sure our local Redis database was working and we set an example key-value pair. We made sure we saved our local Redis database with the appendonly option.

Next, we created an Alibaba Cloud ECS Instance on which to host our ApsaraDB for Redis instance. We configured Ubuntu 16.04 as our operating system and setup Redis on the instance.

Next, we created an Alibaba Cloud ApsaraDB for Redis instance which was attached to the Ubuntu ECS we had just created. We migrated our local database to the Alibaba Cloud instance and checked whether it had migrated successfully.

Finally, we performed some network administration in order to be able to access our Alibaba Cloud ApsaraDB for Redis instance from outside the cloud. We chose the Java Jedis client and wrote a test class to check that we could connect to the cloud database.

Don’t forget to check out our other Alibaba Cloud ApsaraDB tutorials for Memcache, RDS and MongoDB along with all the other products and services Alibaba Cloud offers.