Skip to content

Instantly share code, notes, and snippets.

@Noah-Huppert
Created January 4, 2023 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noah-Huppert/0669564c473c16a435f6d09674c77051 to your computer and use it in GitHub Desktop.
Save Noah-Huppert/0669564c473c16a435f6d09674c77051 to your computer and use it in GitHub Desktop.
Tool to dump and restore Redis database. See instructions.md

Overview

A tool to dump a Redis server to a JSON file and then restore this file to another server.

Written to always dump and restore to database 0.

Instructions

Uses Python. To use the script:

  1. Install the redis package:
    pip install redis
    
  2. Dump a Redis server:
    python ./redis-dump-restore.py <host of dump redis> dump
    
  3. Restore to a Redis server:
    python ./redis-dump-restore.py <host of restore redis> restore
    
import redis
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument(
'host',
type=str,
)
parser.add_argument(
'action',
type=str,
choices=['dump', 'restore'],
)
args = parser.parse_args()
client = redis.Redis(host=args.host)
if args.action == 'dump':
values = {}
for key in client.keys("*"):
key = key.decode("utf-8")
values[key] = client.get(key).decode("utf-8")
print(f"dumped {key}")
with open("dump.json", "w") as f:
json.dump(values, f)
elif args.action == 'restore':
with open("dump.json", "r") as f:
values = json.load(f)
for key in values:
client.set(key, values[key])
print(f"restored {key}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment