Migrating and Backing up with ApsaraDB for Redis

Image for post
Image for post

Alibaba Cloud’s ApsaraDB for Redis is a key-value pair storage database 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. The ApsaraDB suite 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.

In this tutorial, youll get started using Alibaba Cloud ApsaraDB for Redis, and explore how you can migrate and back up a local database with Alibaba Cloud ApsaraDB for Redis.

Setting up the 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

Creating an Alibaba Cloud ECS Instance

Now, let’s setup Redis on Alibaba Cloud’s ApsaraDB for Redis. To do this, we will have to first create an Alibaba Cloud ECS Instance on which you will then attach the Redis service. Let’s create an ECS instance installed with Ubuntu 16.04. Choose the region based on your requirements.

Image for post
Image for post

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

Image for post
Image for post

Agree to the terms and then create the instance.

Image for post
Image for post

Setting up the Instance and Redis

Now, let’s install everything we need on the instance. First, we need to do the required Ubuntu updates:

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

Then, now that that’s finished, let’s install Redis on the ECS instance:

apt install redis-tools

Creating an 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.

Image for post
Image for post

Pick the Redis version you require.

Image for post
Image for post

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 the terms and click Activate.

Image for post
Image for post

Connecting to ApsaraDB Redis

Now let’s connect to the ApsaraDB for Redis instance on the ECS instance we have just created. We will use the SSH protocol for this.

Image for post
Image for post

Next, in the ApsaraDB for Redis details page, 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.

Image for post
Image for post

For this tutorial, we will modify the whitelist setting to allow access from anywhere. Remember that, in a real production scenario, you will want to whitelist just a section of a VPC CIDR block for Redis.

Image for post
Image for post
Image for post
Image for post

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

Image for post
Image for post

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:

Image for post
Image for post

Getting ApsaraDB for Redis Ready for Migration

Follow these steps to make sure that your installation is in proper working order. To do this, you’ll need to follow these steps:

First, start Redis with the redis-server command.

Image for post
Image for post

Then, run the command line interface with redis-cli and check the server is running with a ping. And, set and get a test key-value pair and save it to the backup file.

Image for post
Image for post

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

Image for post
Image for post

Migrating 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
Image for post
Image for post

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

Image for post
Image for post

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
Image for post
Image for post

Now, log in 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.

Image for post
Image for post

Connecting with 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. Note that 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.

Image for post
Image for post

Also, you have to make sure to open the Redis port in the Security Group details page of the ECS instance. Go to the ECS Management Console Security Groups page and select your Security Group. Click Configure Rules, and then click 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 this tutorial.

Image for post
Image for post

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.

Image for post
Image for post

Setting up Backup Configurations

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.

Image for post
Image for post

Summary

To summarize what we did in this tutorial, we first had set up our Redis database and made sure it was working, and we set an example key-value pair. Then, we made sure we saved our local Redis database with the appendonly option. Next, after that, we created an Alibaba Cloud ECS Instance on which to host our ApsaraDB for Redis instance. We configured the instance to have 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 ECS instance 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 console. We chose the Java Jedis client and wrote a test class to check that we could connect to the cloud database.

Enjoyed this tutorial? Learned something? Don’t forget to check out our other Alibaba Cloud ApsaraDB tutorials for setting up ApsaraDB for Memcache, ApsaraDB for RDS and ApsaraDB for MongoDB, along with several other tutorials for several other products and services offered by Alibaba Cloud.

Original Source:

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store