Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created December 16, 2013 11:10
Show Gist options
  • Save dchaplinsky/7985473 to your computer and use it in GitHub Desktop.
Save dchaplinsky/7985473 to your computer and use it in GitHub Desktop.
Very small but handy tool to export keys from redis to json by wildcards and load them back. Dead simple, no error processing, no warnings, no support for huge datasets (beware, dumping/loading 1GB of data will most likely consume way more ram that you are happy to allocate for that purpose)
import os
import sys
from redis import Redis
import argparse
import simplejson as json
import settings
redis_client = Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,
db=settings.REDIS_DB)
PARSER = argparse.ArgumentParser(
description='Dump/Load key-value data from redis by wildcard')
PARSER.add_argument('--wildcard',
dest="wildcards",
help="Wildcard for keys to dump",
action='append')
PARSER.add_argument('operation',
choices=['dump', 'load'],
help="dump or load")
PARSER.add_argument('filename', help="Filename")
ARGS = PARSER.parse_args()
if ARGS.operation == "dump":
data = {}
total_count = 0
for w in ARGS.wildcards:
for key in redis_client.keys(w):
total_count += 1
data[key] = redis_client.get(key)
print("Got %s keys from redis. Saving to the file..." % total_count)
with open(ARGS.filename, "w") as fp:
json.dump(data, fp, indent=" ", sort_keys=True)
print("Saving of %s is done" % ARGS.filename)
elif ARGS.operation == "load":
with open(ARGS.filename, "r") as fp:
data = json.load(fp)
assert isinstance(data, dict)
pipe = redis_client.pipeline()
print("Got %s keys from file. Saving to redis..." % len(data.keys()))
for k, v in data.iteritems():
pipe.set(k, v)
pipe.execute()
print("Saved")
@erikvanzijst
Copy link

This only works for plain keys, not hashes, etc.

@sundquistm
Copy link

As I understand it, this will not retain TTL values.

@dchaplinsky
Copy link
Author

You are both correct :)

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