没有不值得去解决的问题,也没有不值得去学习的技术!

Migrate Redis Data From a Self-Hosted Instance to Tencent Cloud With Python

After rolling back the version to 2.10.6, continue running python migrate.py. The data migration is successful. The reason why the total number of keys migrated in database 0 is 53 is that the data in the self-hosted Redis was changing.

1. Reference URL: https://blog.csdn.net/zhanghan18333611647/article/details/81434786 . This is implemented via a program (Python version 2.x). Create a new file named migrate.py.

Python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
# 把redis里面一个数据库的东西,迁移到另外一个数据库里面
# 建立两个redis连接
# 获取到所有的key .keys()
# 判断key的类型 string\hash\list\set

import redis
#redis_from源数据,redis_to目标数据  redis_from——>redis_to
redis_from = redis.StrictRedis(host='ip',port=6379,password='',db=10)
redis_to = redis.StrictRedis(host='ip',port=6379,password='',db=0)
 
if __name__ == '__main__':
    cnt = 0
    scnt = 0
    lcnt = 0
    setcnt = 0
    hcnt = 0
    
    for k in redis_from.keys():
        # 循环keys里面每一个key
 
        data_type = redis_from.type(k)
        # 判断key的类型 string\hash\list\set
 
        if data_type == 'string':
            v = redis_from.get(k)
            t = redis_from.ttl(k)
            redis_to.set(k, v)
            if int(t) > 0:
                redis_to.expire(k,t)
 
            scnt = scnt + 1
 
        elif data_type == 'list':
            values = redis_from.lrange(k, 0, -1)
            t = redis_from.ttl(k)           
            redis_to.lpush(k, values)
            if int(t) > 0:
                redis_to.expire(k,t)
 
            lcnt = lcnt + 1
        elif data_type == 'set':
            values = redis_from.smembers(k)
            t = redis_from.ttl(k)
            redis_to.sadd(k, values)
 
            if int(t) > 0:
                redis_to.expire(k,t)
            setcnt = setcnt + 1
        elif data_type == 'hash':
            hcnt = hcnt + 1
            keys = redis_from.hkeys(k)                        
            for key in keys:
                value = redis_from.hget(k, key)
                t = redis_from.ttl(k)
                redis_to.hset(k, key, value)
                if int(t) > 0:
                    redis_to.expire(k,t)
 
        else:
            print 'not known type'
            print data_type
            file_object = open('/data/thefile.txt','a+')
            file_object.write(data_type)
            file_object.write('\n')
            file_object.close( )
 
 
 
        cnt = cnt + 1
 
    print 'total', cnt
    print 'string', scnt
    print 'list', lcnt
    print 'set', setcnt
    print 'hash', hcnt

2. On the self-hosted Redis machine, connect to the self-hosted Redis and the Tencent Cloud built-in Redis respectively. Database 0 of the Tencent Cloud built-in Redis is empty. I plan to use this database as the migration target. See Figure 1.

On the self-hosted Redis machine, connect to the self-hosted Redis and the Tencent Cloud built-in Redis respectively. Database 0 of the Tencent Cloud built-in Redis is empty. I plan to use this database as the migration target.
Figure 1

3. Check the Python version, which is Python 2.7.5. The version meets the requirements. See Figure 2.

Check the Python version, which is Python 2.7.5. The version meets the requirements.
Figure 2
Plaintext

[root@iz2zeeh3rrqalw11oajd6zz ~]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


4. Connect to the self-hosted Redis and check the total number of keys in database 0: 40.

Plaintext

[root@iz2zeeh3rrqalw11oajd6zz ~]# redis-cli -h 127.0.0.1 -p 6379 -a 2021Chinamcloud
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> dbsize
(integer) 40
127.0.0.1:6379>


5. Run python migrate.py. An import error occurs: ImportError: No module named redis. See Figure 3.

