redis-dump之can‘t find gem redis-dump

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-dump之can‘t find gem redis-dump相关的知识,希望对你有一定的参考价值。

基于上篇博文CentOS6.10安装redis-dump小插曲继续下面的介绍

一、redis-dump工具简单说明

退出bash shell窗口重新登录bash

[[email protected] ~]# find / -name redis-dump 
/usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump
/usr/local/rvm/gems/ruby-2.3.7/gems/redis-dump-0.4.0/bin/redis-dump

未加载rvm,直接执行redis-dump时报错

[[email protected] ~]# /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump  -u 192.168.2.106:10306 -a ‘DH56ji2(3u4‘ > test.json
/usr/local/rvm/rubies/ruby-2.3.7/lib/ruby/2.3.0/rubygems.rb:241:in `bin_path‘: can‘t find gem redis-dump (>= 0.a) (Gem::GemNotFoundException)
    from /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump:22:in `<main>‘

于是重新加载rvm后再次执行redis-dump成功

[[email protected] ~]# source /usr/local/rvm/scripts/rvm
[[email protected] ~]# ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
[[email protected] ~]# rvm --create ruby-2.3.7
Using /usr/local/rvm/gems/ruby-2.3.7
[[email protected] ~]# ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-linux]
[[email protected] ~]# 
[[email protected] ~]# /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump  -u 192.168.2.106:10306 -a ‘DH56ji2(3u^4‘ > test.json
[[email protected] ~]# 
[[email protected] ~]# du -sh test.json 
87M test.json
[[email protected] ~]# 

于是简单总结如下:
redis-dump工具是基于ruby编写的脚本,所以必须在ruby环境下才能执行此命令工具
所以在执行redis-dump命令前必须先加载ruby环境,source /etc/profile.d/rvm.sh就是加载ruby环境或者是source /usr/local/rvm/scripts/rvm
由于一开始服务器安装的ruby是2.3.7的版本rvm install 2.3.7,所以要切换到ruby-2.3.7 下
rvm --create ruby-2.3.7

二、redis数据备份脚本分析

2.1、原备份脚本:

[[email protected] scripts]# vim dump_redisdata.sh
#!/bin/bash
source /etc/profile.d/rvm.sh
rvm --create ruby-2.3.7 & >/dev/null
DATENOW=`date +%Y%m%d`
DIR=/data1/backup/redisdb/"$DATENOW"
DATETIME=`date +%H%M`

if [ ! -d "$DIR" ]
 then
   mkdir -p  $DIR
fi
/usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump -u 192.168.2.106:10239 -a ‘DJKjK0e=4‘ |gzip >"$DIR"/redis$DATETIME-json.gz

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

上面的脚本直接在shell执行时有问题的,导致写入定时任务后,备份redis数据失败

2.2、失败的分析过程

先加载ruby环境以及切换到ruby-2.3.7下

source /etc/profile.d/rvm.sh
rvm  --create ruby-2.3.7
ruby  --version

[[email protected] ~]# sh dump_redisdata.sh


RVM is not a function, selecting rubies with ‘rvm use ...‘ will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

虽然shell界面还有warn输出,但是查看redis-dump 进程,脚本已经成功运行,

[[email protected] 20180725]# ps -ef|grep redis-dump
root     26675 26667 72 17:48 pts/3    00:00:14 /usr/local/rvm/rubies/ruby-2.3.7/bin/ruby /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump -u 192.168.2.106:10306 -a DH56ji2(3u^4
root     26836 22679  0 17:49 pts/4    00:00:00 grep redis-dump
[[email protected] 20180725]# 

但是不在服务器的shell 命令行先执行命令加载ruby环境以及切换到ruby-2.3.7环境下 就直接在服务器bash shell命令行手动执行脚本,就会报如下错,脚本执行失败,自动退出

[[email protected] ~]# sh  dump_redisdata.sh
/usr/local/rvm/rubies/ruby-2.3.7/lib/ruby/2.3.0/rubygems.rb:241:in `bin_path‘: can‘t find gem redis-dump (>= 0.a) (Gem::GemNotFoundException)
    from /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump:22:in `<main>‘
[[email protected] ~]# 
RVM is not a function, selecting rubies with ‘rvm use ...‘ will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

已经足够说明未加载rvm和切换到ruby2.3.7环境下
直接把原来的备份脚本写入定时任务,这也是备份脚本没执行成功的根本原因。

2.3、关于/bin/bash --login的分析

[[email protected] ~]# sh dump_redisdata.sh

RVM is not a function, selecting rubies with ‘rvm use ...‘ will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

然出现/bin/bash --login 输出,并不影响脚本的执行,但是看着怪不舒服的。于是想办法解决。
于是网上查资料,才发现是服务器的环境变量加载的问题。
现在简单普及下环境变量的

关于环境变量的介绍:

/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 
并从/etc/profile.d目录的配置文件中搜集shell的设置.
/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时该文件被读取.
~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件.

为了能做到在执行脚本前能够提前自动加载ruby的环境,所以选择了~/.bashrc 加载这个环境变量的文件
同时要在这个文件后面添加下面2行代码来加载ruby换进变量

[[email protected] ~]# cat /root/.bashrc

#.bashrc
#User specific aliases and functions
alias rm=‘rm -i‘
alias cp=‘cp -i‘
alias mv=‘mv -i‘

#Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
#####
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
rvm --create ruby-2.3.7

[[email protected] ~]#

2.4、修改后的成功的脚本

[[email protected] scripts]# cat dump_redisdata.sh

#!/bin/bash
. /root/.bashrc
DATENOW=`date +%Y%m%d`
DIR=/data1/backup/redisdb/"$DATENOW"
DATETIME=`date +%H%M`

if [ ! -d "$DIR" ]
 then 
   mkdir -p  $DIR
fi
redis-dump -u 192.168.2.106:10306 -a ‘DH56ji2(3u^4‘ |gzip >"$DIR"/redis$DATETIME-json.gz
oldDate=$(date -d "-6 days" "+%Y%m%d")
cd /data1/backup/redisdb
for folder in $(ls -d */)
do
   [ ${folder%/*} -lt $oldDate ] && rm -rf $folder
done

写入定时任务:

[[email protected] ~]# crontab -l|grep dump_redisdata.sh
12 17 * * * /bin/bash /data/scripts/dump_redisdata.sh & >/dev/null

查看进程,可知,此时脚本已经可以写入定时任务自动执行了

[[email protected] ~]# ps -ef|grep redis-dump
root     22250 22045 72 17:12 ?        00:00:22 /usr/local/rvm/rubies/ruby-2.3.7/bin/ruby /usr/local/rvm/gems/ruby-2.3.7/bin/redis-dump -u 192.168.2.106:2503 -a DP5HJKGF0
root     22529 21776  0 17:12 pts/3    00:00:00 grep redis-dump

到此redis-dump备份redis数据脚本的修改完毕。

以上是关于redis-dump之can‘t find gem redis-dump的主要内容,如果未能解决你的问题,请参考以下文章

can‘t find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

can‘t find gem cocoapods (>= 0.a) with executable pod

openSUSE 安装compass,mkmf.rb can't find,checking for ffi.h...extconf.rb failed

关于ruby安装在windows gem install redis-dump 报错:MSYS2 could not be found

redis 导入导出redis-load,redis-dump详解

redis-dump数据导出以及redis-load还原数据