others-How to export and import redis key with shell script between two standalone redis instances?

1. Purpose

In this post, I would demonstrate how to export specified redis keys to file, and then import the redis keys to another redis from the file, just as the following diagram shows:

image-20211202205436500

The process is as follows:

1) Export specified key from redis 1 to a file

2) Import the keys to redis 2 from the file

2. The solution

I would demo how to do this job with the redis hash key, you can do the same job with other types of keys.

2.1 Export hash key to a file

Let’s say there is a redis key named my_key, which contains a hash map, e.g.

127.0.0.1:6379> hset my_key k1 v1
(integer) 1
127.0.0.1:6379> hget my_key k1
"v1"
127.0.0.1:6379>

The export command is:

echo "HGETALL my_key" | redis-cli -h 127.0.0.1 -p 6379 | xargs -n 2 | awk -F" " -v KEYNAME=my_key '{print "HSET "KEYNAME " " $1, "\""$2"\""}' >> my_key.raw

The command is composed by some parts, first it get all the sub keys of my_key:

# echo "HGETALL my_key" | redis-cli -h 127.0.0.1 -p 6379
1) "k1"
2) "v1"

Then it capture the second column of the result:

# echo "HGETALL my_key" | redis-cli -h 127.0.0.1 -p 6379 | xargs -n 2
k1 v1
#

Then it use the awk command to construct a set of redis commands to be used when import keys:

# echo "HGETALL my_key" | redis-cli -h 127.0.0.1 -p 6379 | xargs -n 2 | awk -F" " -v KEYNAME=my_key '{print "HSET "KEYNAME " " $1, "\""$2"\""}'
HSET my_key k1 "v1"
#

2.2 Import hash key from the file

Now we connect to another redis instance which listens to port 26379, and we’ll try to import the keys from the file we just exported.

cat my_key.raw | redis-cli -h 127.0.0.1 -p 26379

We got this:

(integer) 1
[root@genuine-post-2 redis-2.8.19]# ./src/redis-cli -p 26379
127.0.0.1:26379> hgetall my_key
1) "k1"
2) "v1"
127.0.0.1:26379>

We have done this job!!!

3. Summary

In this post, I demonstrated how to export and import keys from two seperate redis instances, the key point is to use linux shell piping operations. That’s it, thanks for your reading.