diff --git a/docs/src/samples/cluster-restore-snapshot-full.yaml b/docs/src/samples/cluster-restore-snapshot-full.yaml index 2c33dbbf60..d0a581d6ba 100644 --- a/docs/src/samples/cluster-restore-snapshot-full.yaml +++ b/docs/src/samples/cluster-restore-snapshot-full.yaml @@ -1,7 +1,7 @@ apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: - name: cluster-restore-pitr + name: cluster-restore-full spec: instances: 3 @@ -9,29 +9,10 @@ spec: size: 1Gi storageClass: csi-hostpath-sc - externalClusters: - - name: origin - - barmanObjectStore: - serverName: cluster-example-with-backup - destinationPath: s3://backups/ - endpointURL: http://minio:9000 - s3Credentials: - accessKeyId: - name: minio - key: ACCESS_KEY_ID - secretAccessKey: - name: minio - key: ACCESS_SECRET_KEY - wal: - maxParallel: 8 - bootstrap: recovery: - source: origin - volumeSnapshots: storage: - name: cluster-example-with-backup-3-1692618163 + name: cluster-example-2-1695821489 kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io diff --git a/internal/cmd/manager/instance/restoresnapshot/cmd.go b/internal/cmd/manager/instance/restoresnapshot/cmd.go index 694624e17d..c4affa868e 100644 --- a/internal/cmd/manager/instance/restoresnapshot/cmd.go +++ b/internal/cmd/manager/instance/restoresnapshot/cmd.go @@ -27,6 +27,7 @@ import ( "github.com/cloudnative-pg/cloudnative-pg/internal/management/istio" "github.com/cloudnative-pg/cloudnative-pg/internal/management/linkerd" "github.com/cloudnative-pg/cloudnative-pg/pkg/management" + "github.com/cloudnative-pg/cloudnative-pg/pkg/management/log" "github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres" ) @@ -56,7 +57,11 @@ func NewCmd() *cobra.Command { PgWal: pgWal, } - return execute(ctx, info) + err := execute(ctx, info) + if err != nil { + log.Error(err, "Error while recovering Volume Snapshot backup") + } + return err }, PostRunE: func(cmd *cobra.Command, args []string) error { if err := istio.TryInvokeQuitEndpoint(cmd.Context()); err != nil { diff --git a/pkg/fileutils/fileutils.go b/pkg/fileutils/fileutils.go index 830e82d9d9..a389f6b168 100644 --- a/pkg/fileutils/fileutils.go +++ b/pkg/fileutils/fileutils.go @@ -357,9 +357,15 @@ func RemoveFiles(basePath string, filePaths []string) error { for _, pattern := range filePaths { if len(pattern) >= 2 && pattern[len(pattern)-2:] == "/*" { dirPath := filepath.Join(basePath, pattern[:len(pattern)-2]) - if err := RemoveDirectoryContent(dirPath); err != nil { + dirExists, err := FileExists(dirPath) + if err != nil { return err } + if dirExists { + if err := RemoveDirectoryContent(dirPath); err != nil { + return err + } + } continue } diff --git a/pkg/fileutils/fileutils_test.go b/pkg/fileutils/fileutils_test.go index 9b458745a4..a888b18692 100644 --- a/pkg/fileutils/fileutils_test.go +++ b/pkg/fileutils/fileutils_test.go @@ -226,6 +226,7 @@ var _ = Describe("RemoveFiles", func() { err := RemoveFiles(tempDir, []string{ "file1.txt", "dir1/*", + "non_existent_dir/*", }) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/management/postgres/restore.go b/pkg/management/postgres/restore.go index f9ade75b82..f54b7b4c59 100644 --- a/pkg/management/postgres/restore.go +++ b/pkg/management/postgres/restore.go @@ -88,13 +88,6 @@ func (info InitInfo) RestoreSnapshot(ctx context.Context, cli client.Client) err return err } - if cluster.Spec.Bootstrap == nil || cluster.Spec.Bootstrap.Recovery == nil || - cluster.Spec.Bootstrap.Recovery.Source == "" { - // We are recovering from an existing PVC snapshot, we - // don't need to invoke the recovery job - return nil - } - log.Info("Recovering from volume snapshot", "sourceName", cluster.Spec.Bootstrap.Recovery.Source) @@ -102,6 +95,13 @@ func (info InitInfo) RestoreSnapshot(ctx context.Context, cli client.Client) err return fmt.Errorf("error while cleaning up the recovered PGDATA: %w", err) } + if cluster.Spec.Bootstrap == nil || cluster.Spec.Bootstrap.Recovery == nil || + cluster.Spec.Bootstrap.Recovery.Source == "" { + // We are recovering from an existing PVC snapshot, we + // don't need to invoke the recovery job + return nil + } + backup, env, err := info.createBackupObjectForSnapshotRestore(ctx, cli, cluster) if err != nil { return err