RDB(Redis DataBase)

在指定的时间间隔内将内存中的数据集快照写入磁盘,可以理解为Snapshot快照,它恢复时是将快照文件直接读到内存里。

Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。

如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失。

默认配置

../redis.conf

#满足以下三者之一就会触发rdb备份
900s内有1次操作
300s内有10次操作
60s内有10000次操作 #工作目录
/var/lib/redis #备份文件
dump.rdb

详细配置

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# 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
# like in the following example:
#
# save "" save 900 1
save 300 10
save 60 10000 # By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes # Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes # The filename where to dump the DB
dbfilename 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 /var/lib/redis

关闭

1.修改配置文件,重新启动redis

save ""

#save 900 1
#save 300 10
#save 60 10000

2.动态关闭

redis-cli config set save ""

相关命令

#修复损坏的rdb备份文件
redis-check-rdb --fix <rdb备份文件>

优势

1.适合大规模数据恢复

劣势

1.最后一次时间间隔内保存的数据可能丢失,数据完整性和一致性低

2.内存中的数据被克隆一份,磁盘空间占用大


AOF

以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可以改写文件,redis启动之初会读取该文件重新构建数据,换言之,redis重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。

默认配置

#默认不开启aof备份
appendonly no #aof备份文件名
appendfilename "appendonly.aof" #同步策略:每秒同步(其它选项:每次同步、用不同步)
# appendfsync always
appendfsync everysec
# appendfsync no #aof文件为上次rewrite后文件大小的一倍,并且大小超过64m时rewrite
#建议:生产中64mb太小,建议设为3G-5G
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

详细配置

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

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# 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. appendonly no # The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof" # The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise. # The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec". # appendfsync always
appendfsync everysec
# appendfsync no # When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
#
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability. no-appendfsync-on-rewrite no # Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature. auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb # An AOF file may be found to be truncated at the end during the Redis
# startup process, when the AOF data gets loaded back into memory.
# This may happen when the system where Redis is running
# crashes, especially when an ext4 filesystem is mounted without the
# data=ordered option (however this can't happen when Redis itself
# crashes or aborts but the operating system still works correctly).
#
# Redis can either exit with an error when this happens, or load as much
# data as possible (the default now) and start if the AOF file is found
# to be truncated at the end. The following option controls this behavior.
#
# If aof-load-truncated is set to yes, a truncated AOF file is loaded and
# the Redis server starts emitting a log to inform the user of the event.
# Otherwise if the option is set to no, the server aborts with an error
# and refuses to start. When the option is set to no, the user requires
# to fix the AOF file using the "redis-check-aof" utility before to restart
# the server.
#
# Note that if the AOF file will be found to be corrupted in the middle
# the server will still exit with an error. This option only applies when
# Redis will try to read more data from the AOF file but not enough bytes
# will be found.
aof-load-truncated yes

相关命令

#修复损坏的aof文件
redis-check-aof --fix <aof文件>

rewrite

AOF采用文件追加方式,文件会越来越大为避免出现此种情况,新增了重写机制,当AOF文件的大小超过所设定的阈值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集.可以使用命令bgrewriteaof。

AOF文件持续增长而过大时,会fork出一条新进程来将文件重写(也是先写临时文件最后再rename),遍历新进程的内存中数据,每条记录有一条的Set语句。重写aof文件的操作,并没有读取旧的aof文件,而是将整个内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。

Redis会记录上次重写时的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发。

优劣

1.appendfsync alway (同步持久化)

每次发生数据变更会被立即记录到磁盘 性能较差但数据完整性比较好

2.appendfsync everysec (每秒同步)

异步操作,每秒记录 如果一秒内宕机,有数据丢失

3.appendfsync no (从不同步)

总结

  • 相同数据集的数据而言aof文件要远大于rdb文件,恢复速度慢于rdb
  • aof运行效率要慢于rdb,每秒同步策略效率较好,不同步效率和rdb相同。
  • aof备份与rdb备份可以同时开启,但redis优先使用aof恢复。

