利用redis-dump导出、还原数据

 create-2017-12-01

角色:
源服务器 189.202 端口 6520
目标服务器  193.16 端口  6888

业务需求:有一台缓存服务器189.202,因系统问题无法登录,我们需要导出上面的缓存数据,通过迁移工具(redis-migrate-tool、redis-port)和主从方式均失败,最终利用redis-dump实现。

现在我们通过193.16服务器 安装利器后,把202服务器的数据导进来16的6888端口

一 、安装Ruby依赖
1)安装Ruby环境
yum -y install ruby ruby-devel
yum -y install rubygems
 
2)添加taobao Ruby镜像
添加淘宝ruby源;
# gem sources --add http://gems.ruby-china.org --remove https://rubygems.org/
http://gems.ruby-china.org added to sources
source https://rubygems.org/ not present in cache

查看现有ruby源;
# gem sources --l
*** CURRENT SOURCES ***
http://rubygems.org/
http://gems.ruby-china.org

二、安装redis-dump
Centos默认支持ruby到2.0.0, 可gem 安装 redis 需要最低是2.2.2
解决办法是 先安装rvm,再把ruby版本提升至2.3.3
# gem install redis-dump
select id,user,host,db,command,time,state,info from information_schema.processlist where command <>'sleep'  order by time;
ERROR:  Error installing redis-dump:
        redis requires Ruby version >= 2.2.2.
        
# gpg2 --keyserver hkp:// keys.gnupg.net --recv-keys D39DC0E3
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) < [email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

1. 安装curl 
# sudo yum install curl

2.  安装RVM
# curl -L get.rvm.io | bash -s stable

3.  source /usr/local/rvm/scripts/rv m

4.  查看rvm库中已知的ruby版本
# rvm list known   
   # MRI Rubies
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]

5.  安装一个ruby版本
# rvm install 2.3.4   这里要选择正确   //rvm install 2.3会失败
 
6.  使用一个ruby版本
#rvm use 2.3 . 4

# rvm --create ruby-2.3.4
Using /usr/local/rvm/gems/ruby-2.3.4

# ruby --version
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]

7.  卸载一个已知版本 rvm remove 2.0 . 0

安装redis-dump
# gem install redis-dump

导出数据
# redis-dump -u 10.40.189.202:6520 > test.json
 
还原数据 ,如果redis的端口不是6520的话,还需要设定一个环境变量,用来导入数据。
# export REDIS_URI=127.0.0.1:6888
# < test.json redis-load
ERROR (Yajl::ParseError): lexical error: invalid bytes in UTF8 string.
          },"size":274} {"db":0,"key":"0000\u0005t\u0000#100776963
                     (right here) ------^
[root@cache_hx_193_16 bohai]# cp test.json test.json-bak

如果报错,请使用-n选项,使用请参考官方 ,请谨慎使用!
#< test.json redis-load -n    //-n (以二进制形式导入)

Loading data with binary strings
If you have binary or serialized data in your  Redis  database, the YAJL parser may not load your dump file because it sees some of the binary data as 'invalid bytes in UTF8 string'. If you are certain that your data is binary and not malformed UTF8, you can use the -n flag to redis-load to tell YAJL to not check the input for UTF8 validity. Use with caution!

看到数据已经导入:
# redis-cli -p 6888 dbsize  
(integer) 5001

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
传播知识,分享快乐;支持原创,爱心打赏!
利用redis-dump导出、还原数据_第1张图片     利用redis-dump导出、还原数据_第2张图片 利用redis-dump导出、还原数据_第3张图片
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
参考文献:
利用redis-dump导 出、还原数据--> http://www.ywnds.com/?p=7115
redis-dump安装使用手册---> https://github.com/delano/redis-dump
redis requires Ruby version >= 2.2.2问题 ---> https://www.cnblogs.com/carryping/p/7447823.html
 







你可能感兴趣的:(Redis)