Skip to content

Commit

Permalink
add backup-ns list subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
majodev committed Jan 9, 2025
1 parent fcd03be commit 9b142a3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ kubectl label vs/<vs> "backup-ns.sh/delete-after"="YYYY-MM-DD"
Here are some typical volume snapshot list commands based on that labels:

```bash
# List application-aware volume snapshots overview in the current namespace
backup-ns list

# All namespaces
backup-ns list -A

# Filter by daily, weekly, monthly and adhoc:
backup-ns list --daily
backup-ns list --weekly
backup-ns list --monthly
backup-ns list --adhoc

# Under the hook the above commands directly translate to the following kubectl commands.
# It's all based on labels.
# Feel free to use kubectl directly for more advanced queries.

# List application-aware volume snapshots overview all namespaces
kubectl get vs -lbackup-ns.sh/retain -Lbackup-ns.sh/type,backup-ns.sh/retain,backup-ns.sh/daily,backup-ns.sh/weekly,backup-ns.sh/monthly,backup-ns.sh/delete-after --all-namespaces

Expand Down
85 changes: 85 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cmd

import (
"fmt"
"log"
"os/exec"

"github.com/allaboutapps/backup-ns/internal/lib"
"github.com/spf13/cobra"
)

var (
allNamespaces bool
filterDaily bool
filterWeekly bool
filterMonthly bool
filterAdhoc bool
)

var vsListCmd = &cobra.Command{
Use: "list",
Short: "List volume snapshots with backup-ns labels",
Run: func(_ *cobra.Command, args []string) {
namespace := ""
if !allNamespaces {
var err error
namespace, err = lib.GetCurrentNamespace()
if err != nil {
log.Fatal(err)
}
}

// Build label selector
labelSelector := "backup-ns.sh/retain"
if filterDaily {
labelSelector += ",backup-ns.sh/daily"
}
if filterWeekly {
labelSelector += ",backup-ns.sh/weekly"
}
if filterMonthly {
labelSelector += ",backup-ns.sh/monthly"
}
if filterAdhoc {
labelSelector += ",backup-ns.sh/type=adhoc"
}

// Build kubectl command
args = []string{
"get",
"vs",
"-l" + labelSelector,
"-Lbackup-ns.sh/type,backup-ns.sh/retain,backup-ns.sh/daily,backup-ns.sh/weekly,backup-ns.sh/monthly,backup-ns.sh/delete-after",
}

if allNamespaces {
args = append(args, "--all-namespaces")
} else if namespace != "" {
args = append(args, "-n", namespace)
}

// #nosec G204
cmd := exec.Command("kubectl", args...)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Failed to list volume snapshots: %v\nOutput: %s", err, output)
}

if namespace != "" {
fmt.Printf("Namespace: %s\n", namespace)
}

fmt.Printf("Listing volume snapshots with labels: %s\n", labelSelector)
fmt.Print(string(output))
},
}

func init() {
rootCmd.AddCommand(vsListCmd)
vsListCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "List volume snapshots in all namespaces")
vsListCmd.Flags().BoolVar(&filterDaily, "daily", false, "Filter daily snapshots")
vsListCmd.Flags().BoolVar(&filterWeekly, "weekly", false, "Filter weekly snapshots")
vsListCmd.Flags().BoolVar(&filterMonthly, "monthly", false, "Filter monthly snapshots")
vsListCmd.Flags().BoolVar(&filterAdhoc, "adhoc", false, "Filter adhoc snapshots")
}
2 changes: 2 additions & 0 deletions test/test_create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ BAK_DB_MYSQL=true BAK_NAMESPACE=mysql-test BAK_DB_MYSQL_EXEC_RESOURCE=deployment
BAK_DB_MYSQL=true BAK_NAMESPACE=mysql-test BAK_DB_MYSQL_EXEC_RESOURCE=deployment/mysql backup-ns mysql downloadDump -o "$SCRIPT_DIR/mysql-test.tar.gz"
rm -f "$SCRIPT_DIR/mysql-test.tar.gz"
BAK_DB_MYSQL=true BAK_NAMESPACE=mysql-test BAK_DB_MYSQL_EXEC_RESOURCE=deployment/mysql backup-ns mysql restore -f

backup-ns list -A

0 comments on commit 9b142a3

Please sign in to comment.