Redis sao lưu và phục hồi


  • administrators

    Backing up redis rdb file is simply copy .rdb to your backup location. So, we have to choose any safe destination that we'd like to store the snapshot file (It is usually safer to transfer to a different server or disk). But, let's figure out where the current RDB file is located

    cat /etc/redis/redis.conf |grep '^dir '|cut -d' ' -f2

    On my box, /var/lib/redis is the directory where the snapshot file is dumped to. Before copying, it is good idea to save the db in background.

    redis-cli bgsave

    Then copy it to somewhere that is safe. (The path /backup/redis is just an illustration of where it could be copied to)

    cp /var/lib/redis/dump.rdb /backup/redis/dump.$(date +%Y%m%d%H%M).rdb

    That's it. It is simple as that, you don't need to do anything special with your server in order to back up redis rdb. However, restoring Redis snapshot depends on the configured Redis server if it is in the fashion of the AOF (Append Only File) or not. Assuming that the Redis configuration file is at /etc/redis/redis.conf, you could simply check if it is in AOF mode by issueing this command

    cat /etc/redis/redis.conf |grep 'appendonly '|cut -d' ' -f2
    'yes' means AOF mode is enable. 'no' means AOF mode is disable.

    In case it is not AOF mode. ('no'), the process is quite straight forward. First, just stop redis server
    /etc/init.d/redis-server stop
    Then, remove the current dumb.rdb file

    rm -f /var/lib/redis/dump.rdb
    And now the snapshot file has gone, therefore we definitely want the backup one back in.

    cp /backup/dump.rdb
    Remember to set it to be owned by redis user/group

    chown redis:redis dump.rdb
    Now, just start the Redis server.

    /etc/init.d/redis-server start
    Done.

    In case it is AOF mode. ('yes'), it involes a bit more process. The first thing to do is the same as previos case, just stop the redis server

    /etc/init.d/redis-server stop
    Go inside directory where dump.rdb is located

    cd /var/lib/redis/
    After that, clean dump.rb and appendonly.aof files up

    rm -f dump.rdb appendonly.aof
    Copy the backup file in the place, along the line, correct its permission.

    cp /backup/dump.rdb /var/lib/redis/
    chown redis:redis /var/lib/redis/dump.rdb
    The important part is to disable AOF by editing /etc/redis/redis.conf file, set appendonly as no

    appendonly no
    Run the following command to create new appendonly.aof file

    Next, start the Redis server and issue the BGREWRITEAOF command:

    /etc/init.d/redis-server start
    redis-cli BGREWRITEAOF
    Check the progress (0 - done, 1 - not yet)

    redis-cli info | grep aof_rewrite_in_progress
    You should see a new appendonly.aof file. Next, stop the server.

    /etc/init.d/redis-server stop
    After it finished, enable AOF again by changing appendonly in /etc/redis/redis.conf file to yes

    appendonly yes
    Then start the redis server again.

    /etc/init.d/redis-server start
    And we are done.


Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới Cộng đồng Python Việt Nam, vui lòng đợi một lúc để chúng tôi thử kết nối lại.