使用redis-dump导入导出Redis数据

一、环境说明

1.我们是在docker虚拟机中搭建的redis,所以此处设计的导入导出分为两种情况:

  • 导入导出Redis中单个solt中数据;(目前我操作的是这种,可以指定要导出哪一个solt)
  • 导入导出所有solt中所有数据(是上面的特殊情况)

2.redis库的备份与还原(这个暂时不讨论)

  • 涉及Redis持久化和相应命令(dump.rdb)

二、Redis导入导出方式与问题

(使用redis-dump命令)

我是用的是redis-dump命令备份还原redis数据文件


但是在执行

gem install redis-dump -V

命令的时候,出现如下错误提示:

[root@whzbslave13 _data]# gem install redis-dump -V
HEAD http://gems.ruby-china.com/latest_specs.4.8.gz
304 Not Modified
HEAD http://gems.ruby-china.com/specs.4.8.gz
304 Not Modified
Installing gem redis-4.0.1
ERROR:  Error installing redis-dump:
	redis requires Ruby version >= 2.2.2.

此时,你可能想着事升级rubby的系统,到>=2.2.2以上版本,此时搜索之后参考这个https://blog.51cto.com/moerjinrong/2348391:

安装步骤安装到bash rvm-installer stable该步骤时候,可能报错:

[root@whzbslave13 rvm]# bash rvm-installer stable
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
gpg: Signature made Wed 10 Jul 2019 04:31:02 PM CST using RSA key ID 39499BDB
gpg: Can't check signature: No public key
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.9.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

In case of further problems with validation please refer to https://rvm.io/rvm/security

根据提示,gpg安装失败,其建议安装gpg2,安装命令如下:

[root@whzbslave13 rvm]# curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
gpg: key D39DC0E3: "Michal Papis (RVM signing) <mpapis@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
[root@whzbslave13 rvm]# curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
gpg: key 39499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

然后再次执行bash rvm-installer stable,如下,表示该步骤成功!

[root@whzbslave13 rvm]# bash rvm-installer stable
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc



gpg: Signature made Wed 10 Jul 2019 04:31:02 PM CST using RSA key ID 39499BDB
gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.9.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM ?
Please consider donating to our open collective to help us maintain RVM.

?  Donate: https://opencollective.com/rvm/donate

继续后续步骤的安装即可;

rvm list known检验暗转成功与否!

待到网址中的所有步骤安装完成,可以检查redis-dump是否生效:

[root@whzbslave13 rvm]#  redis-dump --help
  Try: /usr/local/rvm/gems/ruby-2.4.1/bin/redis-dump show-commands
Usage: /usr/local/rvm/gems/ruby-2.4.1/bin/redis-dump [global options] COMMAND [command options] 
    -u, --uri=S                      Redis URI (e.g. redis://hostname[:port])
    -d, --database=S                 Redis database (e.g. -d 15)
    -a, --password=S                 Redis password (e.g. -a 'my@pass/word')
    -s, --sleep=S                    Sleep for S seconds after dumping (for debugging)
    -c, --count=S                    Chunk size (default: 10000)
    -f, --filter=S                   Filter selected keys (passed directly to redis' KEYS command)
    -b, --base64                     Encode key values as base64 (useful for binary values)
    -O, --without_optimizations      Disable run time optimizations
    -V, --version                    Display version
    -D, --debug
        --nosafe

三、Redis的备份与还原操作

#执行命令类似如下
#备份:其中-d 指定要导出的slot
redis-dump -u 172.19.32.xx:6379 -d 0 -b > ml_result_xx_6379_.txt
#还原
cat ml_result_xx_6379.txt | redis-load -u 172.19.32.xx:6379 -b

其实另一篇文章也写了这个问题(ruby版本过低问题的解决方法)

https://blog.51cto.com/wujianwei/2105124?cid=705255