Introduction:

Redis is a nosql database , a high performance key-value database management system that stores information in memory for fast access. It can be used to manage your users’ sessions or your site’s cache, for example.

In a previous a blog we have seen how to import a dataset or create one from you own .

To perform this task , the best practice is to backup your current database in order to perform a rollback if you encounter some issue during your import.

We will see how to perform that now!

Backup strategy

First of all let’s see how Redis writes DATA in the database:

  • Redis persistence

Persistence refers to the writing of data to durable storage, such as HDD or SSD.

Below you can find Redis a range of persistence options:

  • RDB (Redis Database): RDB persistence performs point-in-time snapshots of your dataset at specified intervals.
  • AOF (Append Only File): AOF persistence logs every write operation received by the server. These operations can then be replayed again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Redis protocol itself.
  • No persistence: You can disable persistence completely. This is sometimes used when caching.
  • RDB + AOF: You can also combine both AOF and RDB in the same instance.

Note from Redis site:

If you’d rather not think about the tradeoffs between these different persistence strategies, you may want to consider Redis Enterprise’s persistence options, which can be pre-configured using a UI.

Will see in a incoming blog the advantage and disadvantage of each solution , for the moment we will focus on how to backup and restore your db using a rdb file 🙂

Backup your Redis database

Locate your Redis Home folder

Before making your Database backup you have to locate your Redis home:

  • Start your Redis server:
  • Use command to check if database is running:

ps -eaf | grep redis

  • Use Redis-cli utility
redis-cli
  • Use the below command to get the home directory:
127.0.0.1:6379> config get dir
  • Exit from Redis and go to the Home directory:
  • Locate your dump.rdb file

You should find a dump.rdb file in the Redis home directory, it correspond to your database, so when you make a commit with your Redis commands it will write in this file .

  • Save your database

To perform a backup of your database your must use the SAVE command (or BGSAVE) :

From Redis site:

“The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.

You almost never want to call SAVE in production environments where it will block all the other clients. Instead usually BGSAVE is used. However in case of issues preventing Redis to create the background saving child (for instance errors in the fork(2) system call), the SAVE command can be a good last resort to perform the dump of the latest dataset.”

  • You can create a backup folder to store the old dump.rdb file you want , if you need to restore them or use it on another fresh Redis database.

Restore your Database

To restore your database you just need to substitute the dump.rdb file you made before with the current dump.rdb file in your Redis home

  • Let’s simulate a data loss and make a restoration :

We will remove our beloved Diego and Pele from the database

  • Now these entries are no more here:
  • As we have made a backup before that and we have stored it in our backup folder we will be able to restore these entries:
  • Before moving the backup to the Redis home we have to stop the Redis server:
  • We can now move the dump.rdb file to the current Redis home directory
  • Now we just have to restart the Redis server and check if our 2 world cup legends are back
  • Check if data are recovered :

That’s it , we get our data back 🙂

Conclusion

You know now how to backup and restore your data on Redis , there is many ways to administrate your backups such as using a cron or by using Snapshotting utility on Redis , we will see that in a next Blog.

For the moment feel free to check my other blogs and also the dbi bloggers to share knowledge and discover new features of your favorite soft