Redis迁移键
迁移键:
move key db 用于在Redis内部进行数据迁移
dump key + restore key ttl value 可以实现在不同的Redis实例之间进行数据迁移
127.0.0.1:6379> dump name
"\x00\x05allen\a\x00\x82\x9a*T/\xb9\x9b\x87"
127.0.0.1:6379> dump age
"\x00\xc0!\a\x006.9\xfaQ\x06\xe8\r"
127.0.0.1:6379> dump pass
"\x00\x06abc123\a\x00\x90\t\xe8\x1b\xf2\xd4\x8b\xec"
192.168.1.122:6379> restore name 0 "\x00\x05allen\a\x00\x82\x9a*T/\xb9\x9b\x87"
OK
192.168.1.122:6379> restore age 0 "\x00\xc0!\a\x006.9\xfaQ\x06\xe8\r"
OK
192.168.1.122:6379> restore pass 0 "\x00\x06abc123\a\x00\x90\t\xe8\x1b\xf2\xd4\x8b\xec"
OK
migrate host port key|"" destination-db timeout [copy] [replace] [keys key [key ...]]
127.0.0.1:6379> migrate 192.168.1.122 6379 name 0 5000 默认是删除源库的键值
OK
127.0.0.1:6379> migrate 192.168.1.122 6379 "" 0 5000 copy keys age pass 加copy和replace源库不删除键值
OK
# cat redis_mv.sh
#!/bin/bash
redis-cli -h 172.20.0.1 -p 6379 -a password -n 0 keys "*" | while read key
do
redis-cli -h 172.20.0.1 -p 6379 -a password -n 0 --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h 172.20.0.2 -p 6379 -a password -n 1 -x restore $key 0
echo "migrate key $key"
done
#!/bin/bash
#redis 源ip
src_ip=192.168.0.1
#redis 源port
src_port=6379
#redis 源库
src_db=11
#redis 目的ip
dest_ip=192.168.0.1
#redis 目的port
dest_port=6379
#redis 目的
dest_db=6
#redis 密码
pw=123456
#要迁移的key前缀
#key_prefix=com.example.test
redis-cli -h $src_ip -p $src_port -a $pw -n $src_db keys "*" | while read key
do
redis-cli -h $src_ip -p $src_port -a $pw -n $src_db --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $pw -n $dest_db -x restore $key 0
echo "migrate key $key"
done
#!/bin/bash
#redis 源ip
src_ip=192.168.0.1
#redis 源port
src_port=6379
#redis 源库
src_db=1
#redis 目的ip
dest_ip=192.168.0.1
#redis 目的port
dest_port=6379
#redis 目的
dest_db=3
#redis 密码
pw=123456
#要遍历的key
k=(medical_record:id medical_record_attachment:id patient_family_present:id patient_present:id patient_disease:id family_disease:id patient_allergy:id history_allergy:id)
#要迁移的key前缀
#key_prefix=com.example.test
for loop in ${k[*]}
do
redis-cli -h $src_ip -p $src_port -a $pw -n $src_db --raw dump $loop | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $pw -n $dest_db -x restore $loop 0
echo "The value is: $loop"
done