Guide to Backup and Restore Redis Database and Automate

Category : Redis

Hello,
Today we will show how we can backup and restore Redis database and we will also show how we can automate the process of taking backup. Cronjob will be created for automation of the backup of the database on the machine locally.

Requirements

  • Redis-CLI installed on Linux Machine
  • Redis Server Running
  • Root Access to Machine
  • Redis Authentication Password if its set. Get it from here on your machine.
    Redis configuration file – /etc/redis/redis.conf

Procedure

Backup

  1. Locate you redis database directory using

    locate *rdb


    or
    start Redis-CLI

    redis-cli auth master_password
    config get dir

    Note – Use auth master_password only if you have set it for your redis-server else, skip that command.

    Note –  auth master_password is used to authenticate the redis cli session, you can skip if you are not using any master password

  2. Save the progress at current stage using save command in redis-cli

    save

  3. Navigate to your database directory that is shown above in #1.  For us its /home/zapbot/

    cd /home/zapbot

  4. In your database directory, you can see dump.rdb file and if you have enabled ‘appendonly’ then you can see appendonly.aof file along with the database. So these two files have to be backed up. In our case, we don’t have aof file but commands to backup are same as rdb file.
  5. Run this command to store the backup

    sudo cp /home/zapbot/dump.rdb /home/backup/

    Note – Location may vary from user to user because it depends on the configuration done by the user.

  6. Your Redis database is backed up in /home/backup/ directory.

Automation of Backup

We will now configure automatic backup of Redis Database using cronjob which will run at specific intervals automatically. There are many automatic backup tools available but we will use modern tool rdiff-backup. Here we start.

  1. Install rdiff-backup on your machine using

    sudo yum install rdiff-backup

  2. Verify rdiff-backup installed properly

    rdiff-backup –version

  3. To test, runun this command which similarly backup up the .rdb and .aof file itself to selected directory

    sudo rdiff-backup –preserve-numerical-ids /home/zapbot /home/backup/redis/

    Note –
    –preserve-numerical-ids, the ownership of the source and destination folders will be the same.
    In above command we are using /home/backup/redis as backup directory, only this is different from cp command.

  4. Now its time for creating cronjob for automatic backup using rdiff-backup. Run,

    sudo crontab -e

    Paste the following there and save it

    0 0 * * * rdiff-backup –preserve-numerical-ids –no-file-statistics /home/zapbot /home/backup/redis/

    Note –
    This Cron entry will perform a Redis backup every day at midnight.
    –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.

  5. Backup will be saved to your desired directory and you can check it using

    ls -l /home/backup/redis

Restore

  1. Stop Redis Server

    sudo service redis-server stop

    or

    sudo killall -9 redis-server

  2. Turn off appendonly if its enabled in /etc/redis/redis.conf

    appendonly no

  3. Copy Backup Database to working dir

    sudo cp -p /home/backup/redis/dump.rdb /home/zapbot/redis/

    Note – /home/backup/redis/dump.rdb is our redis database that we created and /home/zapbot/redis/ is our redis database working directory

  4. You can also use rdiff-backup to restore the backup instead above cp command, Run as below

    sudo rdiff-backup -r now /home/backup/redis/dump.rdb /home/zapbot/redis/

  5. Set permissions to the database if your directory is /var/lib/redis/ , we will skip chown command because we are using local directory for database

    sudo chown redis:redis /var/lib/redis/dump.rdb
    sudo chmod 660 /var/lib/redis/dump.rdb

  6. Start Redis Server to resume your work

    sudo service redis-server start

  7. If you need to enable appendonly then log in to redis-cli and running following command,
    BGREWRITEAOF
  8. Stop Redis Server by repeating #1
  9. Turn on appendonly if its enabled in /etc/redis/redis.conf

    appendonly yes

  10. Start Redis Server to resume your work

    sudo service redis-server start

So herenow we have finished this brief guide to backup manually and automated alongwith the restoration process.