Skip to content

Commit f38556d

Browse files
armrugbartolinileonardoce
authored
fix: ensure that restore snapshot cmd omits excluded files (#2879)
After the recovery process from a volume snapshot completes, we need to cleanup the content of PGDATA, similarly to what backup tools like Barman do. This is the list of files that we remove from PGDATA: - core.* - pgsql_tmp* - postgresql.auto.conf.tmp - current_logfiles.tmp - pg_internal.init - postmaster.pid - postmaster.opts - recovery.conf - recovery.signal - standby.signal - log/* - pg_dynshmem/* - pg_log/* - pg_notify/* - pg_replslot/* - pg_serial/* - pg_snapshots/* - pg_stat_tmp/* - pg_subtrans/* Closes #2865 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Gabriele Bartolini <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Co-authored-by: Gabriele Bartolini <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]>
1 parent 6a24e5d commit f38556d

6 files changed

Lines changed: 180 additions & 41 deletions

File tree

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
apiVersion: postgresql.cnpg.io/v1
22
kind: Cluster
33
metadata:
4-
name: cluster-restore-pitr
4+
name: cluster-restore-full
55
spec:
66
instances: 3
77

88
storage:
99
size: 1Gi
1010
storageClass: csi-hostpath-sc
1111

12-
externalClusters:
13-
- name: origin
14-
15-
barmanObjectStore:
16-
serverName: cluster-example-with-backup
17-
destinationPath: s3://backups/
18-
endpointURL: http://minio:9000
19-
s3Credentials:
20-
accessKeyId:
21-
name: minio
22-
key: ACCESS_KEY_ID
23-
secretAccessKey:
24-
name: minio
25-
key: ACCESS_SECRET_KEY
26-
wal:
27-
maxParallel: 8
28-
2912
bootstrap:
3013
recovery:
31-
source: origin
32-
3314
volumeSnapshots:
3415
storage:
35-
name: cluster-example-with-backup-3-1692618163
16+
name: cluster-example-2-1695821489
3617
kind: VolumeSnapshot
3718
apiGroup: snapshot.storage.k8s.io

‎internal/cmd/manager/instance/restoresnapshot/cmd.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cloudnative-pg/cloudnative-pg/internal/management/istio"
2828
"github.com/cloudnative-pg/cloudnative-pg/internal/management/linkerd"
2929
"github.com/cloudnative-pg/cloudnative-pg/pkg/management"
30+
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
3031
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres"
3132
)
3233

@@ -56,7 +57,11 @@ func NewCmd() *cobra.Command {
5657
PgWal: pgWal,
5758
}
5859

59-
return execute(ctx, info)
60+
err := execute(ctx, info)
61+
if err != nil {
62+
log.Error(err, "Error while recovering Volume Snapshot backup")
63+
}
64+
return err
6065
},
6166
PostRunE: func(cmd *cobra.Command, args []string) error {
6267
if err := istio.TryInvokeQuitEndpoint(cmd.Context()); err != nil {

‎pkg/fileutils/fileutils.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ import (
2828
"time"
2929
)
3030

31+
// excludedPathsFromRestore contains a list of files that should not be included into the restore process
32+
var excludedPathsFromRestore = []string{
33+
"core.*",
34+
"pgsql_tmp*",
35+
"postgresql.auto.conf.tmp",
36+
"current_logfiles.tmp",
37+
"pg_internal.init",
38+
"postmaster.pid",
39+
"postmaster.opts",
40+
"recovery.conf",
41+
"recovery.signal",
42+
"standby.signal",
43+
"log/*",
44+
"pg_dynshmem/*",
45+
"pg_log/*",
46+
"pg_notify/*",
47+
"pg_replslot/*",
48+
"pg_serial/*",
49+
"pg_snapshots/*",
50+
"pg_stat_tmp/*",
51+
"pg_subtrans/*",
52+
}
53+
3154
// AppendStringToFile append the content of the given string to the
3255
// end of the target file prepending new data with a carriage return
3356
func AppendStringToFile(targetFile string, content string) (err error) {
@@ -320,6 +343,57 @@ func GetDirectoryContent(dir string) (files []string, err error) {
320343
return
321344
}
322345

346+
// RemoveFiles deletes the files and directories specified by the filePaths patterns
347+
// relative to the basePath. If a pattern ends with "/*", it implies that all the
348+
// contents of the directory (not the directory itself) matching the pattern should
349+
// be removed. If a pattern does not end with "/*", then the files matching the
350+
// pattern will be removed.
351+
//
352+
// Example:
353+
// basePath: "/path/to/directory"
354+
// filePaths: ["file1.txt", "subdir/*"]
355+
// This would remove "/path/to/direct
356+
func RemoveFiles(basePath string, filePaths []string) error {
357+
for _, pattern := range filePaths {
358+
if len(pattern) >= 2 && pattern[len(pattern)-2:] == "/*" {
359+
dirPath := filepath.Join(basePath, pattern[:len(pattern)-2])
360+
dirExists, err := FileExists(dirPath)
361+
if err != nil {
362+
return err
363+
}
364+
if dirExists {
365+
if err := RemoveDirectoryContent(dirPath); err != nil {
366+
return err
367+
}
368+
}
369+
continue
370+
}
371+
372+
matches, err := filepath.Glob(filepath.Join(basePath, pattern))
373+
if err != nil {
374+
return err
375+
}
376+
for _, match := range matches {
377+
if err := RemoveFile(match); err != nil {
378+
return err
379+
}
380+
}
381+
}
382+
return nil
383+
}
384+
385+
// RemoveRestoreExcludedFiles removes files and directories that are excluded during a restore operation.
386+
// It leverages the RemoveFiles function, using a predefined list of paths that are meant to be excluded.
387+
//
388+
// Parameters:
389+
// - basePath: The root path from which the exclusions should be applied.
390+
//
391+
// Returns:
392+
// - error: Any error encountered during the removal process, or nil if the operation was successful.
393+
func RemoveRestoreExcludedFiles(basePath string) error {
394+
return RemoveFiles(basePath, excludedPathsFromRestore)
395+
}
396+
323397
// MoveDirectoryContent moves a directory from a source path to its destination by copying
324398
// the source files or subdirectories recursively to the destination.
325399
// Once this is done it deletes the files from the original location.

‎pkg/fileutils/fileutils_test.go‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,94 @@ var _ = Describe("function MoveDirectoryContent", func() {
200200
Expect(files).Should(BeEmpty())
201201
})
202202
})
203+
204+
var _ = Describe("RemoveFiles", func() {
205+
var tempDir string
206+
207+
BeforeEach(func() {
208+
var err error
209+
tempDir, err = os.MkdirTemp("", "test")
210+
Expect(err).NotTo(HaveOccurred())
211+
212+
// Create some sample files and directories
213+
Expect(os.WriteFile(filepath.Join(tempDir, "file1.txt"), []byte("test"), 0o600)).To(Succeed())
214+
Expect(os.WriteFile(filepath.Join(tempDir, "file2.txt"), []byte("test"), 0o600)).To(Succeed())
215+
Expect(os.Mkdir(filepath.Join(tempDir, "dir1"), 0o750)).To(Succeed())
216+
Expect(os.WriteFile(filepath.Join(tempDir, "dir1", "file3.txt"), []byte("test"), 0o600)).To(Succeed())
217+
})
218+
219+
AfterEach(func() {
220+
// Cleanup
221+
Expect(os.RemoveAll(tempDir)).To(Succeed())
222+
})
223+
224+
It("removes specified files and directories", func() {
225+
// Use the RemoveFiles function
226+
err := RemoveFiles(tempDir, []string{
227+
"file1.txt",
228+
"dir1/*",
229+
"non_existent_dir/*",
230+
})
231+
Expect(err).NotTo(HaveOccurred())
232+
233+
// Assert files and directories are removed as expected
234+
_, err = os.Stat(filepath.Join(tempDir, "file1.txt"))
235+
Expect(os.IsNotExist(err)).To(BeTrue(), "Expected file1.txt to be removed")
236+
237+
_, err = os.Stat(filepath.Join(tempDir, "file2.txt"))
238+
Expect(err).NotTo(HaveOccurred(), "Expected file2.txt to not be removed")
239+
240+
_, err = os.Stat(filepath.Join(tempDir, "dir1", "file3.txt"))
241+
Expect(os.IsNotExist(err)).To(BeTrue(), "Expected dir1/file3.txt to be removed")
242+
243+
_, err = os.Stat(filepath.Join(tempDir, "dir1"))
244+
Expect(err).NotTo(HaveOccurred(), "Expected dir1 to not be removed")
245+
})
246+
})
247+
248+
var _ = Describe("RemoveRestoreExcludedFiles", func() {
249+
var tempDir string
250+
251+
BeforeEach(func() {
252+
var err error
253+
tempDir, err = os.MkdirTemp("", "test")
254+
Expect(err).NotTo(HaveOccurred())
255+
256+
// Create temporary directories and files
257+
for _, path := range excludedPathsFromRestore {
258+
fullPath := filepath.Join(tempDir, path)
259+
if len(path) >= 2 && path[len(path)-2:] == "/*" {
260+
dirToCreate := fullPath[:len(fullPath)-2]
261+
Expect(os.Mkdir(dirToCreate, 0o750)).To(Succeed())
262+
Expect(os.WriteFile(filepath.Join(dirToCreate, "testfile.txt"), []byte("test"), 0o600)).To(Succeed())
263+
continue
264+
}
265+
// Ensure directory exists before creating the file
266+
dirOfTheFile := filepath.Dir(fullPath)
267+
if _, err := os.Stat(dirOfTheFile); os.IsNotExist(err) {
268+
Expect(os.MkdirAll(dirOfTheFile, 0o750)).To(Succeed())
269+
}
270+
Expect(os.WriteFile(fullPath, []byte("test"), 0o600)).To(Succeed())
271+
272+
}
273+
})
274+
275+
AfterEach(func() {
276+
_ = os.RemoveAll(tempDir)
277+
})
278+
279+
It("should correctly remove specified files and directories", func() {
280+
Expect(RemoveRestoreExcludedFiles(tempDir)).To(Succeed())
281+
282+
for _, path := range excludedPathsFromRestore {
283+
fullPath := filepath.Join(tempDir, path)
284+
if len(path) >= 2 && path[len(path)-2:] == "/*" {
285+
_, err := os.Stat(filepath.Join(fullPath[:len(fullPath)-2], "testfile.txt"))
286+
Expect(os.IsNotExist(err)).To(BeTrue(), "Expected directory contents to be removed: "+fullPath)
287+
} else {
288+
_, err := os.Stat(fullPath)
289+
Expect(os.IsNotExist(err)).To(BeTrue(), "Expected file to be removed: "+fullPath)
290+
}
291+
}
292+
})
293+
})

‎pkg/management/postgres/configuration.go‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,6 @@ func configurePostgresAutoConfFile(pgData, primaryConnInfo, slotName string) (ch
281281
return changed, nil
282282
}
283283

284-
// removeSignalFiles removes the PostgreSQL signal files to get
285-
// a cleanup up pgdata. This is useful after having restored
286-
// a PVC snapshot
287-
func removeSignalFiles(pgData string) error {
288-
standbySignalFile := filepath.Join(pgData, "standby.signal")
289-
if err := fileutils.RemoveFile(standbySignalFile); err != nil {
290-
return err
291-
}
292-
293-
recoverySignalFile := filepath.Join(pgData, "recovery.signal")
294-
return fileutils.RemoveFile(recoverySignalFile)
295-
}
296-
297284
// createStandbySignal creates a standby.signal file for PostgreSQL 12 and beyond
298285
func createStandbySignal(pgData string) error {
299286
emptyFile, err := os.Create(filepath.Clean(filepath.Join(pgData, "standby.signal")))

‎pkg/management/postgres/restore.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@ func (info InitInfo) RestoreSnapshot(ctx context.Context, cli client.Client) err
8888
return err
8989
}
9090

91+
log.Info("Recovering from volume snapshot",
92+
"sourceName", cluster.Spec.Bootstrap.Recovery.Source)
93+
94+
if err := fileutils.RemoveRestoreExcludedFiles(info.PgData); err != nil {
95+
return fmt.Errorf("error while cleaning up the recovered PGDATA: %w", err)
96+
}
97+
9198
if cluster.Spec.Bootstrap == nil || cluster.Spec.Bootstrap.Recovery == nil ||
9299
cluster.Spec.Bootstrap.Recovery.Source == "" {
93100
// We are recovering from an existing PVC snapshot, we
94101
// don't need to invoke the recovery job
95102
return nil
96103
}
97104

98-
log.Info("Recovering from volume snapshot",
99-
"sourceName", cluster.Spec.Bootstrap.Recovery.Source)
100-
if err := removeSignalFiles(info.PgData); err != nil {
101-
return fmt.Errorf("error while cleaning up the signal files: %w", err)
102-
}
103-
104105
backup, env, err := info.createBackupObjectForSnapshotRestore(ctx, cli, cluster)
105106
if err != nil {
106107
return err

0 commit comments

Comments
 (0)