Redis备份恢复

https://blog.csdn.net/bohai0409/article/details/78734526

一 、安装Ruby依赖
1)安装Ruby环境
yum -y install ruby rubygems ruby-devel

添加淘宝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) <mpapis@gmail.com>" 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/rvm

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版本
#rvmuse2.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. 卸载一个已知版本rvmremove2.0.0

安装redis-dump
# gem install redis-dump

 

备份
bash> redis-dump -u 10.2.16.35:64454 > test.json

还原
还原数据 ,如果redis的端口不是64454的话,如端口为6888,还需要设定一个环境变量,用来导入数据。
# export REDIS_URI=127.0.0.1:6888
bash> < test.json redis-load -u 10.5.0.222:64454
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选项,使用请参考官方,请谨慎使用!
#< all.json redis-load -n -u 10.5.0.222:64454 //-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

posted on 2018-05-02 14:24  法海降妖  阅读(113)  评论(0)    收藏  举报