redis-dump 数据导出和redis-load 还原数据

运维技术   redis  

一 、安装Ruby依赖

  • Centos 安装Ruby环境
yum -y install ruby ruby-devel  
yum -y install rubygems  
  • 添加taobao Ruby镜像
gem sources --add http://gems.ruby-china.org --removehttps://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)  
  • 安装curl,一般都有默认安排可以跳过
yum install curl -y  
  • 安装RVM
curl -L get.rvm.io | bash -s stable  
source/usr/local/rvm/scripts/rvm  
  • 查看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]
  • 安装一个ruby版本,这里我们选择2.3.4
rvm install 2.3.4  
  • 使用一个ruby版本
rvm use 2.3.4  
rvm --create ruby-2.3.4  
ruby --version  
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]  
  • 卸载一个已知版本
rvm remove 2.0.0  
  • 安装redis-dump
gem install redis-dump  

三、redis 安装和启动

  • 安装
yum install redis -y  
  • 启动
service redis start  
  • 插入测试数据
[root@mytest-001 ~]# redis-cli -h 192.168.80.10 -p 6379
192.168.80.10:6379> auth ****密码****  
OK  
192.168.80.10:6379> set mytest aaa  
OK  
192.168.80.10:6379> set mytest2 bbb  
OK  
192.168.80.10:6379> set mytest3 ccc  
OK  

四、导出redis数据

  • 导出
redis-dump -u 192.168.80.10:6379 -a '***密码***' > test.json  
-bash: redis-dump: command not found
  • 解决办法(原因是没有指定ruby-2.3.4的环境,导致找不到redis-dump命令)
rvm --create ruby-2.3.4  
Warning! PATH is not properly set up, '/usr/local/rvm/gems/ruby-2.3.4/bin' is not available.  
         Usually this is caused by shell initialization files. Search for 'PATH=...' entries.
         You can also re-add RVM to your profile by running: 'rvm get stable --auto-dotfiles'.
         To fix it temporarily in this shell session run: 'rvm use ruby-2.3.4'.
         To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
Using /usr/local/rvm/gems/ruby-2.3.4

which redis-dump  
/usr/local/rvm/gems/ruby-2.3.4/bin/redis-dump
  • 再次尝试导出
redis-dump -u 192.168.80.10:6379 -a '***密码***' > test.json  
cat test.json  
{"db":0,"key":"mytest ","ttl":-1,"type":"string","value":"aaa","size":3}
{"db":0,"key":"mytest2","ttl":-1,"type":"string","value":"bbb","size":3}
{"db":0,"key":"mytest3","ttl":-1,"type":"string","value":"ccc","size":3}

五、redis-load 还原数据

  • 在redis-server 中删除数据
192.168.80.10:6379> del mytest  
(integer) 1
192.168.80.10:6379> del mytest2  
(integer) 1
192.168.80.10:6379> del mytest3  
(integer) 1
# 查看已经删除
192.168.80.10:6379> get mytest  
(nil)
  • redis-load还原
cat test.json |redis-load -u 192.168.80.10:6379 -a '***密码***'  
  • 查看数据
192.168.80.10:6379> get mytest  
"aaa"
192.168.80.10:6379> get mytest2  
"bbb"
192.168.80.10:6379> get mytest3  
"ccc"
192.168.80.10:6379>  

六、redis数据库备份脚本

#!/bin/bash
source /etc/profile.d/rvm.sh  
/usr/local/rvm/bin/rvm --create ruby-2.3.4 & >/dev/null
DATENOW=`date +%Y%m%d`  
DIR=/data/backup/redisdb/"$DATENOW"  
DATETIME=`date +%H%M`

if [ ! -d "$DIR" ]  
 then 
   mkdir -p  $DIR
fi

/usr/local/rvm/gems/ruby-2.3.4/bin/redis-dump -u 127.0.0.1:6379 >"$DIR"/redis-$DATETIME-json

oldDate=$(date -d "-6 days" "+%Y%m%d")  
cd /data/backup/redisdb  
for folder in $(ls -d */)  
do  
   [ ${folder%/*} -lt $oldDate ] && rm -rf $folder
done