实现方式1:利用redis主从复制特性

在新的一台服务器上安装同版本的redis ,将配置文件同步到新的服务器。增加下列信息

replicaof 192.168.31.216 6379

然后使用客户端工具查看keys是否同步完成
最后将备份服务redis服务器设置成主服务器
命令

//链接客户端
redis-cli 
//查看当前主从信息
 info replication
 //设置当前从服务器为主服务器
 slaveof no one
//查看设置成功后的服务器主从信息
 info replication

出现下图说明,服务器设置成功
在这里插入图片描述

实现方式2 使用持久化文件恢复

redis支持两种方式的持久化,一种是快照方式(snapshotting),也称RDB方式;两一种是追加文件方式(append-only file),也称AOF方式。RDB方式是redis默认的持久化方式。

RDB方式
RDB方式是将内存中的数据的快照以二进制的方式写入名字为 dump.rdb的文件中。我们对 Redis 进行设置, 让它根据设置周期性自动保存数据集。修改redis.conf文件,如下

######################### SNAPSHOTTING ################################

#Save the DB on disk:

#In the example below the behaviour will be to save:

#after 900 sec (15 min) if at least 1 key changed

#after 300 sec (5 min) if at least 10 keys changed

#after 60 sec if at least 10000 keys changed

#Note: you can disable saving completely by commenting out all “save” lines.

#It is also possible to remove all the previously configured save
#points by adding a save directive with a single empty string argument
#llike in the following example:
#save “”

#900秒内如果有超过1个key被修改则发起保存快照

save 900 1

#300秒内如果有超过10个key被修改则发起保存快照

save 300 10

#60秒内如果有超过1000个key被修改则发起保存快照

save 60 10000
dump.rdb文件默认生成在%REDIS_HOME%etc目录下(如/usr/local/redis/etc/),可以修改redis.conf文件中的dir指定dump.rdb的保存路径
#The working directory.
#The DB will be written inside this directory, with the filename specified
#above using the ‘dbfilename’ configuration directive.
#The Append Only File will also be created inside this directory.
#Note that you must specify a directory here, not a file name.

dir ./

AOF方式
RDB方式是周期性的持久化数据, 如果未到持久化时间点,Redis 因为某些原因而造成故障停机, 那么服务器将丢失最近写入、且仍未保存到快照中的那些数据。所以从redis 1.1开始引入了AOF方式,AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集。 AOF 文件中的命令全部以 Redis 协议的格式来保存,新命令会被追加到文件的末尾。

AOF方式仍然有丢失数据的可能,因为收到写命令后可能并不会马上将写命令写入磁盘,因此我们可以修改redis.conf,配置redis调用write函数写入命令到文件中的时机。如下

#######################APPEND ONLY MODE #############################

AOF and RDB persistence can be enabled at the same time without problems.

If the AOF is enabled on startup Redis will load the AOF, that is the file

with the better durability guarantees.

Please check http://redis.io/topics/persistence for more information.

#启用AOF方式

appendonly yes

#每次有新命令追加到 AOF 文件时就执行一次 fsync :非常慢,也非常安全

appendfsync always

#每秒 fsync 一次:足够快(和使用 RDB 持久化差不多),并且在故障时只会丢失 1 秒钟的数据

appendfsync everysec

#从不 fsync :将数据交给操作系统来处理。更快,也更不安全的选择

appendfsync no

从上面三种AOF持久化时机来看,为了保证不丢失数据,appendfsync always是最安全

  1. Redis 恢复的机制
    如果只配置 AOF ,重启时加载 AOF 文件恢复数据;
    如果同时配置了 RDB 和 AOF ,启动是只加载 AOF 文件恢复数据;
    如果只配置 RDB,启动是将加载 dump 文件恢复数据。
    2. 从 aof 中恢复数据
    1 注意以下配置

appendonly yes
dir /home/redis/data_6379/

2 拷贝 AOF 文件到 Redis 的数据目录
cp appendonly.aof /home/redis/data_6379/
3 启动 redis-server
redis-server redis_6379.conf

3. 从 RDB 文件恢复数据

1 注意以下配置
appendonly no
dir /home/redis/data_6379/

2 拷贝 RDB 文件到 Redis 的数据目录

cp dump.db /home/redis/data_6379/

3 启动 redis-server
redis-server redis_6379.conf

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