redis 部分数据迁移

在一台服务上面运行下面的脚本

#!/bin/bash
# source http://stackoverflow.com/questions/23222616/copy-all-keys-from-one-db-to-another-in-redis

# 数据所在 redis 中的配置

source_host=XXXXXXXXXXXXXX
source_port=6379
source_password=XXXXXXX
source_db=0

# 要备份的 redis 配置
target_host=XXXXXXXXXXXXXX
target_port=6379
target_password=XXXXXXX
target_db=0

# 将 redis key 以 flow-recharge 开头的 数据 进行迁移
keys_pattern=flow-recharge\*

# copy matching keys
redis-cli -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} KEYS "${keys_pattern}" | while read key; do
    echo "Copying ${key}..."
    redis-cli --raw -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} DUMP "${key}" | head -c -1 | redis-cli -x -h ${target_host} -p ${target_port} -a ${target_password} -n ${target_db} RESTORE "${key}" 0

# 加过期时间
    #echo "TTL: $(redis-cli --raw -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} TTL "${key}")"
    #redis-cli --raw -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} TTL "${key}" | head -c -1 | redis-cli -x -h ${target_host} -p ${target_port} -a ${target_password} -n ${target_db} EXPIRE "${key}"
done
 

发布了20 篇原创文章 · 获赞 0 · 访问量 9177

猜你喜欢

转载自blog.csdn.net/qq_30346433/article/details/103164596
今日推荐