Redis-備份與恢復(fù)

RDB(Redis DataBase)

在指定的時間間隔內(nèi)將內(nèi)存中的數(shù)據(jù)集快照寫入磁盤,可以理解為Snapshot快照俭尖,它恢復(fù)時是將快照文件直接讀到內(nèi)存里哑了。

Redis會單獨創(chuàng)建(fork)一個子進程來進行持久化,會先將數(shù)據(jù)寫入到一個臨時文件中缰泡,待持久化過程都結(jié)束了定枷,再用這個臨時文件替換上次持久化好的文件。整個過程中簸州,主進程是不進行任何IO操作的拆火,這就確保了極高的性能。

如果需要進行大規(guī)模數(shù)據(jù)的恢復(fù)宜猜,且對于數(shù)據(jù)恢復(fù)的完整性不是非常敏感泼返,那RDB方式要比AOF方式更加的高效。RDB的缺點是最后一次持久化后的數(shù)據(jù)可能丟失姨拥。

默認配置

../redis.conf

#滿足以下三者之一就會觸發(fā)rdb備份
900s內(nèi)有1次操作
300s內(nèi)有10次操作
60s內(nèi)有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

關(guān)閉

1.修改配置文件绅喉,重新啟動redis

save ""

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

2.動態(tài)關(guān)閉

redis-cli config set save ""

相關(guān)命令

#修復(fù)損壞的rdb備份文件
redis-check-rdb --fix <rdb備份文件>

優(yōu)勢

1.適合大規(guī)模數(shù)據(jù)恢復(fù)

劣勢

1.最后一次時間間隔內(nèi)保存的數(shù)據(jù)可能丟失,數(shù)據(jù)完整性和一致性低

2.內(nèi)存中的數(shù)據(jù)被克隆一份叫乌,磁盤空間占用大


AOF

以日志的形式來記錄每個寫操作柴罐,將Redis執(zhí)行過的所有寫指令記錄下來(讀操作不記錄),只許追加文件但不可以改寫文件憨奸,redis啟動之初會讀取該文件重新構(gòu)建數(shù)據(jù)革屠,換言之,redis重啟的話就根據(jù)日志文件的內(nèi)容將寫指令從前到后執(zhí)行一次以完成數(shù)據(jù)的恢復(fù)工作。

默認配置

#默認不開啟aof備份
appendonly no

#aof備份文件名
appendfilename "appendonly.aof"

#同步策略:每秒同步(其它選項:每次同步似芝、用不同步)
# appendfsync always
appendfsync everysec
# appendfsync no

#aof文件為上次rewrite后文件大小的一倍那婉,并且大小超過64m時rewrite 
#建議:生產(chǎn)中64mb太小,建議設(shè)為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

相關(guān)命令

#修復(fù)損壞的aof文件
redis-check-aof --fix <aof文件>

rewrite

AOF采用文件追加方式党瓮,文件會越來越大為避免出現(xiàn)此種情況详炬,新增了重寫機制,當(dāng)AOF文件的大小超過所設(shè)定的閾值時,Redis就會啟動AOF文件的內(nèi)容壓縮寞奸,只保留可以恢復(fù)數(shù)據(jù)的最小指令集.可以使用命令bgrewriteaof呛谜。

AOF文件持續(xù)增長而過大時,會fork出一條新進程來將文件重寫(也是先寫臨時文件最后再rename)蝇闭,遍歷新進程的內(nèi)存中數(shù)據(jù)呻率,每條記錄有一條的Set語句。重寫aof文件的操作呻引,并沒有讀取舊的aof文件礼仗,而是將整個內(nèi)存中的數(shù)據(jù)庫內(nèi)容用命令的方式重寫了一個新的aof文件,這點和快照有點類似逻悠。

Redis會記錄上次重寫時的AOF大小元践,默認配置是當(dāng)AOF文件大小是上次rewrite后大小的一倍且文件大于64M時觸發(fā)。

優(yōu)劣

1.appendfsync alway (同步持久化)
每次發(fā)生數(shù)據(jù)變更會被立即記錄到磁盤 性能較差但數(shù)據(jù)完整性比較好

2.appendfsync everysec (每秒同步)
異步操作童谒,每秒記錄 如果一秒內(nèi)宕機单旁,有數(shù)據(jù)丟失

3.appendfsync no (從不同步)

總結(jié)

  • 相同數(shù)據(jù)集的數(shù)據(jù)而言aof文件要遠大于rdb文件,恢復(fù)速度慢于rdb
  • aof運行效率要慢于rdb,每秒同步策略效率較好饥伊,不同步效率和rdb相同象浑。
  • aof備份與rdb備份可以同時開啟,但redis優(yōu)先使用aof恢復(fù)琅豆。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末愉豺,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子茫因,更是在濱河造成了極大的恐慌蚪拦,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,029評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冻押,死亡現(xiàn)場離奇詭異驰贷,居然都是意外死亡,警方通過查閱死者的電腦和手機洛巢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評論 3 385
  • 文/潘曉璐 我一進店門括袒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人稿茉,你說我怎么就攤上這事箱熬±嗫眩” “怎么了?”我有些...
    開封第一講書人閱讀 157,570評論 0 348
  • 文/不壞的土叔 我叫張陵城须,是天一觀的道長。 經(jīng)常有香客問我米苹,道長糕伐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,535評論 1 284
  • 正文 為了忘掉前任蘸嘶,我火速辦了婚禮良瞧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘训唱。我一直安慰自己褥蚯,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,650評論 6 386
  • 文/花漫 我一把揭開白布况增。 她就那樣靜靜地躺著赞庶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪澳骤。 梳的紋絲不亂的頭發(fā)上歧强,一...
    開封第一講書人閱讀 49,850評論 1 290
  • 那天,我揣著相機與錄音为肮,去河邊找鬼摊册。 笑死,一個胖子當(dāng)著我的面吹牛颊艳,可吹牛的內(nèi)容都是我干的茅特。 我是一名探鬼主播,決...
    沈念sama閱讀 39,006評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼棋枕,長吁一口氣:“原來是場噩夢啊……” “哼白修!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起戒悠,我...
    開封第一講書人閱讀 37,747評論 0 268
  • 序言:老撾萬榮一對情侶失蹤熬荆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后绸狐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卤恳,經(jīng)...
    沈念sama閱讀 44,207評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,536評論 2 327
  • 正文 我和宋清朗相戀三年寒矿,在試婚紗的時候發(fā)現(xiàn)自己被綠了突琳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,683評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡符相,死狀恐怖拆融,靈堂內(nèi)的尸體忽然破棺而出蠢琳,到底是詐尸還是另有隱情,我是刑警寧澤镜豹,帶...
    沈念sama閱讀 34,342評論 4 330
  • 正文 年R本政府宣布傲须,位于F島的核電站,受9級特大地震影響趟脂,放射性物質(zhì)發(fā)生泄漏泰讽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,964評論 3 315
  • 文/蒙蒙 一昔期、第九天 我趴在偏房一處隱蔽的房頂上張望已卸。 院中可真熱鬧,春花似錦硼一、人聲如沸累澡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽愧哟。三九已至,卻和暖如春具伍,著一層夾襖步出監(jiān)牢的瞬間翅雏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評論 1 266
  • 我被黑心中介騙來泰國打工人芽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留望几,地道東北人。 一個月前我還...
    沈念sama閱讀 46,401評論 2 360
  • 正文 我出身青樓萤厅,卻偏偏與公主長得像橄抹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子惕味,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,566評論 2 349