plugin-barman-cloud icon indicating copy to clipboard operation
plugin-barman-cloud copied to clipboard

Backup Restore fails if we use same name as source db pod

Open vishaltanawade opened this issue 9 months ago • 10 comments

@dosu I am using CNPG 1.26 with Barman Plugin 0.11.0 on ak8s.

I taken the backup of one of DB cluster on azure blob stoarage.

I restore db with different name from that backup and it worked.

Now i deleted that original db . tried to restore db from backup with same db cluster name and it is failing. why we cannot use same db name.

vishaltanawade avatar Feb 14 '26 07:02 vishaltanawade

This is a known design constraint in CloudNativePG, not a bug. The restore fails with an "Expected empty archive" error because CloudNativePG enforces a safety check requiring the archive location to be empty for the target server name.

Why it happens: When you don't explicitly set a serverName, it defaults to the cluster name. So when you try to restore with the same cluster name, it finds existing WAL/backup data at that archive path and blocks the restore to prevent data corruption.

Workarounds:

  1. Use a different cluster name (recommended) - Update your applications to point to the new service name afterward.

  2. Set a different serverName in your recovery configuration - This isolates the restore path from the original cluster's archive while letting you keep the same cluster name.

  3. Clean the archive path first - Empty the storage bucket before recovery (not practical if you need to keep the backups).

There is an annotation (cnpg.io/skipEmptyWalArchiveCheck) to bypass this check, but it's strongly discouraged due to data loss risks.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about plugin-barman-cloud Share Dosu with your team

dosubot[bot] avatar Feb 14 '26 07:02 dosubot[bot]

Hi @dosu,

I crates multiple db clusters dynamically by just appending integer project id in name.

suppose if any cluster need to restore from backup then we cannot change db connection logic , in db connection logic we just replace project id . so i strongly need same cluster name to be created.

or eg: i creates db cluster with name test-db-{id}, in code i dynamically replace id . so while connection also i dynamically replace id by taking id from url, i cannot change id in url, it should be same , that why i want to restore db with same name, is there any way

vishaltanawade avatar May 20 '26 05:05 vishaltanawade

Hi @vishaltanawade,

Yes, you can restore with the same cluster name — you just need to set a different serverName in your recovery configuration. The serverName controls which path in the object store is used for archiving WAL/backups, and it's independent of the Kubernetes cluster resource name [1].

Here's how: when you restore, specify the original cluster's serverName in the externalClusters plugin parameters, and set a new serverName for the restored cluster's ongoing backups:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: test-db-123  # Same name as the original
spec:
  instances: 3
  bootstrap:
    recovery:
      source: source
  externalClusters:
    - name: source
      plugin:
        name: barman-cloud.cloudnative-pg.io
        parameters:
          barmanObjectName: your-object-store
          serverName: test-db-123  # Points to original backup location for recovery

The key point: the new cluster's own backup configuration (in its ObjectStore) should use a different serverName (e.g., test-db-123-v2) so it doesn't conflict with the existing archive data [2] [3]. This gives you an isolated storage path for the restored cluster's future WAL archiving while still allowing you to keep the same cluster name for your connection logic.

So the flow would be:

  1. Delete the original test-db-{id} cluster
  2. Create the new cluster with the same name test-db-{id}
  3. In recovery config, point serverName to the original backup path
  4. In the new cluster's own backup ObjectStore, use a different serverName (e.g., test-db-{id}-restored or append a version/timestamp)

This way your connection logic stays unchanged since the Kubernetes service name remains test-db-{id}.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about plugin-barman-cloud with Dosu Add Dosu to your team

dosubot[bot] avatar May 20 '26 05:05 dosubot[bot]

@dosu, you are saying The key point: the new cluster's own backup configuration (in its ObjectStore) should use a different serverName (e.g., test-db-123-v2) so it doesn't conflict with the existing archive data [2] https://github.com/cloudnative-pg/cloudnative-pg/discussions/10347.

can you show full yaml for that?

vishaltanawade avatar May 20 '26 14:05 vishaltanawade

@vishaltanawade here's the full YAML. Adapt the storage credentials to Azure Blob Storage for your case:

---
# ObjectStore resource (your Azure Blob Storage config)
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
metadata:
  name: azure-store
  namespace: default
spec:
  configuration:
    destinationPath: https://<storage-account>.blob.core.windows.net/<container>/
    azureCredentials:
      connectionString:
        name: azure-creds
        key: AZURE_CONNECTION_STRING
    wal:
      compression: gzip

---
# Cluster restored with the SAME name
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: test-db-123  # Same name as original
  namespace: default
spec:
  instances: 3

  # Restore from the original cluster's backup
  bootstrap:
    recovery:
      source: original-backup

  # Points to where the original backups live
  externalClusters:
  - name: original-backup
    plugin:
      name: barman-cloud.cloudnative-pg.io
      parameters:
        barmanObjectName: azure-store
        serverName: test-db-123  # Original cluster's serverName (where backups were written)

  # Configure ongoing backups with a DIFFERENT serverName
  plugins:
  - name: barman-cloud.cloudnative-pg.io
    parameters:
      barmanObjectName: azure-store
      serverName: test-db-123-v2  # New serverName to avoid archive conflict

The important parts [1] [2]:

  • externalClusters[].plugin.parameters.serverName → set to test-db-123 (the original name) so recovery knows where to find the backup data
  • plugins[].parameters.serverName → set to something different like test-db-123-v2 so the new cluster's WAL archiving writes to an isolated path and doesn't trigger the "expected empty archive" error

