Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple script to migrate all keys from one Redis to another
#!/usr/bin/env python
import argparse
import redis
def connect_redis(conn_dict):
conn = redis.StrictRedis(host=conn_dict['host'],
port=conn_dict['port'],
db=conn_dict['db'])
return conn
def conn_string_type(string):
format = '<host>:<port>/<db>'
try:
host, portdb = string.split(':')
port, db = portdb.split('/')
db = int(db)
except ValueError:
raise argparse.ArgumentTypeError('incorrect format, should be: %s' % format)
return {'host': host,
'port': port,
'db': db}
def migrate_redis(source, destination):
src = connect_redis(source)
dst = connect_redis(destination)
for key in src.keys('*'):
ttl = src.ttl(key)
# we handle TTL command returning -1 (no expire) or -2 (no key)
if ttl < 0:
ttl = 0
print "Dumping key: %s" % key
value = src.dump(key)
print "Restoring key: %s" % key
dst.restore(key, ttl, value)
return
def run():
parser = argparse.ArgumentParser()
parser.add_argument('source', type=conn_string_type)
parser.add_argument('destination', type=conn_string_type)
options = parser.parse_args()
migrate_redis(options.source, options.destination)
if __name__ == '__main__':
run()
@herolantian

This comment has been minimized.

Copy link

@herolantian herolantian commented Aug 25, 2016

you script have a small bug, ttl function get the expire time in seconds units, when use restore, the ttl value is in milliseconds unit so restore(key, ttl, value) should be restore(key, ttl * 1000, value)

@ramonsnir

This comment has been minimized.

Copy link

@ramonsnir ramonsnir commented Mar 6, 2017

@herolantian or use src.pttl(key)

@nahueloyha

This comment has been minimized.

Copy link

@nahueloyha nahueloyha commented Feb 15, 2018

Hi! By any chance do you have something similar with support for redis-cluster? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment