zoukankan      html  css  js  c++  java
  • 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

     
  • 相关阅读:
    range用法(倒序取值)
    set操作
    map集合 可变对象
    元组
    list操作
    列表推导式、字典推导式、集合推导式
    调用父类方法
    谷歌浏览器真机调试的方法
    css3 flex弹性盒自动铺满写法
    微信小程序保存海报的方法
  • 原文地址:https://www.cnblogs.com/fyy-hhzzj/p/9087609.html
Copyright © 2011-2022 走看看