redis中使用redis-dump導(dǎo)出、導(dǎo)入、還原數(shù)據(jù)實(shí)例

字號:


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