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

运维技术   redis  

一 、安装Ruby依赖

  • Centos 安装Ruby环境
yum -y install ruby ruby-devel  
yum -y install rubygems  
  • 添加taobao Ruby镜像
gem sources --add --remove added to sources  
source not present in cache  
  • 查看Ruby镜像
gem sources --l  


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:// --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  
gpg: /root/.gnupg/trustdb.gpg: trustdb created  
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <>" 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 | bash -s stable  
  • 查看rvm库中已知的ruby版本
rvm list known  
   #MRI Rubies
  • 安装一个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 -p 6379> auth ****密码****  
OK> set mytest aaa  
OK> set mytest2 bbb  
OK> set mytest3 ccc  


  • 导出
redis-dump -u -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  
  • 再次尝试导出
redis-dump -u -a '***密码***' > test.json  
cat test.json  
{"db":0,"key":"mytest ","ttl":-1,"type":"string","value":"aaa","size":3}

五、redis-load 还原数据

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


source /etc/profile.d/  
/usr/local/rvm/bin/rvm --create ruby-2.3.4 & >/dev/null
DATENOW=`date +%Y%m%d`  
DATETIME=`date +%H%M`

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

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

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