redis怎么將rdb文件和aof文件還原數據庫


1   AOF ----->      appendonly  yes     2  cp   appendonly.aof 到redis的數據庫目錄也就是配置文件里面的dir關鍵字   3  appendfilename 重啟服務

2  RDB  ----->      appendonly   no     2 cp    dump.rdb到redis數據庫目錄也就是配置文件里面的dir關鍵字   3  重啟服務

另外可以參考以下這篇文章   http://www.cnblogs.com/zhangchao-letv/articles/6123313.html

前面我們已經說過,既可以把redis理解為緩存技術,也可以理解為數據庫,因為redis支持將內存中的數據周期性的寫入磁盤或者把操作追加到記錄文件中,這個過程稱為redis的持久化。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

#   like 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 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 ./

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

使用 redis-port 工具將自建 redis 的 rdb文件同步到雲數據庫

下載 redis-port

redis-port地址

使用示例

  1. ./redis-port restore --input=x/dump.rdb --target=dst_host:dst_port --auth=dst_password [--filterkey="str1|str2|str3"] [--targetdb=DB] [--rewrite] [--bigkeysize=SIZE] [--logfile=REDISPORT.LOG]

參數說明

  • x/dump.rdb : 自建 redis 的 dump 文件路徑

  • dst_host : 雲數據庫 redis 域名

  • dst_port : 雲數據庫 redis 端口

  • dst_password : 雲數據庫 redis 密碼

  • str1|str2|str3 : 過濾具有 str1 或 str2 或 str3 的 key

  • DB : 將要同步入雲數據庫 redis 的 DB

  • rewrite : 覆蓋已經寫入的 key

  • bigkeysize=SIZE : 當寫入的 value 大於 SIZE 時,走大 key 寫入模式

根據 redis-port 日志查看數據同步狀態

根據redis-port日志查看同步狀態

當出現restore: rdb done時數據同步完成。

參考文章: 

https://help.aliyun.com/document_detail/66008.html

http://www.cnblogs.com/hjwublog/p/5660578.html#autoid-4-1-0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM