From 455415e4ffe9a8b9e0d72075b89d770ea0359189 Mon Sep 17 00:00:00 2001
From: Zakhar Bessarab <z.bessarab@victoriametrics.com>
Date: Thu, 21 Sep 2023 13:25:19 +0400
Subject: [PATCH] lib/backup: fix issue with inconsistent copying of
 appliedRetention.txt (#5027)

* lib/backup: fix issue with inconsistent copying of appliedRetention.txt

appliedRetention.txt can be modified in place, so it should be always copied just the same as parts.json

Updates: https://github.com/victoriaMetrics/victoriaMetrics/issues/5005
Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* docs: add changelog entry for appliedRetention.txt copying fix

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

---------

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>
---
 docs/CHANGELOG.md         | 2 ++
 lib/backup/common/part.go | 8 +++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 7606ec6c03..d5330880b1 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -12,6 +12,8 @@ The following `tip` changes can be tested by building VictoriaMetrics components
 
 ## v1.93.x long-time support release (LTS)
 
+* BUGFIX: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): properly copy `appliedRetention.txt` files inside `<-storageDataPath>/{data}` folders during [incremental backups](https://docs.victoriametrics.com/vmbackup.html#incremental-backups). Previously the new `appliedRetention.txt` could be skipped during incremental backups, which could lead to increased load on storage after restoring from backup. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5005).
+
 ## [v1.93.5](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.93.5)
 
 Released at 2023-09-19
diff --git a/lib/backup/common/part.go b/lib/backup/common/part.go
index d7f9aa239b..8b9c68e31c 100644
--- a/lib/backup/common/part.go
+++ b/lib/backup/common/part.go
@@ -40,9 +40,11 @@ type Part struct {
 
 // key returns a string, which uniquely identifies p.
 func (p *Part) key() string {
-	if strings.HasSuffix(p.Path, "/parts.json") {
-		// parts.json file contents changes over time, so it must have an unique key in order
-		// to always copy it during backup, restore and server-side copy.
+	if strings.HasSuffix(p.Path, "/parts.json") ||
+		strings.HasSuffix(p.Path, "/appliedRetention.txt") {
+		// parts.json and appliedRetention.txt files contents changes over time,
+		// so it must have an unique key in order to always copy it during
+		// backup, restore and server-side copy.
 		// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5005
 		id := atomic.AddUint64(&uniqueKeyID, 1)
 		return fmt.Sprintf("unique-%016X", id)