redis的備份和還原,借助了第三方的工具,redis-dump
1、安裝redis-dump
復制代碼 代碼如下:
[root@localhost tank]# yum install ruby rubygems ruby-devel //安裝rubygems 以及相關包
[root@localhost tank]# gem sources -a http://ruby.taobao.org/ //源,加入淘寶,外面的源不能訪問
http://ruby.taobao.org/ added to sources
[root@localhost tank]# gem install redis-dump -V //安裝redis-dump
2、redis-dump導出數據
復制代碼 代碼如下:
[root@localhost tank]# telnet 127.0.0.1 6379 //telnet到redis
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set test 11 //設置一個值
+OK
get test //取值
$2
11
[root@localhost tank]# redis-dump -u 127.0.0.1:6379 >test.json //導出數據
3、redis-load還原數據
復制代碼 代碼如下:
[root@localhost tank]# telnet 127.0.0.1 6379 //telnet到redis
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
flushall //請空所有數據
+OK
keys * //查看已清空
*0
[root@localhost tank]# < test.json redis-load //導入數據
[root@localhost tank]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
keys * //已導入成功
*1
$4
test