Reputation: 21
I have two Redis instances (or two respective dump.rdb files) and I would like to combine them into a single instance with two dbs, one for each respective initial instance.
I could do this using MIGRATE, but it is only available in Redis 2.6.0, which is not supported by my current Redis server.
Upvotes: 2
Views: 6457
Reputation: 31528
This answer is no longer accurate for newer versions of redis. Leaving the answer for historical reasons.
If you are willing to play around a bit with binary files, you can easily combine the two dump.rdb files into one.
Assumptions :
If you open the file in a hex editor, this is the format of the RDB file -
REDIS000x FE 00 <actual data > FF
Here -
000x
is the rdb version number. It will most likely be 0002 or 0003 in your caseFE
is the database selector, and 00
is the database number<actual data>
is the key-value pairs in the current database. You can treat this as a binary blob for your current purpose.FF
is the last byte in the file and indicates the end of the rdb fileSo to merge the two rdb files, do the following -
FF
FE 01
to indicate start of second databaseFE 01
mentioned above.REDIS000x FE 00
FF
You can now copy this new dump.rdb to the appropriate directory in redis and restart.
If you are interested, here is complete documentation of redis dump file format, but you don't need to understand all of it for this simple use case.
Upvotes: 9
Reputation: 12398
I had a lot of troubles with it , so I've created a python script that uses dump and restore. It serializes one db to a dictionary object and picles it. With a different switch the picled file is loaded and the data is uploaded to other redis instance.
https://gist.github.com/romanmah/9664407
Upvotes: 0
Reputation: 437
Here's a technique that I used to consolidate 4 Redis servers (running 2.4.x) into 2 (I, two times, combined 2 separate Redis instances (each with data in db0) into 1 containing two separate dbs):
redis.conf
and use slaveof
).sed -e 's/\"db\":0/\"db\":3/' orig.json > db_3.json
redis-load
command to import the JSON file containing the data from instance B into Foo.
<new.json redis-load -u host:port -d 3
Now you have a single Redis instance (Foo) with A's data in db0 and B's data in db1.
Upvotes: 3