Home > Article > Backend Development > PHP and Redis database backup and recovery
PHP and Redis database backup and recovery
Redis is an open source, high-performance memory database. It can be used as a cache, message queue, counter, etc. It is widely used in Web applications and is the successor to PHP. One of the commonly used tools for end-end development. In practical applications, Redis data backup and recovery is very important, so in this article we will introduce how to use PHP for Redis database backup and recovery.
1. Redis data backup
Redis provides a variety of backup commands, which we can implement by executing the following commands Backup:
SAVE
This command will save the data snapshot of the current Redis server to a .rdb file on the hard disk. Its default path is the working directory when the Redis server is started.
We can also specify the save path of the backup file by modifying the dir option in the Redis configuration file redis.conf.
Redis supports two persistence methods: RDB and AOF. RDB is backed up in the form of snapshots, and AOF is backed up in the form of appends. Both methods can ensure data security.
RDB backup can be achieved by modifying the Redis configuration file redis.conf. Set the following two parameters:
save 900 1 #900秒内如果有至少1个key进行了修改,就会执行快照备份 dir /path/to/dump/ #设置快照备份文件的保存路径
AOF backup can be started by executing the following command:
appendonly yes
The AOF file will record all write operations, and the AOF file can be rebuilt by executing the BGREWRITEAOF command.
2. Redis data recovery
Redis provides the command to load .rdb files:
BGSAVE
This command will perform a snapshot backup in the background and save the results to a .rdb file.
We can also load the specified .rdb file into Redis by executing the following command:
CONFIG SET dir /path/to/dump/ CONFIG SET dbfilename dump.rdb SHUTDOWN
After executing the above command, the Redis server will automatically close and reload the specified .rdb file. .
Backup files in RDB mode can be restored directly. You only need to put the backup files back to the working directory when the Redis server is started. and restart Redis.
Backup files in AOF mode need to be restored by executing the following command:
redis-cli bgrewriteaof
This command will rewrite the AOF file and regenerate a new AOF file. After the operation is completed, you can execute the following command to reload the AOF file:
redis-cli config set appendonly yes #开启AOF redis-cli config set appendfilename "appendonly.aof" #设置AOF文件名 redis-cli config set dir /path/to/dump/ #设置恢复文件的保存路径 redis-cli shutdown
After executing the above command, the Redis server will automatically close and reload the specified AOF file.
Summary
As an in-memory database, Redis is widely used in Web applications, and data backup and recovery are very important. When backing up and restoring through Redis built-in commands, you need to pay attention to the file path of the snapshot backup, the snapshot backup command BGSAVE, the rewrite AOF command BGREWRITEAOF, etc. RDB backup in persistence mode can be directly put back into the working directory for recovery. AOF backup requires rewriting and reloading the AOF file through BGREWRITEAOF. PHP and Redis database cooperate to perform backup and recovery operations with high efficiency.
The above is the detailed content of PHP and Redis database backup and recovery. For more information, please follow other related articles on the PHP Chinese website!