标签:restore sele root 最新 hunk .com rect tab The
几种redis数据导出导入方式192.168.1.101 node1 redis源实例
192.168.1.102 node2 redis目标实例
192.168.1.103 node3 任意linux系统
redis-dump这款工具需要用到Ruby,而centos环境中的yum工具可以安装的Ruby版本最高是2.0的版本,而当前Redis最新的4.0版本中需要用到的Ruby >= 2.2版本,所以我们需要先安装Ruby,而安装Ruby有一个很好的命令行工具可以帮助我们,这款工具就是RVM,RVM可以提供一个便捷的多版本 Ruby 环境的管理和切换。
首先到tmp下,用来存放下载的安装文件
cd /tmp
mkdir rvm
cd rvm
# 开始安装RVM
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer
curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc
gpg --verify rvm-installer.asc
bash rvm-installer stable
source /etc/profile.d/rvm.sh
查看可以安装的Ruby版本
rvm list known
这里我们安装2.4.1
rvm install ruby 2.4.1
设置默认ruby
rvm use 2.4.1 --default
查看ruby版本
ruby --version
移除gem自带源(美国服务器下载慢)
gem sources --remove https://rubygems.org/
添加国内源
gem sources -a https://gems.ruby-china.com
查看仓库源
gem sources -l
*** CURRENT SOURCES ***
http://gems.ruby-china.com/
安装redis-dump
gem install redis-dump -V
安装redis-dump包括的gem包
drydock-0.6.9.gem
uri-redis-0.4.2.gem
yajl-ruby-1.4.1.gem
redis-dump-0.4.0.gem
redis-dump帮助命令
[root@node3 ~]# redis-dump --help
Try: /usr/local/bin/redis-dump show-commands
Usage: /usr/local/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
[root@node3 ~]#
导出命令
redis-dump -u 192.168.1.101:6379 > 192.168.1.101.json
导出指定数据库数据
redis-dump -u 192.168.1.101:6379 -d 5 > 192.168.1.101.json
如果redis设有密码
redis-dump -u :password@192.168.1.101:6379 > 192.168.1.101.json
redis-dump导出当前节点的所有keys;当redis 为cluster时,需要分别导出所有master的keys;
[root@node3 ~]# redis-load --help
Try: /usr/local/bin/redis-load show-commands
Usage: /usr/local/bin/redis-load [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)
-b, --base64 Decode key values from base64 (used with redis-dump -b)
-n, --no_check_utf8
-V, --version Display version
-D, --debug
--nosafe
[root@node3 ~]#
redis-load导入
cat 192.168.1.101.json | redis-load -u 192.168.1.102:6379
# 或者
< 192.168.1.101.json redis-load -u 192.168.1.102:6379
# 清空上文目标实例全部数据
[root@node1 ~]# redis-cli -h 192.168.1.102 -a password flushall
OK
# 源实例开启aof功能,将在dir目录下生成appendonly.aof文件
[root@node1 ~]# redis-cli -h 192.168.1.101 -a password config set appendonly yes
OK
# 假设appendonly.aof就在当前路径下
[root@node1 ~]# redis-cli -h 192.168.1.102 -a password --pipe < appendonly.aof
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 5
# 源实例关闭aof功能
[root@node1 ~]# redis-cli -h 192.168.1.101 -a password config set appendonly no
OK
暂略
[root@node1 ~]# cat redis_mv.sh
#!/bin/bash
redis-cli -h 192.168.1.101 -p 6379 -a password -n 0 keys "*" | while read key
do
redis-cli -h 192.168.1.101 -p 6379 -a password -n 0 --raw dump $key | perl -pe ‘chomp if eof‘ | redis-cli -h 192.168.1.102 -p 6379 -a password -n 1 -x restore $key 0
echo "migrate key $key"
done
标签:restore sele root 最新 hunk .com rect tab The
原文地址:http://blog.51cto.com/moerjinrong/2348391