dcarneiro
dcarneiro

Reputation: 7150

Restore Redis dump to a different database

How can I dump a redis that's running on database 0 and restore it in my local machine on a different database (8) ?

I already secure copied the dump file:

scp hostname@/var/lib/redis/dump.rdb .

But if I change my local redis dump.rdb with this one, I'll get the data on database 0. How can I restore it to a specific database?

Upvotes: 0

Views: 2665

Answers (2)

dcarneiro
dcarneiro

Reputation: 7150

I ended up creating a script in Ruby to dump and restore the keys I wanted. (Please note that this approach is slow, takes around 1 min for 200 keys) .

  • Get the keys to dump / restore

ssh hostname redis-cli --scan --pattern 'awesome_filter_pattern*'

  • Open an ssh connection to the production server
  • Dump the remote key

dump = ssh.exec!("redis-cli dump #{key}").chomp

  • Restore it on localhost

$redis.connection.restore(key, 0, dump)

Upvotes: 1

Itamar Haber
Itamar Haber

Reputation: 49932

Firstly note that the use of numbered/shared Redis databases is inadvisable. You really should consider using dedicated Redis servers with a single DB (0) on them (more info at: https://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances)

Redis does not offer a straightforward way to do this, but there are two basic ways one could go about it:

  1. Pre-processing: modify the dump.rdb file to load into your database of choosing. You could build a tool for that or perhaps use one of the existing ones. Jan-Erik has done an outstanding job of documenting the RDB v7 format at http://rdb.fnordig.de/file_format.html so all you need to do is basically change the Database Selector byte.

  2. Post-restore: use the MOVE command on the output of SCANing your restored database - should be easily scriptable.

Upvotes: 3

Related Questions