Taking a backup of the current state of a Redis instance is a fairly simple matter. It involves forcing the contents of RAM to be persisted to disk and copying the resulting dump.rdb file to another location.
First, let's check the last time the contents of our Redis instance were written to disk. We can do that with the lastsave command in the redis-cli:
127.0.0.1:6379> lastsave (integer) 1501703515
This command returns the UNIX epoch timestamp of the last time the instance's data was persisted to disk.
Next, let's ensure that the contents of our Redis instance have been written to disk. We can do that with the bgsave command in the redis-cli:
127.0.0.1:6379> bgsave Background saving started
This executes a background save against the current instance, persisting the contents of RAM to disk. It is important to note that this command does not affect the instance's ability to handle requests.
If the bgsave command errors out or does not complete, we can use the save command instead. save invokes an immediate point-in-time snapshot that blocks all other requests until its completion. Therefore, you should only use it in a production environment as the last resort.
To see if the bgsave command has completed, we can (again) run the lastsave command:
127.0.0.1:6379> lastsave (integer) 1501703515 127.0.0.1:6379> lastsave (integer) 1501703515 127.0.0.1:6379> lastsave (integer) 1501790543
When the output of lastsave displays a new timestamp, then bgsave has completed. Next, we copy the dump.rdb file, and move it to another location. Before doing that, check to see where the dump.rdb file was written to:
127.0.0.1:6379> config get dir 1) "dir" 2) "/var/lib/redis/6379"
Now exit the redis-cli, and create a backup directory:
127.0.0.1:6379> exit mkdir /local/redis/backup
We will then verify the location of the dump.rdb file, based on the result of the config get dir command:
ls -al /var/lib/redis/6379/ total 12 drwxr-xr-x 2 root root 4096 Aug 3 15:02 . drwxr-xr-x 3 root root 4096 Jun 18 19:38 .. -rw-r--r-- 1 root root 394 Aug 3 15:02 dump.rdb
Next, we will copy the dump.rdb file to the /local/redis/backup directory:
cp /var/lib/redis/6379/dump.rdb /local/redis/backup/dump_20170803.rdb