amazon-web-services - ElastiCache - 将数据从 t1.*/t2.* 迁移到 m3.*

标签 amazon-web-services redis amazon-elasticache

我目前有一个实例类型为 的 Redis 集群cache.t2.micro

没有选项将 Redis 数据自动备份到 s3 对于这种实例类型,也不能运行 BGSAVE 命令作为其受限的描述 here

我注意到如果Redis集群中的主节点重新启动,或者Redis引擎版本从低版本更改为高版本(即从3.x到4.x),则节点中的数据会被完全删除,尽管他们声称尽力而为。

同样不支持为此实例类型制作快照,如 here 所述

我能想到的唯一选择是使用 转储 命令并获取 key 的序列化版本并为所有 DBS 存档此数据,然后使用 将其恢复回新集群。恢复 命令。
但这可能不是最好的方法,因为它不可扩展,并且最终需要一些时间来处理更大的数据集。

也适用于带有 的键TTL 启用,我将不得不运行 TTL 命令,获取 TTL(这是额外的开销)。

但是 DUMP 命令发出错误 DUMP 有效负载版本或校验和错误 排除该选项(难怪此命令不受限制)

在这种情况下,除了读取所有键及其值之外,还有其他方法可以进行备份和恢复吗?

谢谢。

编辑:

所以我知道这不是最好的方法,但这是我能想到的。

TL;博士

读取所有 key 并从一个集群迁移到另一个集群。

对于具有配置的集群,这应该不是问题。大于 t2.*

代码:

import traceback
from redis import Redis
import itertools

def migrate_to_another_redis_node(source_node, source_node_port_no, dest_node, dest_node_port_no):
'''
migrates the keys from one redis node to the other
:param source_node: source redis node url
:param source_node_port_no: source redis node port number
:param dest_node: destination redis node url
:param dest_node_port_no: destination redis node port number
:return: True/False
'''
    try:
        total_keys_migrated = 0
        for db in range(16):
             source_redis_client = Redis(source_node, source_node_port_no, db=db)
             dest_redis_client = Redis(dest_node, dest_node_port_no, db=db)
             for key in source_redis_client.keys('*'):
                 key_type = source_redis_client.type(key).decode()
                 if key_type == 'string':
                     value = source_redis_client.get(key)
                     dest_redis_client.set(key, value)
                 elif key_type == 'list':
                     values = source_redis_client.lrange(key, 0, -1)
                     dest_redis_client.rpush(key, *values)
                 elif key_type == 'hash':
                     key_value_pairs = source_redis_client.hgetall(key)
                     dest_redis_client.hmset(key, key_value_pairs)
                 elif key_type == 'set':
                     values = source_redis_client.smembers(key)
                     dest_redis_client.sadd(key, *values)
                 elif key_type == 'zset':
                     values = list(itertools.chain(*source_redis_client.zrange(key, 0, -1, withscores=True)))
                     dest_redis_client.zadd(key, *values)
                 ttl = source_redis_client.ttl(key)
                 if ttl:
                     dest_redis_client.expire(key, ttl)
                 total_keys_migrated += 1
        print('total keys migrated is {}'.format(total_keys_migrated))
        return True
   except:
        error = traceback.format_exc()
        print(error)
   return False

无论 key 类型如何,以上都有效。
性能:以上在 2 秒内迁移了大约 4000 个 key 。

最佳答案

根据 AWS 文档:

For Redis (cluster mode disabled) clusters, backup and restore aren't supported on cache.t1.micro nodes. All other cache node types are supported.


因此,对于 cahce.t2 节点,您可以创建手动/最终快照并在从快照恢复时更改节点类型

关于amazon-web-services - ElastiCache - 将数据从 t1.*/t2.* 迁移到 m3.*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52220459/

相关文章:

amazon-web-services - EC2 key 对更改

node.js - AWS-SES 电子邮件地址未验证。 Gmail 帐户

python - 如何在Python中导入S3cmd

Redis 哨兵故障转移配置更改

redis - redis 会自动绑定(bind)到 CPU 内核吗?

php - 固定 : PHP Session Store Error Using Memcached

Python3 boto3 对 wav 的响应

node.js - 为什么我在使用嵌套的 redis 语句时出错

amazon-elasticache - io.lettuce.core.RedisCommandTimeoutException : Command timed out

redis - Elasticache Redis 配置