Redis-04.备份与恢复的更多相关文章

  1. Redis 数据备份与恢复,安全,性能测试,客户端连接,管道技术,分区(四)

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

  2. redis开启持久化、redis 数据备份与恢复

    redis持久化介绍  https://segmentfault.com/a/1190000015897415 1. 开启aof持久化.以守护进程启动.远程访问先把配置文件拷贝一份到/etc/redi ...

  3. Redis 数据备份与恢复

    Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 redis 127.0.0.1: ...

  4. redis数据备份与恢复

    1.启动redis 进入redis目录 redis-cli 2.数据备份 redis 127.0.0.1:6379> SAVE 该命令将在 redis 备份目录中创建dump.rdb文件. 3. ...

  5. 一、Redis数据备份与恢复

    Redis里的数据都是保存在内存中,关闭服务器必须进行数据备份. 1.Redis的数据持久化 bgsave做镜像全量持久化,AOF做增量持久化. bgsave的原理:fork和cow(copy on  ...

  6. 8.Redis 数据备份与恢复

    转自:http://www.runoob.com/redis/redis-tutorial.html Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下 ...

  7. Redis—数据备份与恢复

    https://www.cnblogs.com/shizhengwen/p/9283973.html https://blog.csdn.net/w2393040183/article/details ...

  8. Redis的备份与恢复

    备份 dump.rdb:RDB方式的备份文件 appendonly.aof:AOF方式的备份文件 rdb 备份处理 # 编辑redis.conf文件,找到如下参数,默认开启. save 900 1 s ...

  9. Redis 04 列表

    参考源 https://www.bilibili.com/video/BV1S54y1R7SB?spm_id_from=333.999.0.0 版本 本文章基于 Redis 6.2.6 在 Redis ...

  10. Redis之数据备份与恢复

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

随机推荐

  1. PowerScript语句

    赋值语句 赋值语句可以把一个表达式的结果或者变量和常量的值,赋给一个变量或者对象的属性或成员变量.赋值语句的格式是: variablename = expression 其中variablename代 ...

  2. ubuntu 下安装pip3

    在使用任何apt 安装任何软件包之前,建议用以下命令更新软件 sudo apt update 更新好了后可能会出现 apt list --upgradable 安装pip3 sudo apt inst ...

  3. break 和continue在循环中起到的作用

    break语句的作用是终止当前循环,跳出循环体.主意,break只能跳出一层循环. continue语句的作用是终止本轮循环并开始下一轮循环,(这里要主意的是在开始下一轮循环之前,会先测试循环条件). ...

  4. eclipse java tomcat 远程调试

    在远程linux上修改tomcat 中bin 文件夹下 修改catalina.sh文件,在最前面加上如下代码: CATALINA_OPTS="-Xdebug -Xrunjdwp:transp ...

  5. scrapy 爬取智联招聘

    准备工作 1. scrapy startproject Jobs 2. cd Jobs 3. scrapy genspider ZhaopinSpider www.zhaopin.com 4. scr ...

  6. token回话保持,axios请求拦截和导航守卫以及token过期处理

    1:了解token:有时候大家又说token令牌.整个机制是前端第一次登陆发送请求,后端会根据前端的用户名和密码, 通过一些列的算法的到一个token令牌, 这个令牌是独一无二的,前端每次发送请求都需 ...

  7. [精华][推荐]CAS SSO单点登录服务端客户端学习

    1.通过下载稳定版本的方式下载cas的相关源码包,如下: 直接选择4.2.1的稳定代码即可 2.我们项目中的版本版本使用maven apereo远程库去下载 通过远程maven库下载cas-serve ...

  8. linux学习第十七天 (Linux就该这么学)

    今天12月14日学习比较少点,等了一会,主要讲了squid代理,1,正向代理 2反向代理 正向代表分为:标准的正向代理,透明的正向代理 ,这个比较实用, 还讲了RHCE考试的中的内容  iscsi 是 ...

  9. bittorrent 学习(四) tracker peer通讯

    看看 tracker.c文件 http_encode() 为http发送进行编码转换 int http_encode(unsigned char *in,int len1,char *out,int ...

  10. JAVA实训第三次作业

    编写"学生"类及其测试类. 5.1 "学生"类: 类名:Student 属性:姓名.性别.年龄.学号.5门课程的成绩 方法1:在控制台输出各个属性的值. 方法2 ...