Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis move all keys

Tags:

redis

is it possible to use redis's MOVE command to move all keys from 1 database to another? The move command only moves 1 key, but I need to move all the keys in the database.

like image 883
Patrick Avatar asked Mar 26 '11 10:03

Patrick


People also ask

How can you move a key to another database using Redis key commands?

You can move an individual key to another database in your Redis instance with the move command. move takes the name of a key and the database where you want to move the key as arguments. For example, to move the key key_1 to database 8 , you would run the following: move key_1 8.

How do I transfer data from one Redis to another?

Another method for migrating Redis data is to take a snapshot of the data held on your source instance with either Redis's save or bgsave commands. Both of these commands export the snapshot to a file ending in . rdb , which you would then transfer to the target server.

How do I get all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How many keys Redis can store?

Redis can handle up to 2^32 keys, and was tested in practice to handle at least 250 million keys per instance. Every hash, list, set, and sorted set, can hold 2^32 elements. In other words your limit is likely the available memory in your system.


3 Answers

I would recommend taking a look at the following alpha version app to backup and restore redis databases.. (you can install it via gem install redis-dump). You could redis-dump your databaseand then redis-load into another database via the --database argument.

redis-dump project

If this doesn't fit your purposes, you may need to make use of a scripting language's redis bindings (or alternatively throw something together using bash / redis-cli / xargs, etc). If you need assistance along these lines then we probably need more details first.

like image 78
octagonC Avatar answered Oct 03 '22 00:10

octagonC


I've wrote a small python script to move data between two redis servers:(only support list and string types, and you must install 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
like image 33
wuyi Avatar answered Oct 03 '22 02:10

wuyi


you can try my own tool, rdd

it's a command line utility,

can dump database to a file, work on it (filter, match, merge, ...), and back it in a redis instance

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

like image 31
r043v Avatar answered Oct 03 '22 01:10

r043v