Skip to content

Commit 8c404e1

Browse files
committed
add support for restore command
1 parent a75131e commit 8c404e1

File tree

6 files changed

+62
-155
lines changed

6 files changed

+62
-155
lines changed

cmd/pgbackrest-server/api/info.go

Lines changed: 0 additions & 119 deletions
This file was deleted.

cmd/pgbackrest-server/api/stanza_create.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/pgbackrest-server/api/stanza_upgrade.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/pgbackrest-server/exec/pgbackrest.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

cmd/pgbackrest-server/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66

77
"github.com/gorilla/mux"
8-
"github.com/superfly/percona-postgresql-operator/cmd/pgbackrest-server/api"
8+
"github.com/superfly/percona-postgresql-operator/internal/pgbackrest-server/api"
99
)
1010

1111
func main() {
@@ -14,6 +14,8 @@ func main() {
1414

1515
// Endpoints
1616
r.HandleFunc("/info", api.InfoCommandHandler).Methods("POST")
17+
r.HandleFunc("/backup", api.BackupCommandHandler).Methods("POST")
18+
r.HandleFunc("/restore", api.RestoreCommandHandler).Methods("POST")
1719

1820
http.Handle("/", r)
1921
err := http.ListenAndServe(":4422", nil)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"reflect"
8+
9+
pgbackrestserver "github.com/superfly/percona-postgresql-operator/internal/pgbackrest-server"
10+
)
11+
12+
type RestoreCommandOptions struct {
13+
// General Options
14+
Stanza string `json:"stanza"` // Required
15+
Set string `json:"set,omitempty"`
16+
Delta bool `json:"delta,omitempty"`
17+
Force bool `json:"force,omitempty"`
18+
Repo int `json:"repo,omitempty"`
19+
20+
// Filter Options
21+
DBInclude []string `json:"db_include,omitempty"`
22+
23+
// Target & Recovery Options (Point-in-Time Recovery)
24+
TargetType string `json:"target_type,omitempty"` // e.g., 'time', 'xid', 'lsn'
25+
Target string `json:"target,omitempty"`
26+
TargetAction string `json:"target_action,omitempty"` // e.g., 'pause', 'promote', 'shutdown'
27+
RecoveryOption map[string]string `json:"recovery_option,omitempty"`
28+
}
29+
30+
func (c *RestoreCommandOptions) AsSlice() []string {
31+
return pgbackrestserver.IntoOpts(reflect.ValueOf(c))
32+
}
33+
34+
func RestoreCommandHandler(w http.ResponseWriter, r *http.Request) {
35+
var opts RestoreCommandOptions
36+
if err := json.NewDecoder(r.Body).Decode(&opts); err != nil {
37+
http.Error(w, fmt.Sprintf("could not decode json body: %v", err), http.StatusBadRequest)
38+
return
39+
}
40+
41+
cmd := pgbackrestserver.BackrestCommand{
42+
Command: "restore",
43+
Opts: opts.AsSlice(),
44+
}
45+
46+
stdout, _, err := cmd.Run(r.Context())
47+
if err != nil {
48+
http.Error(w, fmt.Sprintf("failed to run pgbackrest restore command: %v", err), http.StatusInternalServerError)
49+
return
50+
}
51+
52+
out := map[string]string{"data": stdout.String()}
53+
w.WriteHeader(http.StatusOK)
54+
if err := json.NewEncoder(w).Encode(out); err != nil {
55+
http.Error(w, fmt.Sprintf("failed to encode json response: %v", err), http.StatusInternalServerError)
56+
return
57+
}
58+
59+
}

0 commit comments

Comments
 (0)