Run python migrate.py. An import error occurs: ImportError: No module named redis.
Figure 3
Plaintext
[root@iz2zeeh3rrqalw11oajd6zz ~]# python migrate.py
Traceback (most recent call last):
  File "migrate.py", line 9, in 
    import redis
ImportError: No module named redis
[root@iz2zeeh3rrqalw11oajd6zz ~]#


6. After installing redis-py, continue running python migrate.py.

Plaintext

[root@iz2zeeh3rrqalw11oajd6zz ~]# python migrate.py
Traceback (most recent call last):
  File "migrate.py", line 39, in 
    redis_to.lpush(k, values)
  File "/usr/lib/python2.7/site-packages/redis/client.py", line 1961, in lpush
    return self.execute_command('LPUSH', name, *values)
  File "/usr/lib/python2.7/site-packages/redis/client.py", line 900, in execute_command
    conn.send_command(*args)
  File "/usr/lib/python2.7/site-packages/redis/connection.py", line 725, in send_command
    self.send_packed_command(self.pack_command(*args),
  File "/usr/lib/python2.7/site-packages/redis/connection.py", line 775, in pack_command
    for arg in imap(self.encoder.encode, args):
  File "/usr/lib/python2.7/site-packages/redis/connection.py", line 120, in encode
    "bytes, string, int or float first." % typename)
redis.exceptions.DataError: Invalid input of type: 'list'. Convert to a bytes, string, int or float first.
[root@iz2zeeh3rrqalw11oajd6zz ~]#


7. Check the Redis versions respectively. The self-hosted Redis version is 3.2.12. The Tencent Cloud built-in Redis version is 2.8.23. The versions are inconsistent. See Figure 4 and Figure 5.

The self-hosted Redis version is 3.2.12.
Figure 4

 

The Tencent Cloud built-in Redis version is 2.8.23.
Figure 5
Plaintext

[root@iz2zeeh3rrqalw11oajd6zz ~]# redis-cli -h 127.0.0.1 -p 6379 -a 2021Chinamcloud
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-1160.11.1.el7.x86_64 x86_64


Plaintext
RDM Redis Console
Connecting...
Connected.
Tencent Cloud (Company Development Environment):0>info
"# Server
redis_version:2.8.23
run_id:0a99b4d4dac9744a76075aa77a978a2dfcdf1ba6
uptime_in_seconds:74995115
uptime_in_days:867
hz:10
lru_clock:8808554

8. Check the installed redis version in Python. Create a new file named python_redis.py, and observe that the version is 3.5.3. See Figure 6.

Check the installed redis version in Python. Create a new file named python_redis.py, and observe that the version is 3.5.3.
Figure 6
Python
import redis

print redis.VERSION

9. Execute the command: pip install redis==2.10.6. Roll back the version to 2.10.6. See Figure 7.

Execute the command: pip install redis==2.10.6. Roll back the version to 2.10.6.
Figure 7

10. After rolling back the version to 2.10.6, continue running python migrate.py. The data migration is successful. The reason why the total number of keys migrated in database 0 is 53 is that the data in the self-hosted Redis was changing. See Figure 8.

After rolling back the version to 2.10.6, continue running python migrate.py. The data migration is successful. The reason why the total number of keys migrated in database 0 is 53 is that the data in the self-hosted Redis was changing.
Figure 8
Plaintext
[root@iz2zeeh3rrqalw11oajd6zz ~]# python python_redis.py
(2, 10, 6)
[root@iz2zeeh3rrqalw11oajd6zz ~]# python migrate.py
total 53
string 25
list 3
set 0
hash 25


11. Check the data in database 0 of the Tencent Cloud built-in Redis. The total number of keys is 53, confirming the migration was successful. When the Tencent Cloud built-in Redis is version 4.0, the migration can also succeed. See Figure 9.

Check the data in database 0 of the Tencent Cloud built-in Redis. The total number of keys is 53, confirming the migration was successful. When the Tencent Cloud built-in Redis is version 4.0, the migration can also succeed.
Figure 9

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系