Redis currently has two persistence strategies: RDB * and AOF  (both can be used concurrently). The backup strategy is the same for both, however restoring is a little different if AOF is enabled (whether or not RDB is also enabled).

Backup

The redis-cli utility will be used to create the backup. This process is the same regardless of the persistence strategy configured within Redis. The process will be as follows :

  • Determine the directory where the backup files are written. This directory is stored in the redis configuration file (typically /etc/redis/redis.conf) under the dir keyword. At present this location is hard-coded in the redis config.template file, however it could be exposed in the future through configuration groups.
  • Find the time of the last Redis persistence run, and retain the information temporarily. This is accomplished by executing the LASTSAVE command through the redis-cli, and is necessary to determine when the next run completes .
  • Start a backup using the redis-cli utility. This is done by executing the BGSAVE command.  Note: If Redis persistence is turned off (it is on by default in the config.template) then enough disk space must be available to write the backup file.
  • Wait until the backup completes. This is determined by polling using the LASTSAVE command as above, and waiting until the timestamp changes. 
  • Compress/encrypt the backup
  • Stream the compressed/encrypted output to storage in Swift under the database_backups container. If persistence is turned off, the backup file can then be deleted.

Restore

Restoring a Redis server from backup depends on the persistence method that is running on the server. 

To restore a Redis server from a backup:

  • Determine if AOF mode is enabled

If AOF is disabled:

  • Determine the location where backup files are located
  • Make sure the redis server is not running (stop if necessary)
  • Remove the existing dump.rdb file
  • Retrieve the backup from storage
  • Put the backup into the proper location
  • Change the ownership to redis:redis
  • Start the redis server

If AOF is enabled:

  • Determine the location where backup files are located
  • Make sure the redis server is not running (stop if necessary)
  • Remove existing dump.rdb and appendonly.aof
  • Retrieve the backup from storage
  • Put the backup into the proper location
  • Change the ownership to redis:redis
  • Disable AOF in the Redis configuration
  • Start the redis server
  • Create a new AOF file
  • Stop the Redis server
  • Turn on AOF in the Redis configuration
  • Start the Redis server

Locating the Redis Data Directory

Redis stores its data in a directory on your server, which is what we want to back up. First we need to know where it is.

In Ubuntu and other Linux distributions, the Redis database directory is /var/lib/redis. But if you’re managing a server that you inherited and the Redis data location was changed, you can locate it by typing:

 sudo locate *rdb 

While connected to Redis, the next two commands will authenticate to it and get the data directory:

auth insert-redis-password-here

config get dir

The output of the last command should be your Redis data directory:Output

1) "dir"
2) "/var/lib/redis"

Make note of your Redis directory. If it’s different than the directory shown, make sure you use this directory throughout the tutorial.

You can exit the database command line interface now:

exit

Check that this is the correct directory:

ls /var/lib/redis

You should see a dump.rdb file. That’s the Redis data. If appendonly is also enabled, you will also see an appendonly.aof or another .aof file, which contains a log of all write operations received by the server.

See this post about Redis persistence for a discussion of the differences between these two files. Basically, the .rdb file is a current snapshot, and the .aof file preserves your Redis history. Both are worth backing up.

We’ll start with just the .rdb file, and end with an automated backup of both files.

Backing Up the Redis Data

Now that you know where your Redis data are located, it’s time to make the backup. From the official Redis website comes this quote:

Redis is very data backup friendly since you can copy RDB files while the database is running: the RDB is never modified once produced, and while it gets produced it uses a temporary name and is renamed into its final destination atomically using rename(2) only when the new snapshot is complete.

So, you can back up or copy the database file while the Redis server is running. Assuming that you’re backing it up to a directory under your home folder, performing that backup is as simple as typing:

sudo cp /var/lib/redis/dump.rdb /home/sammy/redis-backup-001

Redis saves content here periodically, meaning that you aren’t guaranteed an up-to-the-minute backup if the above command is all you run. You need to save your data first.

However, if a potentially small amount of data loss is acceptable, just backing up this one file will work.

Saving the Database State

To get a much more recent copy of the Redis data, a better route is to access redis-cli, the Redis command line.

Then, issue the save command like so:

save

The output should be similar to this:Output

OK
(1.08s)

Exit the database.

Now you may run the cp command given above, confident that your backup is fully up to date.

While the cp command will provide a one-time backup of the database, the best solution is to set up a cron job that will automate the process, and to use a tool that can perform incremental updates and, if needed, restore the data.

Configuring Automatic Updates with rdiff-backup and Cron

In this section, we’ll configure an automatic backup that backs up your entire Redis data directory, including both data files.

There are several automated backup tools available. In this tutorial, we’ll use a newer, user-friendly tool called rdiff-backup.

rdiff-backup a command line backup tool. It’s likely that rdiff-backup is not installed on your server, so you’ll first have to install it:

sudo apt-get install -y rdiff-backup

Now that it’s installed, you can test it by backing up your Redis data to a folder in your home directory. In this example, we assume that your home directory is /home/sammy:

Note that the target directory will be created by the script if it does not exist. In other words, you don’t have to create it yourself.

With the –preserve-numerical-ids, the ownerships of the source and destination folders will be the same.

sudo rdiff-backup --preserve-numerical-ids /var/lib/redis /home/sammy/redis

Like the cp command earlier, this is a one-time backup. What’s changed is that we’re backing up the entire /var/lib/redis directory now, and using rdiff-backup.

Now we’ll automate the backup using cron, so that the backup takes place at a set time. To accomplish that, open the system crontab:

