One example of such an environment is a database such as Redis. Since its an in-memory database, a simple error such as power failure can lead to data loss. It is therefore essential to have persistence for your data.
This tutorial will learn how to work with the Redis Database Backup to save the data stored in the memory to the system’s disk and perform restorations from the RDB.
Backing Up Data
We use the SAVE command to perform a backup of the current dataset in Redis. The command will create a snapshot containing all the data in the Redis cluster in the binary format of the dump.rdb file.
To use the SAVE command, type SAVE inside the Redis CLI.
OK
Once you execute the command, Redis should return a string, OK, indicating that no errors were encountered in the command.
If not, Redis will return an error message showing the error type in the command. For example, Redis will provide the following error for incorrect arguments.
(error) ERR wrong number of arguments for 'save' command
Once the SAVE command is executed successfully, Redis will create a dump.rdb file in the Redis directory.
Redis does not recommend using the SAVE command in production. This is because it blocks other clients until the operation is completed.
To resolve this, you can use the BGSAVE command. It works similarly to the SAVE command but uses a child process in the background.
For example:
Background saving started
Restoring Data
Backups serve no purpose if we cannot use them. Let us discuss how you can use the dump.rdb file to restore your datasets.
Start by noting the location of your backup dump.rbd file.
Next, stop the Redis server:
Move the dump.rdp backup file into the root of Redis server
You should now have the data from the dump.rdb loaded into Redis.
Conclusion
This article taught us how to perform backup and restoration in Redis using the RDB dump file.
Stay tuned for more tutorials!