Reputation: 7150
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
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) .
ssh hostname redis-cli --scan --pattern 'awesome_filter_pattern*'
dump = ssh.exec!("redis-cli dump #{key}").chomp
$redis.connection.restore(key, 0, dump)
Upvotes: 1
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:
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.
Post-restore: use the MOVE
command on the output of SCAN
ing your restored database - should be easily scriptable.
Upvotes: 3