简体   繁体   English

如何备份 AWS Elasticache Redis 数据?

[英]How To Backup AWS Elasticache Redis Data?

I'm testing a new Redis cluster (AWS Large primary/slave nodes) in AWS Elasticache Redis.我正在 AWS Elasticache Redis 中测试新的 Redis 集群(AWS 大型主/从节点)。

How can I backup or export my data from the Redis cluster?如何从 Redis 集群备份或导出我的数据?

Other hosted redis services automatically create a Redis RDB dump file and store it on S3.其他托管的 Redis 服务会自动创建一个 Redis RDB 转储文件并将其存储在 S3 上。 I would like to do something similar with ElastiCache.我想用 ElastiCache 做类似的事情。

EDIT: New AWS Feature as of 4/24/2014编辑:截至 2014 年 4 月 24 日的新 AWS 功能

Amazon has added internal backup support as of 4/24. 亚马逊自 4 月 24 日起增加了内部备份支持。 This allows you to snapshot redis data daily and restore it to an ElastiCache cluster.这允许您每天对 redis 数据进行快照并将其恢复到 ElastiCache 集群。 It does not allow exporting/downloading at the present.目前不允许导出/下载。 The solution below is still required if you want to keep your own archives/backups of redis data.如果您想保留自己的 redis 数据存档/备份,仍然需要以下解决方案。

Most people should be able to use the built in backup system now available.大多数人应该能够使用现在可用的内置备份系统。

Original Answer原答案

It looks like the only way to do this is to do the following.看起来这样做的唯一方法是执行以下操作。

  • Spin up new EC2 instance启动新的 EC2 实例
  • Install Redis安装Redis
  • Configure local replica attached to ElastiCache redis primary配置附加到 ElastiCache redis 主节点的本地副本
  • Wait for data to sync locally等待数据本地同步
  • Issue a redis SAVE command to generate a local dump发出 redis SAVE命令以生成本地转储
  • Archive local dump.rdb on S3在 S3 上存档本地dump.rdb

I'd love a simpler solution or something built into ElastiCache.我喜欢更简单的解决方案或 ElastiCache 内置的东西。

EDIT: Elaboration编辑:详细说明

I ended up actually building this using the Ruby gem/utility "redis-backup" ( https://github.com/josegonzalez/ruby-redis-backup ) with a crontab running the following shell command:我最终使用 Ruby gem/实用程序“redis-backup”( https://github.com/josegonzalez/ruby-redis-backup )和运行以下 shell 命令的 crontab 实际构建了它:

sudo -u redis /usr/bin/env S3_SAVE=true redis-backup -s /var/lib/redis/dump.rdb -B YOUR_S3_BUCKET_FOR_BACKUPS -A S3_ACCESS_KEY_ID -S S3_ACCESS_SECRET_KEY

It doesn't look like this is possible anymore.看起来这已经不可能了。 According to the AWS docs,根据 AWS 文档,

"Beginning with Redis 2.8.22, ElastiCache no longer supports external read replicas." “从 Redis 2.8.22 开始,ElastiCache 不再支持外部只读副本。”

ElastiCache may disable commands like save or bgsave . ElastiCache 可能会禁用savebgsave等命令。 For reference, check Restricted Redis Commands .作为参考,请查看Restricted Redis Commands

The following bash script solution supports datatypes string and hash.以下 bash 脚本解决方案支持数据类型字符串和哈希。 If you want support for other data types (eg, set, list, sorted set, bitmap), you have to extend the script (as commented in the script).如果您想要支持其他数据类型(例如,集合、列表、排序集合、位图),您必须扩展脚本(如脚本中的注释)。

#!/bin/bash

# change KEY_PATTERN accordingly if you want a subset of the keys in redis cache
KEY_PATTERN="*"

# provide the default redis url here if you don't supply that from command line/environment:
if [[ -z $REDIS_SERVICE_URL ]]; then
    REDIS_SERVICE_URL=redis://localhost:6379
fi

IFS=' '

iKey=0 # counter to iterate over the keys
echo {

# iterate through the keys in redis:
while read key; do
    if [ $iKey -gt 0 ]; then
        echo ","
    fi

    echo \"$key\":

    # get the datatype for the current key
    datatype=$(redis-cli -u "$REDIS_SERVICE_URL" type "$key")

    # this script supports string/hash datatype.
    # Extend if you want to support other data types.
    if [ "$datatype" = 'string' ]; then
        val=$(redis-cli -u "$REDIS_SERVICE_URL" get "$key")
        echo -n \"${val//\"/\\\"}\"
    elif [ "$datatype" = 'hash' ]; then
        echo [
        i=0
        while read val; do
            if [ $i -gt 0 ]; then
                echo ","
            fi

            echo \"${val//\"/\\\"}\"

            i=$((i+1))
        done <<< $(redis-cli -u "$REDIS_SERVICE_URL" hgetall "$key")
        echo ]
    else
        echo Unsupported type $datatype
        exit -1
    fi

    iKey=$((iKey+1))
done <<< $(redis-cli -u "$REDIS_SERVICE_URL" keys "$KEY_PATTERN")

echo }

unset IFS

Formatted JSON of a sample output using above script:使用上述脚本的示例输出的格式化 JSON:

{
    "vlaue-key-1": "value-1",
    "hash-key-1": [
        "key-a",
        "value of key-a in hash-key-1",
        "key-b",
        "20"
    ],
    "vlaue-key-2": "25.5",
    "hash-key-2": [
        "key-x",
        "value-x of hash-key-2/key-x",
        "key-b",
        "9999"
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM