How to recover redis data from snapshot(rdb file) copied from another machine?

I transferred my redis snapshot (dump.rdb file) using scp to a remote server. I need to run a redis server on this remote and recover the data from the dump.rdb file. How can I do that?


Solution 1:

There is nothing specific to do. Just install the redis server on the new machine, and edit the configuration file. You just need to change the following parameters to point to the location of the dump file you have just copied.

# The filename where to dump the DB
dbfilename mydump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# 
# Also the Append Only File will be created inside this directory.
# 
# Note that you must specify a directory here, not a file name.
dir /data/mydirectory/

Finally, the redis server can be started in the normal way.

Solution 2:

For databases where the appendonly flag is set to no, you can do the following:

  1. Stop redis (because redis overwrites the current rdb file when it exits).
  2. Copy you backup rdb file to the redis working directory (this is the dir option in your redis config). Also make sure your backup filename matches the dbfilename config option.
  3. Start redis.

If, on the other hand, you need to restore a rdb file to an append only database, you should do something along the lines of:

  1. Stop redis (because redis overwrites the current rdb file when it exits).
  2. Copy your backup rdb file to the redis working directory (this is the dir option in your redis config). Also make sure your backup filename matches the dbfilename config option.
  3. Change the redis config appendonly flag to no (otherwise redis will ignore your rdb file when it starts).
  4. Start redis.
  5. Run redis-cli BGREWRITEAOF to create a new appendonly file.
  6. Restore redis config appendonly flag to yes.

Specifically, this is the relevant bit of documentation from the redis config file comments:

# Note that you can have both the async dumps and the append only file if you                                                     
# like (you have to comment the "save" statements above to disable the dumps).                                                    
# >> Still if append only mode is enabled Redis will load the data from the                                                          
# >> log file at startup ignoring the dump.rdb file. 

Solution 3:

Assuming that you run Redis 2.6 or higher, your Redis snapshot filename is dump.rdb, and it exists in the directory /home/user/dbs, the following command would do the trick:

redis-server --dbfilename dump.rdb --dir /home/user/dbs

Relevant section from the official documentation: Passing arguments via the command line

Solution 4:

Or you can:

  1. Stop your redis server / instance, eg., service redis6379 stop
  2. Copy the dump.rdb file to the right location, eg., cp /path/to/dump-6379.rdb /var/lib/redis/dump-6379.rdb. Give it the right permissions (user:group should be redis:redis and mode 644)
  3. Start your redis server / instance, eg., service redis6379 start

It is important that you stop the redis server before copying the file to the right location, because Redis saves a snapshot before terminating, so it will replace your file.

Besides, you might want to back up the existing dump.rdb file first.