Redis move all keys

Can I use the redis MOVE command to move all keys from one database to another? The move command only moves 1 key, but I need to move all the keys in the database.

+6
redis
source share
5 answers

I would recommend taking a look at the following alpha version application for backing up and restoring redis databases .. (you can install it via gem install redis-dump ). You can recreate the database and then reload it into another database with the --database argument.

redis-dump project

If this does not meet your goals, you may need to use scripting language cross-references (or, alternatively, dump something together using bash / redis-cli / xargs, etc.). If you need help with these lines, we should probably get more details first.

+3
source share

I wrote a small python script to move data between two redis servers: (only a support list and line types, and you have to install the python redis client):

 ''' Created on 2011-11-9 @author: wuyi ''' import redis from optparse import OptionParser import time def mv_str(r_source, r_dest, quiet): keys = r_source.keys("*") for k in keys: if r_dest.keys(k): print "skipping %s"%k continue else: print "copying %s"%k r_dest.set(k, r_source.get(k)) def mv_list(r_source, r_dest, quiet): keys = r_source.keys("*") for k in keys: length = r_source.llen(k) i = 0 while (i<length): print "add queue no.:%d"%i v = r_source.lindex(k, i) r_dest.rpush(k, v) i += 1 if __name__ == "__main__": usage = """usage: %prog [options] source dest""" parser = OptionParser(usage=usage) parser.add_option("-q", "--quiet", dest="quiet", default = False, action="store_true", help="quiet mode") parser.add_option("-p", "--port", dest="port", default = 6380, help="port for both source and dest") parser.add_option("", "--dbs", dest="dbs", default = "0", help="db list: 0 1 120 220...") parser.add_option("-t", "--type", dest="type", default = "normal", help="available types: normal, lpoplist") parser.add_option("", "--tmpdb", dest="tmpdb", default = 0, help="tmp db number to store tmp data") (options, args) = parser.parse_args() if not len(args) == 2: print usage exit(1) source = args[0] dest = args[1] if source == dest: print "dest must not be the same as source!" exit(2) dbs = options.dbs.split(' ') for db in dbs: r_source = redis.Redis(host=source, db=db, password="", port=int(options.port)) r_dest = redis.Redis(host=dest, db=db, password="", port=int(options.port)) print "______________db____________:%s"%db time.sleep(2) if options.type == "normal": mv_str(r_source, r_dest, options.quiet) elif options.type == "lpoplist": mv_list(r_source, r_dest, options.quiet) del r_source del r_dest 
+1
source share

you can try my own tool rdd

this is a command line utility

can dump the database into a file, work with it (filter, map, merge, ...) and return it to the redis instance

take care alpha stage, https://github.com/r043v/rdd/

+1
source share

Now that redis has scripts using lua, you can easily write a command that passes all the keys, checks their type and moves them accordingly to the new database.

0
source share

I suggest you try the following: 1. copy the rdb file to another directory; 2. change the name of the rdb file; 3. change the redis configure adapter to a new db;

-3
source share

All Articles