Several ways to export and import redis data

Posted by eddie_twoFingers on Thu, 05 Dec 2019 07:02:06 +0100

Several ways to export and import redis data

1 environmental description:

192.168.1.101 node1 redis source instance
 192.168.1.102 node2 redis target instance
 192.168.1.103 node3 any linux system

2 redis dump mode

2.1 installation of RVM

Redis dump needs to use ruby, and the maximum version of ruby that can be installed in centos environment is 2.0, while the current version of redis 4.0 needs to use Ruby > = 2.2, so we need to install Ruby first, and there is a good command-line tool to help us install ruby. This tool is RVM, which can provide a lot of convenience Management and switch of version Ruby environment.

First to tmp To store the downloaded installation files
cd /tmp
mkdir rvm
cd rvm
# Start RVM installation
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

2.2 installing Ruby

View the Ruby version that can be installed

rvm list known

Here we install 2.4.1

rvm install ruby 2.4.1 

Set default ruby

rvm use 2.4.1 --default

View ruby Version

ruby --version

2.3 install redis dump tool

Remove the gem's own source (US servers download slowly)

gem sources --remove https://rubygems.org/ 

Add domestic source

gem sources -a https://gems.ruby-china.com

View warehouse source

gem sources -l
*** CURRENT SOURCES ***

http://gems.ruby-china.com/

Install redis dump

gem install redis-dump -V

Install the gem package included in redis dump

drydock-0.6.9.gem
uri-redis-0.4.2.gem
yajl-ruby-1.4.1.gem
redis-dump-0.4.0.gem

2.4 redis dump export

Redis dump help command

[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 ~]#

Export command

redis-dump -u 192.168.1.101:6379 > 192.168.1.101.json

Export specified database data

redis-dump -u 192.168.1.101:6379 -d 5 > 192.168.1.101.json

If redis has a password

redis-dump -u :password@192.168.1.101:6379 > 192.168.1.101.json

Redis dump exports all the keys of the current node. When redis is a cluster, you need to export the keys of all the master nodes separately;

2.5 redis load import command

[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 import

cat 192.168.1.101.json | redis-load -u 192.168.1.102:6379

# perhaps
< 192.168.1.101.json redis-load -u 192.168.1.102:6379

3 aof import mode

3.1 generating aof data from source instance

# Clear all data of the above target instance
[root@node1 ~]# redis-cli -h 192.168.1.102 -a password flushall
OK
# The source instance enables the AOF function and generates the appendonly.aof file in the dir directory
[root@node1 ~]# redis-cli -h 192.168.1.101 -a password config set appendonly yes
OK

3.2 importing aof data from target instance

# Assume appendonly.aof is in the current path
[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
# Source instance turns off aof function
[root@node1 ~]# redis-cli -h 192.168.1.101 -a password config set appendonly no
OK

4 rdb file migration method

Tentatively

5 migration of source instance db0 to target instance db1

[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

Topics: Database Redis Ruby JSON