RSS Amplifier

GingerDev · Oct 8, 2024

Redis DB migration in less than 5 minutes

0
Sign in to vote or save

Abhinay Kumar · GingerDev

Recently at MyCaptain, we had to migrate our entire infrastructure to Azure to keep the cost of running applications to $0.00 (I will write about keeping the cost to practically zero for at least three years in a post soon). Sticking to our character we did not migrate until the last minute :). Among all the other tasks, migrating the Redis DB took surprisingly less time than I thought and that prompted me to share this quick post with everyone.

Let me show you how easy it is.

First, SSH into your remote server(source) where Redis is hosted.

ssh remote_username@remote_host

Now, login to the Redis command line and find out the data directory by typing the CONFIG command.

$ redis-cli
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis/" or "data/redis" -> on cloud66
127.0.0.1:6379> SAVE
OK

You should see a directory listed where Redis stores the data dump. In my case it was under the data/redis directory as the server was setup through Cloud66 and they might have changed the default location for the dump in this case however without any changes you should see /var/lib/redis/

Next, Download the file locally from the remote server(source):

scp remote_username@remote_host:/data/redis/dump.rdb ./

Upload dump.rdb file to the remote server(destination):

scp ./dump.rdb remote_username@remote_host:/tmp/

Stop the redis server(destination) in case it is running (This is an important step as Redis keeps replacing the dump.rdb file at a certain interval and if you copy the file without stopping the server, very likely it will be replaced with another dump.)

sudo systemctl stop redis

Verify if the server(destination) has stopped:

sudo systemctl status redis
○ redis.service - Cloud66 Redis Datastore Server
     Loaded: loaded (/lib/systemd/system/redis.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Tue 2024-09-24 03:52:18 UTC; 20s ago
    Process: 136595 ExecStart=/opt/redis/src/redis-server /opt/redis/redis.conf (code=exited, status=0/SUCCESS)
   Main PID: 136595 (code=exited, status=0/SUCCESS)
        CPU: 3.741s
Sep 24 03:28:39 swan systemd[1]: Started Cloud66 Redis Datastore Server.
Sep 24 03:52:18 swan systemd[1]: Stopping Cloud66 Redis Datastore Server...
Sep 24 03:52:18 swan systemd[1]: redis.service: Deactivated successfully.
Sep 24 03:52:18 swan systemd[1]: Stopped Cloud66 Redis Datastore Server.
Sep 24 03:52:18 swan systemd[1]: redis.service: Consumed 3.741s CPU time.

Move the dump.rdb file to the Redis data directory:

sudo mv /tmp/dump.rdb dump.rdb

Make sure dump.rdb is owned by redis user:

sudo chown redis:redis /data/redis/dump.rdb
sudo chmod 644 /data/redis/dump.rdb

Finally, start the server(destination):

sudo systemctl start redis

and verify whether the server is running:

sudo systemctl status redis
● redis.service - Cloud66 Redis Datastore Server
     Loaded: loaded (/lib/systemd/system/redis.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-09-24 03:55:23 UTC; 18s ago
   Main PID: 138064 (redis-server)
      Tasks: 6 (limit: 19182)
     Memory: 60.2M
        CPU: 577ms
     CGroup: /system.slice/redis.service
             └─138064 "/opt/redis/src/redis-server 0.0.0.0:6379" "" "" "" "" "" "" "" "" ""
Sep 24 03:55:23 swan systemd[1]: Started Cloud66 Redis Datastore Server.

Check whether relevant keys are present in the DB:

redis-cli
KEYS *
=> 1) "do_not_cache_everything_key"
    2) "cache_some_key"
    :
    :
    :
    :

That’s it. Hope you find it useful and do let me know the kind of topics you would like to read about. Thanks

Read the original on gingerdev.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.