@@ -20,7 +20,8 @@ stackstate-backup-cli/
2020│ ├── root.go # Root command and global flags
2121│ ├── version/ # Version information command
2222│ ├── elasticsearch/ # Elasticsearch backup/restore commands
23- │ └── stackgraph/ # Stackgraph backup/restore commands
23+ │ ├── stackgraph/ # Stackgraph backup/restore commands
24+ │ └── victoriametrics/ # VictoriaMetrics backup/restore commands
2425│
2526├── internal/ # Internal packages (Layers 0-3)
2627│ ├── foundation/ # Layer 0: Core utilities
@@ -35,7 +36,8 @@ stackstate-backup-cli/
3536│ │
3637│ ├── orchestration/ # Layer 2: Workflows
3738│ │ ├── portforward/ # Port-forwarding orchestration
38- │ │ └── scale/ # Deployment scaling workflows
39+ │ │ ├── scale/ # Deployment/StatefulSet scaling workflows
40+ │ │ └── restore/ # Restore job orchestration
3941│ │
4042│ ├── app/ # Layer 3: Dependency Container
4143│ │ └── app.go # Application context and dependency injection
@@ -62,7 +64,8 @@ stackstate-backup-cli/
6264
6365** Key Packages** :
6466- ` cmd/elasticsearch/ ` : Elasticsearch snapshot/restore commands (configure, list-snapshots, list-indices, restore-snapshot)
65- - ` cmd/stackgraph/ ` : Stackgraph backup/restore commands (list, restore)
67+ - ` cmd/stackgraph/ ` : Stackgraph backup/restore commands (list, restore, check-and-finalize)
68+ - ` cmd/victoriametrics/ ` : VictoriaMetrics backup/restore commands (list, restore, check-and-finalize)
6669- ` cmd/version/ ` : Version information
6770
6871** Dependency Rules** :
@@ -117,7 +120,8 @@ appCtx.Formatter
117120
118121** Key Packages** :
119122- ` portforward/ ` : Manages Kubernetes port-forwarding lifecycle
120- - ` scale/ ` : Deployment scaling workflows with detailed logging
123+ - ` scale/ ` : Deployment and StatefulSet scaling workflows with detailed logging
124+ - ` restore/ ` : Restore job orchestration (confirmation, job lifecycle, finalization, resource management)
121125
122126** Dependency Rules** :
123127- ✅ Can import: ` internal/foundation/* ` , ` internal/clients/* `
@@ -167,7 +171,7 @@ appCtx.Formatter
167171
168172```
1691731. User invokes CLI command
170- └─> cmd/elasticsearch /restore-snapshot .go
174+ └─> cmd/victoriametrics /restore.go (or stackgraph/restore.go)
171175 │
1721762. Parse flags and validate input
173177 └─> Cobra command receives global flags
@@ -177,16 +181,17 @@ appCtx.Formatter
177181 ├─> internal/clients/k8s/ (K8s client)
178182 ├─> internal/foundation/config/ (Load from ConfigMap/Secret)
179183 ├─> internal/clients/s3/ (S3/Minio client)
180- ├─> internal/clients/elasticsearch/ (ES client)
181184 ├─> internal/foundation/logger/ (Logger)
182185 └─> internal/foundation/output/ (Formatter)
183186 │
1841874. Execute business logic with injected dependencies
185188 └─> runRestore(appCtx)
186- ├─> internal/orchestration/scale/ (Scale down)
187- ├─> internal/orchestration/portforward/ (Port-forward)
188- ├─> internal/clients/elasticsearch/ (Restore snapshot)
189- └─> internal/orchestration/scale/ (Scale up)
189+ ├─> internal/orchestration/restore/ (User confirmation)
190+ ├─> internal/orchestration/scale/ (Scale down StatefulSets)
191+ ├─> internal/orchestration/restore/ (Ensure resources: ConfigMaps, Secrets)
192+ ├─> internal/clients/k8s/ (Create restore Job)
193+ ├─> internal/orchestration/restore/ (Wait for completion & cleanup)
194+ └─> internal/orchestration/scale/ (Scale up StatefulSets)
190195 │
1911965. Format and display results
192197 └─> appCtx.Formatter.PrintTable() or PrintJSON()
@@ -262,15 +267,50 @@ defer close(pf.StopChan) // Automatic cleanup
262267
263268### 5. Scale Down/Up Pattern
264269
265- Deployments are scaled down before restore operations and scaled up afterward:
270+ Deployments and StatefulSets are scaled down before restore operations and scaled up afterward:
266271
267272``` go
268273// Example usage
269- scaledDeployments , _ := scale.ScaleDown (k8sClient, namespace, selector, log)
270- defer scale.ScaleUp (k8sClient, namespace, scaledDeployments , log)
274+ scaledResources , _ := scale.ScaleDown (k8sClient, namespace, selector, log)
275+ defer scale.ScaleUpFromAnnotations (k8sClient, namespace, selector , log)
271276```
272277
273- ### 6. Structured Logging
278+ ** Note** : Scaling now supports both Deployments and StatefulSets through a unified interface.
279+
280+ ### 6. Restore Orchestration Pattern
281+
282+ Common restore operations are centralized in the ` restore ` orchestration layer:
283+
284+ ``` go
285+ // User confirmation
286+ if !restore.PromptForConfirmation () {
287+ return fmt.Errorf (" operation cancelled" )
288+ }
289+
290+ // Wait for job completion and cleanup
291+ restore.PrintWaitingMessage (log, " service-name" , jobName, namespace)
292+ err := restore.WaitAndCleanup (k8sClient, namespace, jobName, log, cleanupPVC)
293+
294+ // Check and finalize background jobs
295+ err := restore.CheckAndFinalize (restore.CheckAndFinalizeParams {
296+ K8sClient: k8sClient,
297+ Namespace: namespace,
298+ JobName: jobName,
299+ ServiceName: " service-name" ,
300+ ScaleSelector: config.ScaleDownLabelSelector ,
301+ CleanupPVC: true ,
302+ WaitForJob: false ,
303+ Log: log,
304+ })
305+ ```
306+
307+ ** Benefits** :
308+
309+ - Eliminates duplicate code between Stackgraph and VictoriaMetrics restore commands
310+ - Consistent user experience across services
311+ - Centralized job lifecycle management and cleanup
312+
313+ ### 7. Structured Logging
274314
275315All operations use structured logging with consistent levels:
276316
0 commit comments