Pivotal Knowledge Base

Follow

How to Backup and Restore Open Source Redis

Environment

Open Source Software (OSS) Redis

Purpose

This procedure describes how to backup and restore OSS Redis.

Procedure

Backup

  1. Backing up Redis rdb file is simply copy .rdb to your backup location. So, we have to choose any safe destination that we'd like to store the snapshot file, (it is usually safer to transfer to a different server or disk).
  2. The first step is to figure out where the current RDB file is located
    cat /etc/redis/redis.conf |grep '^dir '|cut -d' ' -f2
  3. In my env, /var/lib/redis is the directory where the snapshot file is dumped to. Before copying, it is a good idea to save the DB in the background.
    redis-cli bgsave
  4. Once dump is completed copy the dump.rdb file to somewhere that is safe. (The path /backup/redis is just an illustration of where it could be copied to)
    cp /var/lib/redis/dump.rdb /backup/redis/dump.$(date +%Y%m%d%H%M).rdb
  5. That's it. Your backup is complete.

Restore

Before starting the restore process determine if aof is enabled. 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. Run the following check to see if it is in AOF mode: 'yes' means AOF mode is enable, 'no' means AOF mode is disable. 

cat /etc/redis/redis.conf |grep 'appendonly '|cut -d' ' -f2

AOF = no:

  1. In case it is not AOF mode. ('no'), the process is quite straightforward. First, just stop Redis server (new Redis):
    /etc/init.d/redis-server stop
  2. Then, remove the current dumb.rdb file (if there is one) or rename it to dump.rdb.old
    mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.back
  3. After confirming there is no dump.rdb file name in the folder, copy the good backup you took earlier into the /var/lib/redis folder (or which folder Redis is using in your env).
    cp /backup/redis/dump.xxxxxx.rdb /var/lib/redis/dump.rdb
  4. Remember to set it to be owned by Redis user/group
    chown redis:redis dump.rdb
  5. Now, just start the Redis server.
    /etc/init.d/redis-server start
  6. Restore is completed.

AOF = yes:

  1. The first thing to do is the same as previous case, just stop the Redis server:
    /etc/init.d/redis-server stop
  2. Go inside directory where dump.rdb is located:
    cd /var/lib/redis/
  3. Then, remove the current dumb.rdb file (if there is one) or rename it to dump.rdb.old:
    mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.old
    mv /var/lib/redis/appendonly.aof /var/lib/redis/appendonly.aof.old
  4. Copy the good backup file in and correct its permission:
    cp /backup/redis/dump.xxxxxx.rdb /var/lib/redis/dump.rdb
    chown redis:redis /var/lib/redis/dump.rdb
  5. Next the important part is to disable AOF by editing /etc/redis/redis.conf file, set appendonly as "no":
  6. Verify appendonlu is set to "no":

    cat /etc/redis/redis.conf |grep 'appendonly '|cut -d' ' -f2
    
  7. Next start the Redis server and run the following command to create new appendonly.aof file:

    /etc/init.d/redis-server start
    redis-cli bgrewriteaof
  8. Check the progress (0 - done, 1 - not yet):
    redis-cli info | grep aof_rewrite_in_progress
    You should see a new appendonly.aof file.
  9. Next, stop the server:
    /etc/init.d/redis-server stop
  10. After it finished, enable AOF again by changing appendonly in /etc/redis/redis.conf file to yes
  11. Then start the Redis server again:
    /etc/init.d/redis-server start
  12. The Restore is completed.

Comments

Powered by Zendesk