redis move all keys
Asked Answered
S

6

7

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.

Saboteur answered 26/3, 2011 at 10:43 Comment(0)
E
3

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.

Entasis answered 27/3, 2011 at 4:48 Comment(0)
B
2

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
Benco answered 11/11, 2011 at 7:33 Comment(0)
D
1

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/

Dalliance answered 20/2, 2012 at 9:9 Comment(0)
W
0

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

Waverley answered 21/6, 2011 at 10:52 Comment(1)
For a huge number of keys, it will probably not work. There is currently no way to incrementally iterate on the keyspace. The SCAN command will probably be part of Redis 2.8, but it is not yet available.Wishywashy
S
0

Python redis API's move() function provides exactly what you need.

for key in REDIS_CLIENT.scan_iter():
    REDIS_CLIENT.move(key, <desired_db_number>)

REDIS_CLIENT is initialized redis database client where you specify db_number to which is the client connected. For example if one specifies #3 and for .move(key, 5) than you move keys from db #3 to db #5.

Seclusion answered 11/1 at 9:36 Comment(0)
A
-3

I suggest you can try it as below: 1. copy the rdb file to another dir; 2. modify the rdb file name; 3. modify the redis configure file adapter to the new db;

Ayo answered 31/7, 2012 at 2:39 Comment(2)
flushdb will erase all the keys. Not funny.Wishywashy
⊙﹏⊙ Sorry, My English is so pool that I misunderstand Patrick's meaning to remove all keys.Ayo

© 2022 - 2024 — McMap. All rights reserved.