Recently, I was asked by a colleague about restoring a Redis database in docker (on a Windows WSL environment running Rancher Desktop). While I use Redis in my local WSL environment for development purposes on a day-to-day basis, I had never considered backups and restores. This post covers how to do that. While this is specific to a dockerized instance of Redis, the same should be applicable for Redis running on bare-metal or even in a virtual machine. First, let’s understand how Redis persists data.
Background
Redis is an in-memory database, but it supports persistence using RDB snapshots. An RDB snapshot is a point-in-time dump of Redis’ in-memory state written to disk, typically stored as a dump.rdb file. When Redis starts, if a dump.rdb file exists in its configured data directory, Redis will load it into memory automatically.
Step-by-Step: Creating and Restoring a Backup
1. Create a new Redis instance
Start a fresh Redis container using your usual setup (Docker, Docker Compose, or Rancher Desktop). Ensure Redis is configured with a persistent data directory (commonly /data inside the container).
2. Create test data
Connect to Redis using redis-cli and create a few keys:
SET key1 “value1” SET key2 “value2” SET key3 “value3”
This gives us some known data to verify later.
3. Force Redis to persist data to disk
Redis normally saves snapshots automatically, but for testing purposes it’s best to force a save. Using the redis-cli, run:
SAVE
This command instructs Redis to immediately write the in-memory database to disk as dump.rdb. At this point, Redis creates a file named: dump.rdb inside its data directory (commonly /_data).
Note: If you are running Redis in docker, and you need to open a terminal within your container to run redis-cli commands, open a new terminal window on your host macine and run:
docker exec -it your-redis-container /bin/sh
Notice how your terminal prompt changed? Now you are sitting at a terminal within your container itself and you can run commands directly against that container. Run:
redis-cli
Now you’re one level deeper. You can now run Redis commands within the shell of your container within the shell of your host operating system! Now you can run all sorts of Redis operations – to add keys, delete keys, search keys or run SAVE to force Redis to save its current state to a dump file.
4. Copy the backup file to a safe location
Locate the Redis data directory (usually a Docker volume mapped to /_data) and copy the dump.rdb file to a separate backup location on your machine. This file is now your portable Redis backup.
5. Remove the old Redis instance completely
To simulate a real restore scenario:
- Stop the Redis container
- Remove the container
- Remove any associated volumes or persistent data This ensures the next Redis instance starts with no existing data.
6. Create a brand-new Redis instance
Start a new Redis container with a fresh data directory. Before starting Redis:
- Stop the container (if it auto-started)
- Copy the previously saved dump.rdb file into the new instance’s data directory Make sure the file is named exactly:
dump.rdbRedis will not load files with different names.
7. Restart Redis
Start the Redis container again. During startup, Redis detects the dump.rdb file and automatically loads it into memory.
8. Verify the restore
Connect using Redis Insight or redis-cli and confirm that your test keys exist:
KEYS *
You should see:
key1 key2 key3
This confirms that the database was successfully restored from the backup.
Locating the Redis _data Directory (For Rancher Desktop Users)
It may not be immediately apparent where to go to grab or replace the dump.rdb files when using Rancher Desktop over WSL, on Windows. To figure out where Redis is saving those, open a new terminal window and run:
docker inspect your-redis-container
The output provides a lot of useful information about your container. Locate the Mounts section within the output and within it, you’ll see the location for the _data directory. In my setup, it looked something like:
"Source": "/var/lib/docker/volumes/[a-long-string]/_data"
Make a note of the above location. Open a new Windows Exporer window and navigate to:
\\wsl.localhost\rancher-desktop-data
From there, you can get to the _data directory by traversing the path you noted earlier.
Key Takeaways
- Always stop Redis before overwriting persistence files
- File naming matters (dump.rdb must be exact)
- Redis restores happen only at startup
- RDB snapshots are simple, portable, and reliable for backups especially when you want to share that data with your teammates or even hydrate to a base-state for dev/testing purposes.