sudo crontab -e

(If you haven’t used crontab before on this server, select your favorite text editor at the prompt.)

At the bottom of the filek append the entry shown below.

 0 0 * * * rdiff-backup --preserve-numerical-ids --no-file-statistics /var/lib/redis /home/sammy/redis 

This Cron entry will perform a Redis backup every day at midnight. The –no-file-statistics switch will disable writing to the file_statistics file in the rdiff-backup-data directory, which will make rdiff-backup run more quickly and use up a bit less disk space.

For more about Cron in general, read this article about Cron. or this.

As it stands, the backup will be made once a day, so you can come back tomorrow for the final test. Or, you can temporarily increase the backup frequency to make sure it’s working.

you can verify that they are in place using this command. (Make sure you wait until the backup has actually triggered):

ls -l /home/sammy/redis

Your output should look similar to this:Output

total 20
-rw-rw---- 1 redis redis    70 Sep 14 13:13 dump.rdb
drwx------ 3 root  root  12288 Sep 14 13:49 rdiff-backup-data
-rw-r----- 1 redis redis   119 Sep 14 13:09 redis-staging-ao.aof

You’ll now have daily backups of your Redis data, stored in your home directory on the same server.

Restoring Redis Database from Backup

Now that you’ve seen how to back up a Redis database, this step will show you how to restore your database from a dump.rdb backup file.

Restoring a backup requires you to replace the active Redis database file with the restoration file. Since this is potentially destructive, we recommend restoring to a fresh Redis server if possible.

You wouldn’t want to overwrite your live database with a more problematic restoration. However, renaming rather than deleting the current file minimizes risk even if restoring to the same server.

Stopping Redis

Before we can replace the Redis dump file, we need to stop the currently running instance of Redis. Your database will be offline once you stop Redis.

Renaming Current dump.rdb

Redis reads its contents from the dump.rdb file. Let’s rename the current one, to make way for our restoration file.

sudo mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.old

Note that you can restore dump.rdb.old if you decide the current version was better than your backup file.

If AOF Is Enabled, Turn It Off

AOF tracks every write operation to the Redis database. Since we’re trying to restore from a point-in-time backup, though, we don’t want Redis to recreate the operations stored in its AOF file.

Let’s rename the .aof file to get it out of the way temporarily. This renames every file that ends with .aof, so if you have more than one AOF file you should rename the files individually, and NOT run this command:

sudo mv /var/lib/redis/*.aof /var/lib/redis/appendonly.aof.old

Edit your Redis configuration file to temporarily turn off AOF:

sudo nano /etc/redis/redis.conf

In the AOF section, look for the appendonly directive and change it from yes to no. That disables it:/etc/redis/redis.conf

appendonly no

Restoring the dump.rdb File

Now we’ll use our restoration file, which should be saved at /home/sammy/redis/dump.rdb if you followed the previous steps in this tutorial.

If you are restoring to a new server, now’s the time to upload the file from your backup server to the new server:

scp /home/sammy/redis/dump.rdb sammy@your_new_redis_server_ip:/home/sammy/dump.rdb

Now, on the restoration server, which can be the original Redis server or a new one, you can use cp to copy the file to the /var/lib/redis folder:

sudo cp -p /home/sammy/redis/dump.rdb /var/lib/redis

(If you uploaded the file to /home/sammy/dump.rdb, use the command sudo cp -p /home/sammy/dump.rdb /var/lib/redis instead to copy the file.)

Alternately, if you want to use rdiff-backup, run the command shown below. Note this will only work if you are restoring from the folder you set up with rdiff-backup originally. With rdiff-backup, you have to specify the name of the file in the destination folder:

sudo rdiff-backup -r now /home/sammy/redis/dump.rdb /var/lib/redis/dump.rdb

Starting Redis

Now we need to start the Redis server again.

sudo service redis-server start

Checking Database Contents

Let’s see if the restoration worked.

Log in to Redis:

redis-cli

Check the shapes:triangles entry:

GET x

Great! Our restoration worked. If you’re not using AOF, you’re done! Your restored Redis instance should be back to normal.

(Optional) Enabling AOF

If you want to resume or start using AOF to track all the writes to your database, follow these instructions. The AOF file has to be recreated from the Redis command line.

Log in to Redis:

redis-cli

Turn on AOF:

BGREWRITEAOF

You should get the output:Output

Background append only file rewriting started

Run the info command. This will generate quite a bit of output:

info

Scroll to the Persistence section, and check that the aof entries match what’s shown here. If aof_rewrite_in_progress is 0, then the recreation of the AOF file has completed.Output

# Persistence

. . .

aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:0
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

If it’s confirmed that recreation of the AOF file has completed, you may now exit the Redis command line:

exit

You can list the files in /var/lib/redis again:

ls /var/lib/redis

You should see a live .aof file again, such as appendonly.aof or redis-staging-ao.aof, along with the dump.rdb file and other backup files.

Once that’s confirmed, stop the Redis server:

sudo service redis-server stop

Now, turn on AOF again in the redis.conf file:

sudo nano /etc/redis/redis.conf

Then re-enable AOF by changing the value of appendonly to yes:/etc/redis/redis.conf

appendonly yes

Start Redis:

sudo service redis-server start

If you’d like to verify the contents of the database one more time, just run through the Checking Database Contents section once more.

That’s it! Your restored Redis instance should be back to normal.

Note

You may also want to reference the rdiff-backup documentation’s examples for how to use rdiff-backup effectively: