環境說明:linux
172.20.0.1 redis源實例 172.20.0.2 redis目標實例 172.20.0.3 任意linux系統
1、redis-dump方式redis
1.安裝redis-dump工具json
[root@172.20.0.3 ~]# yum install ruby rubygems ruby-devel -y # 更改gem源 [root@172.20.0.3 ~]# gem sources -a http://ruby.taobao.org Error fetching http://ruby.taobao.org: bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)
訪問http://ruby.taobao.org,公告通知鏡像維護站點已遷往Ruby China鏡像ruby
#gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ [root@172.20.0.3 ~]# gem sources --add http://gems.ruby-china.org/ --remove http://rubygems.org/ http://gems.ruby-china.org/ added to sources source http://rubygems.org/ not present in cache [root@172.20.0.3 ~]# gem sources -l *** CURRENT SOURCES *** http://gems.ruby-china.org/ [root@172.20.0.3 ~]# gem install redis-dump -V
2.redis-dump導出bash
[root@172.20.0.3 ~]# redis-dump -u :password@172.20.0.1:6379 > 172.20.0.1.json
3.redis-load導入app
[root@172.20.0.3 ~]# cat 172.20.0.1.json | redis-load -u :password@172.20.0.2:6379
2、aof導入方式工具
1.源實例生成aof數據fetch
# 清空上文目標實例所有數據 [root@172.20.0.1 ~]# redis-cli -h 172.20.0.2 -a password flushall OK # 源實例開啓aof功能,將在dir目錄下生成appendonly.aof文件 [root@172.20.0.1 ~]# redis-cli -h 172.20.0.1 -a password config set appendonly yes OK
2.目標實例導入aof數據rest
# 假設appendonly.aof就在當前路徑下 [root@172.20.0.1 ~]# redis-cli -h 172.20.0.2 -a password --pipe < appendonly.aof All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 5 # 源實例關閉aof功能 [root@172.20.0.1 ~]# redis-cli -h 172.20.0.1 -a password config set appendonly no OK
3、rdb文件遷移方式server
暫略
4、源實例db0遷移至目標實例db1
[root@172.20.0.1 ~]# 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