Backup routine for Redis

— 2013-10-24 —

After I created the memoris API and started to use it I had to set up a backup routine for the Redis database running behind it. I only run one Redis server not a cluster.

Lets dump some data

Redis has a command to save .rdb file called BGSAVE. The command will asynchronously save a dump.rdb file in the same folder as your redis server. This means that one probably would want to move the file and rename it. To create the dump file run the command below.

$ redis-cli bgsave

It is possible to use the SAVE command as well, but I'd like to do it asynchronously because ...

Structuring backup files

I would like to take a dump of the database every other hour. This backup is just incase of data coruption or me doing something stupid like overwriting something I should not overwrite. That said, those backups will be the base for my remote backup as well. The remote backup will be created by another machine fetching the latest backup from the server of Secure copy(scp).

I created to cron jobs that look like this:

3  */2 * * * redis-cli bgsave
23 */2 * * * mv /var/lib/redis/dump.rdb /backups/redis/`date +%Y-%m-%d`.rdb

The last will copy the dump file to a backup directory. Since only year, month and day is specified it will only be one dump each day, but it will be overwritten every other hour of that day. There is 20 min between the two backups so I do not have to fear the second cron job running before the dump is finished. That number could be adjusted to be closer to the average time of a dump of the database size if one would want to.

Puppet manifest

I created a puppet manifest to create the cron jobs and to ensure that the backup directories are in place. I created a gist of the puppet manifest if you want to clone.

file{ [ '/backups', '/backups/redis' ]:
  ensure  => 'directory',
  owner   => 'root'
cron{ 'redis-bgsave':
  command => 'redis-cli bgsave',
  user    => 'root',
  hour    => '0-23/2',
  minute  => 3,

cron{ 'redis-move-backup':
  command => 'mv /var/lib/redis/dump.rdb /backups/redis/`date +%Y-%m-%d`.rdb',
  user    => 'root',
  hour    => '0-23/2',
  minute  => 23,
  require => [ File['/backups/redis'], Cron['redis-bgsave'] ]