Centos7 How do I back up and restore Redis data

  • 2020-10-23 21:18:22
  • OfStack

What is a Redis?

Redis is an in-memory key value cache and store (that is, a database) that can also be permanently saved to disk. In this article, you will learn how to back up and restore your redis database on Centos 7.

Backup and restore instructions

By default, Redis data is saved to disk in the rdb file, which is a point-in-time snapshot of the Redis data set. Snapshots are taken at specified intervals, so they are perfect for backups.

1. Data backup

In Centos 7 and other Linux distributions, the Redis database directory defaults to /var/lib/redis. However, if you have changed the redis storage location, you can find it by typing the following command:


[root@renwolecom ~]# find / -name *rdb

Use the redis-ES29en management tool to access the database:


[root@renwolecom ~]# redis-cli

Since most of the data is running in memory, redis only saves it once every 1 period. To get the latest copy, execute the following command:


10.10.204.64:6379> save
OK
(1.02s)

In addition, if Redis has user authentication set, it needs to verify before saving, for example:


10.10.204.64:6379> auth RenwoleQxl5qpKHrh9khuTW
10.10.204.64:6379> save

Then you can make a backup, for example:


[root@renwolecom ~]# cp /var/lib/redis/dump.rdb /apps/redis-backup-20180129

2. Data restoration

Restoring a backup requires you to replace an existing Redis database file with a restore file. To ensure that the original data files are not corrupted, we recommend that you revert to the new Redis server whenever possible.

The Redis database is stopped, and the Redis database is offline once it is stopped.


[root@renwolecom ~]# systemctl stop redis

If you restore to the original Redis server, rename the current data file before restoring:


[root@renwolecom ~]# mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.old
[root@renwolecom ~]# cp -p /apps/redis-backup-20180129/dump.rdb /var/lib/redis/dump.rdb

Set dump. rdb file permissions. The copied data file may not have Redis user and read permissions.


[root@renwolecom ~]# chown redis:redis /var/lib/redis/dump.rdb
[root@renwolecom ~]# chmod 660 /var/lib/redis/dump.rdb

Start the redis


[root@renwolecom ~]# systemctl start redis

There we go! Now you can login to redis to verify your data.

Note:

As required, close AOF, and AOF tracks each write operation to the Redis database. Since we are trying to recover from a point-in-time backup, we do not want Redis to recreate the operation stored in its AOF file.

Whether to open AOF or not can be learned by viewing the file:


[root@renwolecom ~]# ls /var/lib/redis/

If you see a file with the.aof suffix, you have AOF enabled.

Rename the.aof file,


[root@renwolecom ~]# redis-cli
0

If you have more than one.aof file, name it separately.

Edit your Redis profile to temporarily close AOF:


[root@renwolecom ~]# redis-cli
1

If you have any questions during the backup, please feel free to leave a comment. Thank you very much for your support!


Related articles: