Created
January 12, 2015 23:39
-
-
Save 72squared/8e9adb5fa5dff0ec65c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import redis | |
import rediscluster | |
import argparse | |
def main(srcs, dsts, verbose=False): | |
srcs = [redis.StrictRedis(**host) for host in srcs] | |
if len(dsts) > 1: | |
dst = rediscluster.RedisCluster(startup_nodes=dsts) | |
else: | |
dst = redis.StrictRedis(**dsts[0]) | |
ct = 0 | |
for src in srcs: | |
cursor = 0 | |
while True: | |
cursor, keys = src.scan(cursor=cursor, count=500) | |
if not keys: | |
break | |
pipe = src.pipeline(transaction=False) | |
for key in keys: | |
pipe.dump(key) | |
pipe.pttl(key) | |
res = pipe.execute() | |
pipe = dst.pipeline(transaction=False) | |
for i, key in enumerate(keys): | |
data = res[i * 2] | |
pttl = int(res[(i * 2) + 1]) | |
if len(data) < 1: | |
continue | |
if pttl < 1: | |
pttl = 0 | |
if verbose: | |
print key | |
ct += 1 | |
pipe.delete(key) | |
pipe.restore(key, pttl, data) | |
res = pipe.execute(raise_on_error=False) | |
for data in res: | |
if isinstance(data, Exception): | |
print data | |
if not verbose: | |
print " - %s" % ct | |
if cursor == 0: | |
break | |
print "processed %s keys" % ct | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='import data from redis shards into current redis server') | |
parser.add_argument('-s', | |
'--src', | |
type=str, | |
required=True, | |
help='comma separated list of hosts in the form of hostname:port') | |
parser.add_argument('-d', | |
'--dst', | |
type=str, | |
required=True, | |
help='comma separated list of hosts in the form of hostname:port') | |
parser.add_argument('-v', '--verbose', type=bool, default=False, help='turn on verbose output') | |
return parser.parse_args() | |
def parse_hosts(hoststring): | |
hosts = [] | |
for hoststring in hoststring.split(','): | |
hoststring = hoststring.strip() | |
hostname, port = hoststring.split(':') | |
hosts.append({'host': hostname, 'port': int(port)}) | |
return hosts | |
if __name__ == '__main__': | |
args = parse_args() | |
main(parse_hosts(args.src), parse_hosts(args.dst), args.verbose) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment