This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

About the author: Wu Kong, 8 years of experience in first-line Internet development and architecture, explains distribution, architecture design, Java core technology with stories. “JVM performance optimization combat” column author, open source “Spring Cloud combat PassJava” project, public number: Wukong chat architecture. This article is available at www.passjava.cn

Redis RDB persistence scheme, I believe you are familiar with, but for enterprises, if only a persistent RDB file, not enough to deal with production level accidents. The usual solution is to make multiple backups of the RDB. Today, we will take you to live operation RDB cold backup, and data recovery through the RDB.

Enterprise-level cold backup solution

Redis RDB persistence is very suitable for enterprise-class cold backup solutions, where the cold backup can be understood as copying the generated files to other machines or cloud servers.

The RDB is suitable for cold backup for the following reasons:

  • After RDB files are generated, they change infrequently, unless frequent checkpoints are triggered to cause regeneration.
  • RDB is a Redis memory snapshot and is faster than AOF log recovery.
  • You can configure multiple RDB generation policies based on the system usage scenario and actual situation.

A backup plan

1. Run the crontab command delivered with Linux to execute scheduled tasks and invoke the data backup script.

2. Back up the latest RDB snapshot files to the specified directory every hour. Only the backups in the latest 48 hours are retained.

3. Back up the latest RDB snapshot file to the specified directory every day. Only the backup of the latest month is retained.

4. Send the backup files to the remote cloud server every night.

The flowchart is as follows:

Hourly backup

First, you need to write a script to do the data backup. The command to create the script is as follows:

mkdir /usr/local/redis
mkdir /usr/local/redis/copy
vi /usr/local/redis/copy/redis_rdb_copy_hourly.sh
mkdir /usr/local/redis/snapshotting
chmod 777 /usr/local/redis
Copy the code

Then write this script file:

#! /bin/sh

cur_date=`date +%Y%m%d%H`
rm -rf /usr/local/redis/snapshotting/$cur_date
mkdir /usr/local/redis/snapshotting/$cur_date
cp /var/redis/6379/dump.rdb /usr/local/redis/snapshotting/$cur_date

del_date=`date -d -48hour +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$del_date
Copy the code

Script explanation:

  • Cur_data represents the current time, accurate to the hour, such as 2021080616.
  • Example Delete the snapshot file in the current hour.
  • Create an empty backup file for the current hour.
  • Copy the current snapshot file to the empty backup file created in the previous step.
  • Del_date indicates the time 48 hours ago, accurate to the hour, such as 2021080416.
  • Delete files backed up 48 hours ago.

Set scheduled tasks to run scripts at 0 minutes per hour:

crontab -e
0 * * * * sh /usr/local.redis/copy/redis_rdb_copy_hourly.sh
Copy the code

Since we have to wait until 0 in the next hour, we run the script tests manually:

cd /usr/local/redis/copy
./redis_rdb_copy_hourly.sh 
Copy the code

A directory will be created in the snapshotting folder: 2021080809, indicating that this is the backup folder at 2021-08-0809 (note that this time is UTC). There will also be a dump.rdb file in this directory. As shown in the figure below:

Daily backup

Similarly to the hourly backup, create a script that backs up once a day:

vi /usr/local/redis/copy/redis_rdb_copy_daily.sh
chomd 777 *
Copy the code

Write a script:

#! /bin/sh

cur_date=`date +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$cur_date
mkdir /usr/local/redis/snapshotting/$cur_date
cp /var/redis/6379/dump.rdb /usr/local/redis/snapshotting/$cur_date

del_date=`date -d -1month +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$del_date
Copy the code

Create a scheduled task that backs up data once a day:

crontab -e

0 0 * * * sh /usr/local/redis/copy/redis_rdb_copy_daily.sh
Copy the code

Manually executing a backup script:

cd /usr/local/redis/copy
./redis_rdb_copy_daily.sh 
Copy the code

A directory will be created in the Snapshotting folder: 20210808, indicating that this is today’s backup folder at 2021-08-08 (note that this time is UTC). There will also be a dump.rdb file in this directory. As shown in the figure below:

These backup suggestions are uploaded to the cloud server.

Restore data from the backup file

Suppose a scenario: a program that went online a few hours ago contaminated all the Redis data, and the data was wrong. What should we do?

You can select a backup file from an earlier point in time for restoration.

Recovery process

  • Stop Redis and temporarily disable the persistence configuration of AOF.
  • Delete AOF log files and RDB snapshot files.
  • Copy the RDB snapshot file to the RDB file loading directory in Redis.
  • Restart Redis to confirm that data recovery is successful.
  • Hot modifies Redis’ AOF persistence configuration. Redis writes data from memory to AOF files.
  • Stop Redis again, manually modify the configuration file, and enable AOF persistence to prevent hot changes from taking effect.
  • Restart Redis again.

References:

www.runoob.com/linux/linux…