The cluster name (metadata.name) stays test-db-123, so your Kubernetes service name and connection logic remain unchanged. Only the internal backup storage path changes via serverName.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about plugin-barman-cloud Share Dosu with your team

dosubot[bot] avatar May 20 '26 14:05 dosubot[bot]

If i do like above will it create any issue later for backup or restore ? @dosu

vishaltanawade avatar May 21 '26 04:05 vishaltanawade

@vishaltanawade no, this is actually the recommended pattern and should not cause issues for future backups or restores [1]. Here's what to expect:

Ongoing backups — Will work normally. The new serverName (test-db-123-v2) creates its own isolated folder in your Azure Blob Storage for both backups and WAL files. Scheduled backups, retention policies, and WAL archiving all operate independently under the new serverName.

Future restores — If you ever need to restore again from the new backups, you simply reference test-db-123-v2 as the serverName in the externalClusters section, and use yet another new serverName (e.g., test-db-123-v3) for that cluster's ongoing backups [2].

One thing to keep in mind: Each restore cycle creates a new serverName "generation," so over time you'll accumulate old backup data in your storage under previous serverNames. You may want to periodically clean up old serverName folders from Azure Blob Storage once you're confident you no longer need those backups.

In short: backups, WAL archiving, retention, and future restores all work correctly with this approach. It's the safe and supported way to handle your use case.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about plugin-barman-cloud with Dosu Add Dosu to your team

dosubot[bot] avatar May 21 '26 04:05 dosubot[bot]

@dosu,

Future restores — If you ever need to restore again from the new backups, you simply reference test-db-123-v2 as the serverName in the externalClusters section, and use yet another new serverName (e.g., test-db-123-v3) for that cluster's ongoing backups https://github.com/cloudnative-pg/plugin-barman-cloud/issues/827#issuecomment-4189242785.

One thing to keep in mind: Each restore cycle creates a new serverName "generation," so over time you'll accumulate old backup data in your storage under previous serverNames. You may want to periodically clean up old serverName folders from Azure Blob Storage once you're confident you no longer need those backups.

In short: backups, WAL archiving, retention, and future restores all work correctly with this approach. It's the safe and supported way to handle your use case.

you are saying this,

lets suppose i want to take backup of this restored db and later want to restore it as well, can you please provide that full yaml

vishaltanawade avatar May 21 '26 05:05 vishaltanawade

@vishaltanawade here's the full lifecycle across two generations:


Step 1: The restored cluster (from earlier) — takes new backups under test-db-123-v2

---
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
metadata:
  name: azure-store
  namespace: default
spec:
  configuration:
    destinationPath: https://<storage-account>.blob.core.windows.net/<container>/
    azureCredentials:
      connectionString:
        name: azure-creds
        key: AZURE_CONNECTION_STRING
    wal:
      compression: gzip
    data:
      compression: gzip

---
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: test-db-123
  namespace: default
spec:
  instances: 3

  # Restore from original backup
  bootstrap:
    recovery:
      source: original-backup

  externalClusters:
  - name: original-backup
    plugin:
      name: barman-cloud.cloudnative-pg.io
      parameters:
        barmanObjectName: azure-store
        serverName: test-db-123  # Where original backups live

  # Ongoing backups go to a NEW serverName
  plugins:
  - name: barman-cloud.cloudnative-pg.io
    parameters:
      barmanObjectName: azure-store
      serverName: test-db-123-v2  # New generation for this cluster's backups

---
# ScheduledBackup to trigger periodic backups
apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
  name: test-db-123-backup
  namespace: default
spec:
  schedule: "0 0 */1 * *"  # Daily at midnight (adjust as needed)
  backupOwnerReference: self
  cluster:
    name: test-db-123
  pluginConfiguration:
    name: barman-cloud.cloudnative-pg.io
    parameters:
      barmanObjectName: azure-store

Step 2: Later, if you need to restore from the v2 backups — use test-db-123-v3 for the next generation

---
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: test-db-123  # Same cluster name again
  namespace: default
spec:
  instances: 3

  # Restore from the v2 backups
  bootstrap:
    recovery:
      source: v2-backup

  externalClusters:
  - name: v2-backup
    plugin:
      name: barman-cloud.cloudnative-pg.io
      parameters:
        barmanObjectName: azure-store
        serverName: test-db-123-v2  # Points to where v2 backups were written

  # This cluster's NEW backups go to v3
  plugins:
  - name: barman-cloud.cloudnative-pg.io
    parameters:
      barmanObjectName: azure-store
      serverName: test-db-123-v3  # Next generation

---
apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
  name: test-db-123-backup
  namespace: default
spec:
  schedule: "0 0 */1 * *"
  backupOwnerReference: self
  cluster:
    name: test-db-123
  pluginConfiguration:
    name: barman-cloud.cloudnative-pg.io
    parameters:
      barmanObjectName: azure-store

The pattern is always the same:

Scenario externalClusters serverName (read from) plugins serverName (write to)
First restore test-db-123 test-db-123-v2
Second restore test-db-123-v2 test-db-123-v3
Third restore test-db-123-v3 test-db-123-v4

Each time you restore, bump the version number. The cluster name (test-db-123) stays the same throughout, so your connection logic never changes.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about plugin-barman-cloud Share Dosu with your team

dosubot[bot] avatar May 21 '26 05:05 dosubot[bot]

Restore from the v2 backups

bootstrap: recovery: source: v2-backup

above section is from restore db , can you please explain what is this source , or it is random string , generally i used azure-source as it value and it worked

vishaltanawade avatar May 21 '26 05:05 vishaltanawade