redis數據備份(持久化)

redis數據備份(持久化)

1,簡介:

redis持久化主要有以下兩種方式來進行
    RDB: 數據按照配置定期快照方式保存,默認文件名爲dump.rdb       redis重啓自動加載
    AOF: 操作命令追加日誌的方式來保存,默認文件名appendonly.aof   redis重啓自動加載

    config get dir  查找redis安裝目錄,也就是dump.rdb的目錄。
    redis重啓時,dump.rdb文件放在redis安裝目錄,則redis會自動加載數據(默認爲安裝目錄,地址可配置)

2,RDB詳解:

1,rdb備份詳情:

RDB快照觸發機制 
    1,手動觸發

        save            單進程執行,執行完成前阻塞操作     (不常用)
        bgsave          fork一個子進程執行,不影響主進程   (常用,且redis內部操作都是bgsave)


    2,自動觸發

        1,常見自動觸發場景

            1,主從複製中從節點進行全量複製,主節點自動執行bgsave,然後將rdb文件發給從節點
            2,debug reload 命令的時候。(debug reload命令的意思是快照rdb文件,清空數據庫,然後重新加載rdb)
            3,shutdown 命令 

        2,配置文件自動觸發

            ################################ 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           //900s內至少一個寫命令則bgsave
            save 300 10          //300s內至少10個寫命令則bgsave
            save 60 10000        //60s內至少10000條寫命令
            #以上是save觸發配置的默認值,可根據需要修改

            # 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 //假如bgsave出錯後,redis主進程是否支持繼續寫入
            (yes不可寫入,no繼續寫入)
            # 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   //rdb文件是否壓縮,yes壓縮,但是會影響cpu時間,no不壓縮,這樣快一點但是rdb文件會過大

            # 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      //對rdb文件是否進行校驗,默認開啓,開啓會影響cpu資源

            # The filename where to dump the DB
            dbfilename dump.rdb    //rdb文件名,可自行配置,默認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 ./               //rdb文件的目錄,可自信配置

            ################################# REPLICATION #################################



            上面一段配置文件是獲取的redis,rdb持久化的純淨配置文件。
            比較常用的就是
            save m n 
            dir rdb_path

2,常見命令:

            info stats 下 latest_fork_usec,可以獲取最近一個bgsave所用的時間
            執行latesave 可以獲取最後一個生成rdb的時間,返回一個時間戳

            還有很多其他詳情,對開發來說並不常用,需要查詢的時候度娘即可

3,總結:

            rdb備份的優點主要就是rdb文件小,恢復數據比較快。備份是子進程執行的,不會影響主進程。
            缺點就是如果寫入頻繁的話,無法做到實時備份。

2,AOF詳解:

1,aof備份詳情:

簡介:
    aof區別於rdb,aof是通過操作日誌追加來持久化reids操作。
    就是客戶端每一次對操作命令寫入日誌當中,追加aof文件。

觸發機制:
    1,手動觸發:
        bgrewriteaof 

    2,自動觸發:
        redis默認關閉aof備份,需要在配置文件中開啓。

    aof備份redis數據配置文件
    ############################## 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   //是否開啓aof持久化

    # The name of the append only file (default: "appendonly.aof")

    appendfilename "appendonly.aof"  //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 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      //依賴操作系統將數據寫入磁盤。大部分linux默認30s

    # 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 //子進程重寫的時候,主進程是否操作磁盤。設置爲yes,則同意主進程也操作磁
    盤,子進程沒結束,主進程阻塞。設置爲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 //當前aof文件超過了上一個操作的aof文件的100%,則進行重寫
    auto-aof-rewrite-min-size 64mb  //當前aof文件大於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 //redis恢復數據的時候,是否忽略最後一條指令。yes忽略。no不忽略。設置爲no的時候,如果最後一條指令有問題則redis恢復失敗

    # When rewriting the AOF file, Redis is able to use an RDB preamble in the
    # AOF file for faster rewrites and recoveries. When this option is turned
    # on the rewritten AOF file is composed of two different stanzas:
    #
    #   [RDB file][AOF tail]
    #
    # When loading Redis recognizes that the AOF file starts with the "REDIS"
    # string and loads the prefixed RDB file, and continues loading the AOF
    # tail.
    aof-use-rdb-preamble yes  //是否開啓aof/rdb混合持久化設置(redis恢復數據時候用到)。設置爲yes的時候,重寫之前的數據做rdb快照,並且將rdb快照內容和增量aof內存數據的命令存在一起,都寫入新的aof文件。增加恢復數據時候的速度

    ################################ LUA SCRIPTING  ###############################

    常用配置

        appendonly no   //是否開啓aof持久化
        appendfilename "appendonly.aof"  //aof持久化文件
        appendfsync always  //每一個操作後立即寫入磁盤。
        appendfsync everysec  //每秒鐘統計操作,寫入磁盤。
        appendfsync no      //依賴操作系統將數據寫入磁盤。大部分linux默認30s
        auto-aof-rewrite-percentage 100 //當前aof文件超過了上一個操作的aof文件的100%,則進行重寫
        auto-aof-rewrite-min-size 64mb  //當前aof文件大於64mb,則進行重寫

2,常見命令:

            bgrewriteaof 手動重寫
            redis-check-aof fileName.aof   //aof文件有損壞的情況下,修改aof文件
            還有很多其他詳細命令,對開發來說並不常用,需要的時候度娘即可

3,總結:

        aof優點可以精確到每個指令都同步。aof且是日誌型備份,所有操作相對獨立。可恢復到任何一個節點。
        缺點就是aof文件相對rdb大很多,redis恢復數據的時候效率比較低
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章