This article explains how you can perform Point-in-time-Recovery (PITR) in Valkey/Redis.
To perform PITR, you need to have append-only logging enabled.
By default, AOF in Valkey/Redis only records the operations that have been executed against the instance, not when they were executed. For that, we need to enable the aof-timestamp-enabled parameter.
So your Valkey/Redis instance needs to have the following parameters:
|
1 |
appendonly yes<br>appendfilename "appendonly.aof"<br>aof-timestamp-enabled yes |
AOF persistence logs every write operation received by the server. These operations can then be replayed again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Valkey protocol itself.
Valkey/Redis writes the commands to AOF files in plaintext, so if you only need to remove an accidental command (i.e., FLUSHDB), or there are corrupted commands in the append-only logs, you can simply open the AOF file and fix/delete it.
A more comprehensive documentation on Valkey/Redis serialization format can be found on the official Valkey documentation.
So, because AOF will be rewritten when its size reaches the value defined by auto-aof-rewrite-*, if you do not have backups of the append-only logs available, you can only restore up to the point when AOF rewrite started. While the auto rewrite process can be disabled (by setting auto-aof-rewrite-percentage to 0), if you opt to do so, you will need to monitor the server’s storage closely and take action to ensure it is not completely filled.
You can identify the available restore points by grepping the AOF file for the string #TS:
|
1 |
# grep '#TS' appendonly.aof.1.incr.aof<br>#TS:1764918042<br>#TS:1764918195<br>#TS:1764918200<br>#TS:1764918201<br>#TS:1764918207<br>#TS:1764918214<br>#TS:1764918216<br>#TS:1764918220<br>#TS:1764918221<br>#TS:1764918222<br>#TS:1764918330 |
To quickly convert the timestamps in AOF files into human-readable dates, we can use the commands below
|
1 |
# grep '#TS' appendonly.aof.1.incr.aof | sed 's|#TS:||g' |tr -d 'r'| while read -r ts<br>do<br> echo "#TS:${ts} = $(date -d "@$ts" +"%Y-%m-%d %H:%M:%S")"<br>done<br>#TS:1764918042 = 2025-12-05 07:00:42<br>#TS:1764918195 = 2025-12-05 07:03:15<br>#TS:1764918200 = 2025-12-05 07:03:20<br>#TS:1764918201 = 2025-12-05 07:03:21<br>#TS:1764918207 = 2025-12-05 07:03:27<br>#TS:1764918214 = 2025-12-05 07:03:34<br>#TS:1764918216 = 2025-12-05 07:03:36<br>#TS:1764918220 = 2025-12-05 07:03:40<br>#TS:1764918221 = 2025-12-05 07:03:41<br>#TS:1764918222 = 2025-12-05 07:03:42<br>#TS:1764918330 = 2025-12-05 07:05:30 |
After identifying the timestamp you wish to restore to, we can use the command valkey-check-aof to truncate the AOF file to that point. For example, if I wanted to restore to the timestamp 1764918201 (2025-12-05 07:03:21):
|
1 |
# valkey-check-aof --truncate-to-timestamp 1764918201 appendonly.aof.1.incr.aof<br>Start checking Old-Style AOF<br>Successfully truncated AOF appendonly.aof.1.incr.aof to timestamp 1764918201 |
We can then check the result by grepping the AOF file again for timestamps
|
1 |
grep '#TS' appendonly.aof.1.incr.aof<br>#TS:1764918042<br>#TS:1764918195<br>#TS:1764918200<br>#TS:1764918201 |
After truncating, we can start the service as normal, and confirm that valkey-server can read the truncated AOF file by viewing its log:
|
1 |
93048:M 05 Dec 2025 07:38:35.798 * Server initialized<br>93048:M 05 Dec 2025 07:38:35.798 * Reading RDB base file on AOF loading...<br>93048:M 05 Dec 2025 07:38:35.798 * Loading RDB produced by Valkey version 8.1.4<br>93048:M 05 Dec 2025 07:38:35.798 * RDB age 2292 seconds<br>93048:M 05 Dec 2025 07:38:35.798 * RDB memory usage when created 0.87 Mb<br>93048:M 05 Dec 2025 07:38:35.798 * RDB is base AOF<br>93048:M 05 Dec 2025 07:38:35.798 * Done loading RDB, keys loaded: 0, keys expired: 0.<br>93048:M 05 Dec 2025 07:38:35.798 * DB loaded from base file appendonly.aof.1.base.rdb: 0.000 seconds<br>93048:M 05 Dec 2025 07:38:35.800 * DB loaded from incr file appendonly.aof.1.incr.aof: 0.002 seconds<br>93048:M 05 Dec 2025 07:38:35.800 * DB loaded from append only file: 0.002 seconds<br>93048:M 05 Dec 2025 07:38:35.800 * Opening AOF incr file appendonly.aof.1.incr.aof on server start<br>93048:M 05 Dec 2025 07:38:35.800 * Ready to accept connections tcp |
Valkey/Redis Point-in-Time Recovery (PITR) is achieved exclusively through the Append Only File (AOF) mechanism with timestamping enabled.
The final process is:
Resources
RELATED POSTS