How to back up and restore Redis data by Centos7
What is Redis?
Redis is an in-memory cache and store of keys (i.e., a database) that can also be permanently saved to disk. In this article, you will learn how to backup and restore your redis database on Centos 7.
Backup Restore Instructions
By default, Redis data is saved to disk in an.rdb file, which is a point-in-time snapshot of the Redis dataset. 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 change the redis storage location, you can find it by typing:
[root@renwolecom ~]# find / -name *rdb
Use the redis-cli administration tool to access the database:
[root@renwolecom ~]# redis-cli
Since most of the data runs in memory, redis saves only once in a while. To get an up-to-date copy, execute the following command:
10.10.204.64:6379> saveOK(1.02s)
In addition, if Redis sets user authentication, it needs to be verified first and then saved, for example:
10.10.204.64:6379> auth RenwoleQxl5qpKHrh9khuTW10.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 Recovery
Restoring a backup requires you to replace the existing Redis database file with the recovery file. To ensure that the original data files are not corrupted, we recommend restoring to a new Redis server as much as possible.
Stop Redis database, once stopped, Redis database is offline.
[root@renwolecom ~]# systemctl stop redis
If restoring 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 permissions for dump.rdb file. The copied data file may not have Redis user and read permissions. You need to manually grant:
[root@renwolecom ~]# chown redis:redis /var/lib/redis/dump.rdb[root@renwolecom ~]# chmod 660 /var/lib/redis/dump.rdb
Start redis
[root@renwolecom ~]# systemctl start redis
It's done! You can now log in to redis to verify your data.
Note:
Depending on the requirements, turn off AOF, which tracks each write to the Redis database. Since we are trying to recover from a point-in-time backup, we do not want Redis to recreate operations stored in its AOF files.
Whether to open AOF can be learned by looking at the file:
[root@renwolecom ~]# ls /var/lib/redis/
If you see files with the suffix.aof, you have AOF enabled.
Rename the.aof file,
[root@renwolecom ~]# mv /var/lib/redis/*.aof /var/lib/redis/appendonly.aof.old
If there are multiple.aof files, name them individually.
Edit your Redis profile Turn off AOF temporarily:
[root@renwolecom ~]# vim /etc/redis/redis.confappendonly no
If you have any questions during backup, please leave a message. Thank you very much for your support!