在一臺服務上面運行下面的腳本
#!/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