Dragonfly Cloud announces new enterprise security features - learn more

Question: How can I create and restore a backup in Redis?

Answer

Redis provides the functionality to create and restore backups of your database. Let's see how you can do it.

Creating a Backup

For creating a backup, Redis uses the SAVE or BGSAVE commands. The SAVE command operates synchronously but will block other operations until it has completed. On the other hand, BGSAVE performs the backup process in the background. Here is an example:

redis-cli bgsave

After running this command, a dump file named dump.rdb will be created in your Redis directory.

Restoring a Backup

Restoring a backup in Redis is quite simple as well. It involves just moving your backup file (dump.rdb) to the proper Redis directory and starting your server. Redis automatically loads the dump.rdb file on startup.

To find the default directory where Redis looks for the dump.rdb file, you can check your Redis configuration file (usually named redis.conf). Look for the line that starts with dir.

Here's how you can find and set the correct path:

  1. Open your Redis configuration file:

    nano /etc/redis/redis.conf
  2. Locate the dir configuration. It should look something like this:

    dir /var/lib/redis
    
  3. Use the path specified in the dir line for the following steps.

Here is a simple example of restoring a backup:

  1. First, stop your Redis server:

    redis-cli shutdown
  2. Then, move your backup file to the Redis directory found in the configuration:

    mv /path/to/your/dump.rdb /path/to/redis/dir/
  3. Finally, start your Redis server again:

    redis-server

Please note, the path /path/to/your/ and /path/to/redis/dir/ need to be replaced with actual paths in your system that you found in the redis.conf file.

In conclusion, Redis makes it easy to create backups using the SAVE or BGSAVE commands and restoring them by simply moving the backup file to the correct directory and restarting the server. Be sure to always back up your data regularly and verify your backups for security and reliability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost