Skip to content

Instantly share code, notes, and snippets.

@iserko
Last active June 1, 2022 19:18

Revisions

  1. iserko revised this gist Feb 27, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion redis_migrate.py
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,6 @@ def migrate_redis(source, destination):
    value = src.dump(key)
    print "Restoring key: %s" % key
    dst.restore(key, ttl, value)
    return
    return


  2. iserko revised this gist Feb 27, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions redis_migrate.py
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,9 @@ def migrate_redis(source, destination):
    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
  3. iserko revised this gist Feb 27, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion redis_migrate.py
    Original file line number Diff line number Diff line change
    @@ -27,10 +27,11 @@ def migrate_redis(source, destination):
    src = connect_redis(source)
    dst = connect_redis(destination)
    for key in src.keys('*'):
    ttl = src.ttl(key)
    print "Dumping key: %s" % key
    value = src.dump(key)
    print "Restoring key: %s" % key
    dst.restore(key, 0, value)
    dst.restore(key, ttl, value)
    return
    return

  4. iserko revised this gist Feb 27, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions redis_migrate.py
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,12 @@ def conn_string_type(string):
    def migrate_redis(source, destination):
    src = connect_redis(source)
    dst = connect_redis(destination)
    print "test_key src", src.get('test_key')
    print "test_key dst", dst.get('test_key')
    for key in src.keys('*'):
    print "Dumping key: %s" % key
    value = src.dump(key)
    print "Restoring key: %s" % key
    dst.restore(key, 0, value)
    return
    return


  5. iserko created this gist Feb 27, 2014.
    42 changes: 42 additions & 0 deletions redis_migrate.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/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)
    print "test_key src", src.get('test_key')
    print "test_key dst", dst.get('test_key')
    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()