Skip to content

Commit d1933af

Browse files
feat: add authentik blueprint export to backup command
- Added new --export flag to support exporting supplemental data (currently authentik-blueprint) - Added --output-dir flag to customize backup/export destination directory - Refactored directory creation logic to handle both backup and export cases - Improved error handling and logging for directory operations - Moved hardcoded /opt/mnt path to configurable flag with default value
1 parent b780345 commit d1933af

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

cmd/backup/hecate.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"time"
99

10+
"github.com/CodeMonkeyCybersecurity/eos/pkg/authentik"
1011
"github.com/CodeMonkeyCybersecurity/eos/pkg/eos_cli"
1112
"github.com/CodeMonkeyCybersecurity/eos/pkg/eos_io"
1213
"github.com/spf13/cobra"
@@ -21,30 +22,51 @@ var BackupHecateCmd = &cobra.Command{
2122
RunE: eos_cli.Wrap(func(rc *eos_io.RuntimeContext, cmd *cobra.Command, args []string) error {
2223
log := otelzap.Ctx(rc.Ctx)
2324

25+
exportTarget, _ := cmd.Flags().GetString("export")
26+
outputDir, _ := cmd.Flags().GetString("output-dir")
27+
28+
// Ensure output directory exists before performing any operations.
29+
if outputDir == "" {
30+
outputDir = "/opt/mnt"
31+
}
32+
if err := os.MkdirAll(outputDir, 0o755); err != nil {
33+
log.Error("Failed to create destination directory", zap.Error(err))
34+
return fmt.Errorf("failed to create %s: %w", outputDir, err)
35+
}
36+
37+
if exportTarget != "" {
38+
switch exportTarget {
39+
case "authentik-blueprint":
40+
log.Info("Exporting Authentik blueprint",
41+
zap.String("destination_dir", outputDir))
42+
43+
blueprintPath, err := authentik.ExportBlueprintToDirectory(rc, outputDir)
44+
if err != nil {
45+
log.Error("Failed to export Authentik blueprint", zap.Error(err))
46+
return err
47+
}
48+
49+
log.Info("Authentik blueprint export completed",
50+
zap.String("blueprint_path", blueprintPath))
51+
return nil
52+
default:
53+
return fmt.Errorf("unsupported export target: %s", exportTarget)
54+
}
55+
}
56+
2457
// Define source and destination
2558
sourceDir := "/opt/hecate"
26-
destDir := "/opt/mnt"
2759

2860
// Ensure /opt/hecate exists
2961
if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
3062
log.Error("Source directory does not exist", zap.String("path", sourceDir))
3163
return fmt.Errorf("source directory %s does not exist", sourceDir)
3264
}
3365

34-
// Ensure /opt/mnt exists, create if not
35-
if _, err := os.Stat(destDir); os.IsNotExist(err) {
36-
log.Info("Destination directory does not exist, creating...", zap.String("path", destDir))
37-
if err := os.MkdirAll(destDir, 0755); err != nil {
38-
log.Error("Failed to create destination directory", zap.Error(err))
39-
return fmt.Errorf("failed to create %s: %w", destDir, err)
40-
}
41-
log.Info(" Destination directory created", zap.String("path", destDir))
42-
}
43-
4466
// Prepare timestamped backup filename
4567
timestamp := time.Now().Format("20060102_150405")
4668
backupFileName := fmt.Sprintf("%s_hecate_backup.tar.gz", timestamp)
47-
backupFilePath := filepath.Join(destDir, backupFileName)
69+
backupFilePath := filepath.Join(outputDir, backupFileName)
4870

4971
log.Info("Starting backup...",
5072
zap.String("source", sourceDir),
@@ -72,5 +94,7 @@ var BackupHecateCmd = &cobra.Command{
7294
}
7395

7496
func init() {
97+
BackupHecateCmd.Flags().String("export", "", "Export supplemental data instead of creating archive (options: authentik-blueprint)")
98+
BackupHecateCmd.Flags().String("output-dir", "/opt/mnt", "Destination directory for backups or exports")
7599
BackupCmd.AddCommand(BackupHecateCmd)
76100
}

0 commit comments

Comments
 (0)