How to Take Redis Backup and Restore from One Server to Another on Ubuntu?

How to Take Redis Backup and Restore from One Server to Another on Ubuntu?

Redis is an in-memory data structure store widely used as a cache, message broker, and database. To ensure data safety and continuity, it's crucial to back up Redis data and restore it to another server when needed. In this guide, we'll walk you through the steps to back up and restore Redis from one Ubuntu server to another.


Step 1: Check Redis Status on Source Server

Before taking a backup, ensure Redis is running on the source server.

sudo systemctl status redis

If Redis is not running, start it using:

sudo systemctl start redis

Step 2: Locate Redis Data Files

Redis stores its data in an RDB (Redis Database) file. The default location is:

/var/lib/redis/dump.rdb

To confirm the file location, check the Redis configuration file:

grep 'dir' /etc/redis/redis.conf

Step 3: Backup Redis Database

To ensure data consistency, save the Redis snapshot before copying:

redis-cli save

Once saved, copy the dump file to a safe location:

sudo cp /var/lib/redis/dump.rdb /backup/

You can also use tar to compress it:

tar -czvf redis_backup.tar.gz /var/lib/redis/dump.rdb

Step 4: Transfer Backup to Destination Server

Use SCP or rsync to transfer the backup to another server.

Using SCP:

scp /backup/redis_backup.tar.gz user@destination-server:/backup/

Using rsync:

rsync -avz /backup/redis_backup.tar.gz user@destination-server:/backup/

Step 5: Stop Redis on the Destination Server

Before restoring, stop Redis to avoid data corruption.

sudo systemctl stop redis

Step 6: Restore Redis Backup

Extract the backup file:

tar -xzvf /backup/redis_backup.tar.gz -C /var/lib/redis/

Set correct ownership and permissions:

sudo chown redis:redis /var/lib/redis/dump.rdb
sudo chmod 660 /var/lib/redis/dump.rdb

Step 7: Start Redis and Verify Data

Restart Redis on the destination server:

sudo systemctl start redis

Check if Redis is running:

sudo systemctl status redis

Verify data by connecting to Redis and checking keys:

redis-cli
keys *

Conclusion

By following these steps, you can easily back up and restore your Redis database from one Ubuntu server to another. This ensures data persistence and recovery in case of failures. For regular backups, consider automating the process using cron jobs.