This guide provides step-by-step instructions for creating and restoring a dump in Redis. Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. Dumps are a way to back up and restore data in Redis.
To create a dump of your Redis database, follow these steps:
-
Connect to Redis: Open a terminal or command prompt and connect to your Redis instance using the
redis-clicommand:redis-cli -a $(sudo grep -Po '^requirepass \K.*' /etc/redis/redis.conf) -
Create the Dump: Use the
SAVEcommand to create a dump of the current database. This command saves the dataset to a file calleddump.rdbin the Redis data directory.SAVE
-
Create the Dump: Exit redis environment
exit -
Make a copy: Copy the dump file
dump.rdbin/var/lib/redisand make a copy of itsudo cp -p /var/lib/redis/dump.rdb /var/lib/redis/dump.bak.rdb
And, same for the file
appendonly.aofsudo cp -p /var/lib/redis/appendonly.aof /var/lib/redis/appendonly.bak.aof
IMPORTANT
Copy both files otherwise your restoration won't work! -
Verify the Dump: Check that the copy of the files
dump.bak.rdbandappendonly.bak.aofhave been created in/var/lib/redis.ls /var/lib/redis
To restore a dump in Redis, follow these steps:
-
Stop the Redis Server: If Redis is running, stop the Redis server:
sudo systemctl stop redis-server.service
-
Replace the Dump File: Restore the dump file
dump.bak.rdbin/var/lib/redissudo cp -p /var/lib/redis/dump.bak.rdb /var/lib/redis/dump.rdb
And, same for the file
appendonly.bak.aofsudo cp -p /var/lib/redis/appendonly.bak.aof /var/lib/redis/appendonly.aof
IMPORTANT
Restore both files otherwise your restoration won't work! -
Start the Redis Server: Start the Redis server again.
sudo systemctl start redis-server.service
-
Verify the Restoration: Connect to Redis using
redis-cliand verify that the data has been restored correctly:redis-cli KEYS *This command will display all keys in the database, confirming that the restoration was successful.
- It's important to ensure that Redis is stopped before replacing the dump file to avoid data corruption.
For more information about Redis and its commands, refer to the Redis Documentation.