Backup and Restore Redis

In this article we explain, how to Backup and Restore Redis on Kubernetes cluster.

Restore Redis

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).

The first step is to figure out where the current RDB file is located:

1
cat /etc/redis/redis.conf |grep '^dir '|cut -d' ' -f2

 

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

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)

1
cp /var/lib/redis/dump.rdb /backup/redis/dump.$(date +%Y%m%d%H%M).rdb

 

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.

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

 

AOF = yes:

The first thing to do is remove redis deployment from kubernetes server:

1
kubectl delete -f ./redis.yaml

 

Attach to the redis-persistent storage on mounted file system it can be GlusterFS – Volume, Azure Storage – File Share, min.io S3 bucket

Then, remove the current dumb.rdb file (if there is one) or rename it to dump.rdb.old:

Copy the good backup dump.rdb file in and correct its permission:

1
2
chown 999:999 dump.rdb
chmod 644 dump.rdb

 

Next the important part is to disable AOF by editing redis.yaml file, set appendonly as “no”:
Verify appendonly is set to “no”:

1
2
3
4
5
containers:
  - name: redis
    image: redis:5.0.4
    imagePullPolicy: Always
    args: ["--requirepass", "$(redis_pass)", "--appendonly", "no", "--save", "900", "1", "--save", "30", "1"]

 

Next create the Redis deployment on kubernetes and run the following command to create new appendonly.aof file:

1
kubectl apply-f ./redis.yaml

 

1
kubectl exec redis-0 -- redis-cli -a bgrewriteaof

 

Check the progress (0 – done, 1 – not yet), and if exists new appendonly.aof file on the same size like dump.rdb.

1
kubectl exec redis-0 -- redis-cli -a info | grep aof_rewrite_in_progress

 

You should see a new appendonly.aof file. Next, recreate redis server:
After it finished, enable AOF again by changing redis.yaml file to yes

1
2
3
4
5
containers:
  - name: redis
    image: redis:5.0.4
    imagePullPolicy: Always
    args: ["--requirepass", "$(redis_pass)", "--appendonly", "yes", "--save", "900", "1", "--save", "30", "1"]

 

Then recreate the Redis server again:

1
2
kubectl delete-f ./redis.yaml
kubectl apply-f ./redis.yaml

 

The Restore is completed.