-
Notifications
You must be signed in to change notification settings - Fork 0
STAC-24630: Add stackgraph-v2 backup/restore #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
craffit
wants to merge
11
commits into
main
Choose a base branch
from
stac-24630
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8f55350
STAC-24630: Add stackgraph-v2 backup/restore
craffit 1a76699
STAC-24630: Work out review comments
craffit 24b1933
STAC-24630: Add additional message when jobs fail/get interrupted
craffit 5c9a70e
STAC-24630: Make sure to scale up after a failed restore
craffit 14eb630
STAC-24630: Some small fixes
craffit eec5a85
STAC-24630: order stackgraph backups by name, not S3 LastModified
viliakov b692f9e
STAC-25639: retry transient port-forward setup failures (#35)
viliakov 0b157e1
STAC-25639: elasticsearch restore wait until all shards and indices a…
viliakov caac769
Merge remote-tracking branch 'origin/main' into stac-24630
craffit 239b415
STAC-25517: Fix success message on check-and-finalize
craffit ecf5bf9
Merge remote-tracking branch 'origin/main' into stac-24630
craffit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package stackgraphv2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackvista/stackstate-backup-cli/cmd/cmdutils" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/app" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/clients/k8s" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/config" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/logger" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/restore" | ||
| ) | ||
|
|
||
| const ( | ||
| abortNameTemplate = "stackgraph-v2-abort" | ||
| abortScript = "/backup-restore-scripts/restore-stackgraph-backup-v2-abort.sh" | ||
| ) | ||
|
|
||
| func abortCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "abort", | ||
| Short: "Abort a partially backfilled restore.", | ||
| Long: "Abort a partially backfilled restore. This will not load (backfill) any data but keep the data as is." + | ||
| "Be aware this leaves the restore incomplete, historical data will not be recovered, but leave the instance in a usable state." + | ||
| "This should be used when backfilling is failing and the database restore is accepted as-is.", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| cmdutils.Run(globalFlags, runAbort, cmdutils.StorageIsRequired) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runAbort(appCtx *app.Context) error { | ||
| // Setup Kubernetes resources for restore job | ||
| appCtx.Logger.Println() | ||
| if err := restore.EnsureResources(appCtx.K8sClient, appCtx.Namespace, appCtx.Config, appCtx.Logger); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create restore job | ||
| appCtx.Logger.Println() | ||
| appCtx.Logger.Infof("Creating abort job.") | ||
|
|
||
| jobName := fmt.Sprintf("%s-%s", abortNameTemplate, time.Now().Format("20060102t150405")) | ||
|
|
||
| if err := createAbortJob(appCtx.K8sClient, appCtx.Namespace, jobName, appCtx.Config); err != nil { | ||
| return fmt.Errorf("failed to create abort job: %w", err) | ||
| } | ||
|
|
||
| appCtx.Logger.Successf("Abort job created: %s", jobName) | ||
|
|
||
| err := waitAndCleanupAbortJob(appCtx.K8sClient, appCtx.Namespace, jobName, appCtx.Logger) | ||
|
|
||
| if err != nil { | ||
| logAfterJobResult(appCtx.Logger, jobName, false) | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // waitAndCleanupAbortJob waits for job completion and cleans up resources | ||
| func waitAndCleanupAbortJob(k8sClient *k8s.Client, namespace, jobName string, log *logger.Logger) error { | ||
| restore.PrintWaitingMessage(log, "stackgraph-v2", jobName, namespace) | ||
| return restore.WaitAndCleanup(k8sClient, namespace, jobName, log, false) | ||
| } | ||
|
|
||
| // createAbortJob creates a Kubernetes Job for aborting a partially backfilled restore | ||
| func createAbortJob(k8sClient *k8s.Client, namespace, jobName string, config *config.Config) error { | ||
| return createSimpleJob(k8sClient, namespace, jobName, "abort", abortScript, config) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package stackgraphv2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackvista/stackstate-backup-cli/cmd/cmdutils" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/app" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/clients/k8s" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/config" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/logger" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/restore" | ||
| ) | ||
|
|
||
| const ( | ||
| backfillNameTemplate = "stackgraph-v2-backfill" | ||
| backfillScript = "/backup-restore-scripts/restore-stackgraph-backup-v2-backfill.sh" | ||
| ) | ||
|
|
||
| func backfillCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "backfill", | ||
|
craffit marked this conversation as resolved.
|
||
| Short: "Complete an interrupted restore by backfilling old data", | ||
| Long: "Complete an interrupted restore by backfilling old data. This process can run while the system is already up and running." + | ||
| "After the backfill is done, the restore process is complete. " + | ||
| "During a normal restore the restore command will take care of backfilling the data, but if that gets interrupted (Ctrl-C) " + | ||
| "or somehow crashes due to cluster instability, this command allows retrying the backfill portion of the restore.", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| cmdutils.Run(globalFlags, runBackfill, cmdutils.StorageIsRequired) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runBackfill(appCtx *app.Context) error { | ||
| // Setup Kubernetes resources for restore job | ||
| appCtx.Logger.Println() | ||
| if err := restore.EnsureResources(appCtx.K8sClient, appCtx.Namespace, appCtx.Config, appCtx.Logger); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create restore job | ||
| appCtx.Logger.Println() | ||
| appCtx.Logger.Infof("Creating backfill job.") | ||
|
|
||
| jobName := fmt.Sprintf("%s-%s", backfillNameTemplate, time.Now().Format("20060102t150405")) | ||
|
|
||
| if err := createBackfillJob(appCtx.K8sClient, appCtx.Namespace, jobName, appCtx.Config); err != nil { | ||
| return fmt.Errorf("failed to create backfill job: %w", err) | ||
| } | ||
|
|
||
| appCtx.Logger.Successf("Backfill job created: %s", jobName) | ||
|
|
||
| err := waitAndCleanupBackfillJob(appCtx.K8sClient, appCtx.Namespace, jobName, appCtx.Logger) | ||
|
|
||
| if err != nil { | ||
| logAfterJobResult(appCtx.Logger, jobName, false) | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // waitAndCleanupBackfillJob waits for job completion and cleans up resources | ||
| func waitAndCleanupBackfillJob(k8sClient *k8s.Client, namespace, jobName string, log *logger.Logger) error { | ||
| restore.PrintWaitingMessage(log, "stackgraph-v2", jobName, namespace) | ||
| return restore.WaitAndCleanup(k8sClient, namespace, jobName, log, false) | ||
| } | ||
|
|
||
| // createBackfillJob creates a Kubernetes Job for backfilling old data during a restore | ||
| func createBackfillJob(k8sClient *k8s.Client, namespace, jobName string, config *config.Config) error { | ||
| return createSimpleJob(k8sClient, namespace, jobName, "backfill", backfillScript, config) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package stackgraphv2 | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/stackvista/stackstate-backup-cli/cmd/cmdutils" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/app" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/config" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/restore" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/scale" | ||
| ) | ||
|
|
||
| // Check and finalize command flags | ||
| var ( | ||
| checkJobName string | ||
| waitForJob bool | ||
| ) | ||
|
|
||
| func checkAndFinalizeCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "check-and-finalize", | ||
| Short: "Check and finalize a Stackgraph restore (v2) job", | ||
| Long: `Check the status of a background Stackgraph restore job and clean up resources. | ||
|
|
||
| This command is useful when a restore/backfill/abort job was interrupted (Ctrl+C). | ||
| It will check the job status, print logs if it failed, and clean up the job and PVC resources. | ||
|
|
||
| Examples: | ||
| # Check job status without waiting | ||
| sts-backup stackgraph-v2 check-and-finalize --job stackgraph-v2-restore-20250128t143000 -n my-namespace | ||
|
|
||
| # Wait for job completion and cleanup | ||
| sts-backup stackgraph-v2 check-and-finalize --job stackgraph-v2-restore-20250128t143000 --wait -n my-namespace`, | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| cmdutils.Run(globalFlags, runCheckAndFinalize, cmdutils.StorageIsRequired) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&checkJobName, "job", "j", "", "Stackgraph restore job name (required)") | ||
| cmd.Flags().BoolVarP(&waitForJob, "wait", "w", false, "Wait for job to complete before cleanup") | ||
| _ = cmd.MarkFlagRequired("job") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runCheckAndFinalize(appCtx *app.Context) error { | ||
| err := restore.CheckAndFinalize(restore.CheckAndFinalizeParams{ | ||
| K8sClient: appCtx.K8sClient, | ||
| Namespace: appCtx.Namespace, | ||
| JobName: checkJobName, | ||
| ServiceName: "stackgraph-v2", | ||
| ScaleUpFn: scale.ScaleUpAndReleaseLock, | ||
| ScaleDownFn: scale.ScaleDown, | ||
| ScaleSelector: appCtx.Config.Stackgraph.Restore.ScaleDownLabelSelector, | ||
| CleanupPVC: true, | ||
| WaitForJob: waitForJob, | ||
| Log: appCtx.Logger, | ||
| }) | ||
| logAfterJobResult(appCtx.Logger, checkJobName, err == nil) | ||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package stackgraphv2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/service/s3" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stackvista/stackstate-backup-cli/cmd/cmdutils" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/app" | ||
| s3client "github.com/stackvista/stackstate-backup-cli/internal/clients/s3" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/config" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/output" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward" | ||
| ) | ||
|
|
||
| const ( | ||
| backupFileNameRegex = `^sts-backup-.*\.graph.v2$` | ||
| ) | ||
|
|
||
| func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List available Stackgraph backups (v2) from S3", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| cmdutils.Run(globalFlags, runList, cmdutils.StorageIsRequired) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func runList(appCtx *app.Context) error { | ||
| // Setup port-forward to S3-compatible storage | ||
| storageService := appCtx.Config.GetStorageService() | ||
| serviceName := storageService.Name | ||
| remotePort := storageService.Port | ||
|
|
||
| pf, err := portforward.SetupPortForward(appCtx.K8sClient, appCtx.Namespace, serviceName, remotePort, appCtx.Logger) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer close(pf.StopChan) | ||
|
|
||
| // Create S3 client with actual port | ||
| s3Client, err := appCtx.NewS3Client(pf.LocalPort) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create S3 client: %w", err) | ||
| } | ||
|
|
||
| // List objects in bucket | ||
| bucket := appCtx.Config.Stackgraph.Bucket | ||
| prefix := appCtx.Config.Stackgraph.S3Prefix + "v2/" | ||
|
|
||
| appCtx.Logger.Infof("Listing Stackgraph backups in bucket '%s' with prefix '%s'...", bucket, prefix) | ||
|
|
||
| input := &s3.ListObjectsV2Input{ | ||
| Bucket: aws.String(bucket), | ||
| Prefix: aws.String(prefix), | ||
| Delimiter: aws.String("/"), | ||
| } | ||
|
|
||
| result, err := s3Client.ListObjectsV2(context.Background(), input) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list S3 objects: %w", err) | ||
| } | ||
|
|
||
| filteredObjects := s3client.ConvertBackupObjects(result.Contents) | ||
|
|
||
| // Filter to only include direct children of the prefix that match the backup filename pattern, | ||
| // and strip the prefix from the key | ||
| filteredObjects, err = s3client.FilterByPrefixAndRegex(filteredObjects, prefix, backupFileNameRegex) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to filter objects: %w", err) | ||
| } | ||
|
|
||
| s3client.SortByKeyDescending(filteredObjects) | ||
|
|
||
| if len(filteredObjects) == 0 { | ||
| appCtx.Formatter.PrintMessage("No backups found") | ||
| return nil | ||
| } | ||
|
|
||
| table := output.Table{ | ||
| Headers: []string{"NAME", "LAST MODIFIED"}, | ||
| Rows: make([][]string, 0, len(filteredObjects)), | ||
| } | ||
|
|
||
| for _, obj := range filteredObjects { | ||
| row := []string{ | ||
| strings.TrimPrefix(obj.Key, prefix), | ||
| obj.LastModified.Format("2006-01-02 15:04:05 MST"), | ||
| } | ||
| table.Rows = append(table.Rows, row) | ||
| } | ||
|
|
||
| return appCtx.Formatter.PrintTable(table) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.