|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/fly-apps/postgres-flex/internal/flypg" |
| 10 | + "github.com/fly-apps/postgres-flex/internal/flypg/state" |
| 11 | + "github.com/olekukonko/tablewriter" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var backupListCmd = &cobra.Command{ |
| 16 | + Use: "list", |
| 17 | + Short: "Lists all backups", |
| 18 | + Long: `Lists all available backups created.`, |
| 19 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 20 | + if !backupsEnabled() { |
| 21 | + return fmt.Errorf("backups are not enabled") |
| 22 | + } |
| 23 | + |
| 24 | + return listBackups(cmd) |
| 25 | + }, |
| 26 | + Args: cobra.NoArgs, |
| 27 | +} |
| 28 | + |
| 29 | +var backupCreateCmd = &cobra.Command{ |
| 30 | + Use: "create", |
| 31 | + Short: "Creates a new backup", |
| 32 | + Long: `Creates a new backup.`, |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + if !backupsEnabled() { |
| 35 | + return fmt.Errorf("backups are not enabled") |
| 36 | + } |
| 37 | + |
| 38 | + if err := createBackup(cmd); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + fmt.Println("Backup completed successfully!") |
| 43 | + |
| 44 | + return nil |
| 45 | + }, |
| 46 | + Args: cobra.NoArgs, |
| 47 | +} |
| 48 | + |
| 49 | +var backupShowCmd = &cobra.Command{ |
| 50 | + Use: "show", |
| 51 | + Short: "Shows details about a specific backup", |
| 52 | + Long: `Shows details about a specific backup.`, |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + if !backupsEnabled() { |
| 55 | + return fmt.Errorf("backups are not enabled") |
| 56 | + } |
| 57 | + return showBackup(cmd, args) |
| 58 | + }, |
| 59 | + Args: cobra.ExactArgs(1), |
| 60 | +} |
| 61 | + |
| 62 | +func showBackup(cmd *cobra.Command, args []string) error { |
| 63 | + id := args[0] |
| 64 | + |
| 65 | + ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second) |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + store, err := state.NewStore() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to initialize store: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + barman, err := flypg.NewBarman(store, os.Getenv("S3_ARCHIVE_CONFIG"), flypg.DefaultAuthProfile) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to initialize barman: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + backupDetails, err := barman.ShowBackup(ctx, id) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to get backup details: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + fmt.Println(string(backupDetails)) |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func createBackup(cmd *cobra.Command) error { |
| 89 | + ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute) |
| 90 | + defer cancel() |
| 91 | + |
| 92 | + n, err := flypg.NewNode() |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to initialize node: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + conn, err := n.RepMgr.NewLocalConnection(ctx) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to connect to local db: %v", err) |
| 100 | + } |
| 101 | + defer func() { _ = conn.Close(ctx) }() |
| 102 | + |
| 103 | + isPrimary, err := n.RepMgr.IsPrimary(ctx, conn) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to determine if node is primary: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + if !isPrimary { |
| 109 | + return fmt.Errorf("backups can only be performed against the primary node") |
| 110 | + } |
| 111 | + |
| 112 | + store, err := state.NewStore() |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("failed to initialize store: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + barman, err := flypg.NewBarman(store, os.Getenv("S3_ARCHIVE_CONFIG"), flypg.DefaultAuthProfile) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("failed to initialize barman: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + name, err := cmd.Flags().GetString("name") |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("failed to get name flag: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + immediateCheckpoint, err := cmd.Flags().GetBool("immediate-checkpoint") |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("failed to get immediate-checkpoint flag: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + cfg := flypg.BackupConfig{ |
| 133 | + ImmediateCheckpoint: immediateCheckpoint, |
| 134 | + Name: name, |
| 135 | + } |
| 136 | + |
| 137 | + fmt.Println("Performing backup...") |
| 138 | + |
| 139 | + if _, err := barman.Backup(ctx, cfg); err != nil { |
| 140 | + return fmt.Errorf("failed to create backup: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func listBackups(cmd *cobra.Command) error { |
| 147 | + ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second) |
| 148 | + defer cancel() |
| 149 | + |
| 150 | + store, err := state.NewStore() |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("failed to initialize store: %v", err) |
| 153 | + } |
| 154 | + |
| 155 | + barman, err := flypg.NewBarman(store, os.Getenv("S3_ARCHIVE_CONFIG"), flypg.DefaultAuthProfile) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("failed to initialize barman: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + isJSON, err := cmd.Flags().GetBool("json") |
| 161 | + if err != nil { |
| 162 | + return fmt.Errorf("failed to get json flag: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + if isJSON { |
| 166 | + jsonBytes, err := barman.ListRawBackups(cmd.Context()) |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("failed to list backups: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + fmt.Println(string(jsonBytes)) |
| 172 | + return nil |
| 173 | + } |
| 174 | + |
| 175 | + backupList, err := barman.ListBackups(ctx) |
| 176 | + if err != nil { |
| 177 | + return fmt.Errorf("failed to list backups: %v", err) |
| 178 | + } |
| 179 | + |
| 180 | + if len(backupList.Backups) == 0 { |
| 181 | + fmt.Println("No backups found") |
| 182 | + return nil |
| 183 | + } |
| 184 | + |
| 185 | + var filterStatus string |
| 186 | + |
| 187 | + filterStatus, err = cmd.Flags().GetString("status") |
| 188 | + if err != nil { |
| 189 | + return fmt.Errorf("failed to get status flag: %v", err) |
| 190 | + } |
| 191 | + |
| 192 | + table := tablewriter.NewWriter(os.Stdout) |
| 193 | + table.SetHeader([]string{"ID", "Name", "Status", "End time", "Begin WAL"}) |
| 194 | + |
| 195 | + // Set table alignment, borders, padding, etc. as needed |
| 196 | + table.SetAlignment(tablewriter.ALIGN_LEFT) |
| 197 | + table.SetBorder(true) // Set to false to hide borders |
| 198 | + table.SetCenterSeparator("|") |
| 199 | + table.SetColumnSeparator("|") |
| 200 | + table.SetRowSeparator("-") |
| 201 | + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) |
| 202 | + table.SetHeaderLine(true) // Enable header line |
| 203 | + table.SetAutoWrapText(false) |
| 204 | + |
| 205 | + for _, b := range backupList.Backups { |
| 206 | + if filterStatus != "" && b.Status != filterStatus { |
| 207 | + continue |
| 208 | + } |
| 209 | + |
| 210 | + table.Append([]string{ |
| 211 | + b.ID, |
| 212 | + b.Name, |
| 213 | + b.Status, |
| 214 | + b.EndTime, |
| 215 | + b.BeginWal, |
| 216 | + }) |
| 217 | + } |
| 218 | + |
| 219 | + table.Render() |
| 220 | + |
| 221 | + return nil |
| 222 | +} |
| 223 | + |
| 224 | +func backupsEnabled() bool { |
| 225 | + return os.Getenv("S3_ARCHIVE_CONFIG") != "" |
| 226 | +} |
0 commit comments