| name | image-backup-restore |
| description | Implementing and debugging PostgreSQL image backup and restore features. Use when working with database snapshots, backup jobs, restore operations, azcopy blob transfers, or troubleshooting image-related provisioning failures. |
PostgreSQL Image Backup & Restore
Overview
Image backup creates point-in-time snapshots of PostgreSQL instances by:
- Running pg_dump to create a logical backup
- Uploading to Azure Blob Storage via azcopy
- Recording metadata in CMS database
Image restore provisions new instances from existing backups by:
- Creating PVC, Secret, ConfigMap for the new instance
- Running azcopy download + pg_restore to load data from blob storage
- Starting PostgreSQL with the restored data
Key Files
toygres-orchestrations/src/
โโโ orchestrations/
โ โโโ create_image.rs # Backup orchestration
โ โโโ create_instance.rs # Restore flow (v1.0.5 inlined)
โโโ activities/
โ โโโ run_backup_job.rs # pg_dump + azcopy upload
โ โโโ run_restore_job.rs # azcopy download + pg_restore
โ โโโ deploy_postgres_from_pvc.rs # Start from existing PVC
โ โโโ create_pvc.rs # Create empty PVC for restore
โ โโโ cms/
โ โโโ image_ops.rs # Image CMS operations
โโโ templates/
โ โโโ backup-job.yaml # K8s Job for backup
โ โโโ restore-job.yaml # K8s Job for restore
โโโ activity_types.rs # ImageOperation enum
Backup Flow
1. CreateImageRecord
2. GetInstanceByK8sName
3. RunBackupJob
4. UpdateImageStatus
Restore Flow (v1.0.5)
The restore logic is now inlined in create_instance v1.0.5 (no delegation to older versions):
1. Fetch image details (validate state = 'ready')
2. Get source password from image record
3. CreatePvc // Create empty PVC
4. RunRestoreJob // azcopy download + extract
5. Wait for restore job completion
6. DeployPostgresFromPvc // Start PostgreSQL
7. Wait for pod ready
8. GetConnectionStrings
9. TestConnection // Verify connectivity
Critical: Password Handling
The restore flow must use the source image's password, not a new password:
let password_result = ctx
.schedule_activity_typed::<ImageOperation, ImageOperationResult>(
cms::image_ops::NAME,
&ImageOperation::GetSourcePassword { id: image_uuid },
)
.await?;
Critical: Image Type Preservation
When restoring, the image_type is fetched from the source image record:
let image_type = match image.image_type.as_str() {
"stock" => ImageType::Stock,
"pg_durable" => ImageType::PgDurable,
_ => ImageType::Stock,
};
This ensures pg_durable images are deployed with correct configuration.
azcopy Workload Identity
CRITICAL: On AKS, always use --login-type=workload:
azcopy login --identity
azcopy login --login-type=workload
Debug workload identity issues:
kubectl exec <pod> -- env | grep AZURE_
Common Issues
test_connection Timeout
Symptom: Instance provisioning fails at test_connection step.
Root cause: Azure LoadBalancer DNS propagation can take 60-90+ seconds.
Fix: Use 120s timeout:
RetryPolicy::new(5)
.with_timeout(Duration::from_secs(120)),
azcopy 403 AuthorizationPermissionMismatch
Symptom: azcopy login --identity succeeds but operations fail with 403.
Root cause: --identity uses IMDS, not AKS workload identity.
Fix: Use --login-type=workload.
Restore Shows Wrong image_type
Symptom: Restored instance shows "stock" instead of "pg_durable" in CMS.
Root cause: Using input.image_type instead of source image's image_type.
Fix: Fetch source image details before creating CMS record.
Debugging Commands
kubectl logs -n toygres job/<job-name> -f
az storage blob list --account-name <acct> --container-name images --auth-mode login
kubectl exec -it <postgres-pod> -n toygres -- psql -U postgres -c "SELECT 1"
kubectl get jobs -n toygres
API Endpoints
POST /api/images
{
"name": "my-backup",
"instance_id": "uuid",
"description": "Optional description"
}
POST /api/instances
{
"name": "restored-instance",
"password": "ignored-uses-source",
"source_image_id": "image-uuid",
"dns_label": "restored-instance"
}
Note: source_image_id cannot be combined with runtime_image_id or image_override.