You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by mi...@apache.org on 2023/03/02 02:31:58 UTC
[shardingsphere-on-cloud] branch main updated: feat(pitr):agent restore api (#234)
This is an automated email from the ASF dual-hosted git repository.
miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git
The following commit(s) were added to refs/heads/main by this push:
new 69da154 feat(pitr):agent restore api (#234)
69da154 is described below
commit 69da15424e4d3b2233ba948023b7fcb7906e05b4
Author: lltgo <ll...@foxmail.com>
AuthorDate: Thu Mar 2 10:31:52 2023 +0800
feat(pitr):agent restore api (#234)
* feat(pitr):agent restore api
* chore:rm comments
* chore: add todo
---
.gitignore | 1 +
pitr/agent/internal/cons/error.go | 1 +
pitr/agent/internal/handler/restore.go | 21 ++++++++++++++++-
pitr/agent/internal/handler/show.go | 2 +-
pitr/agent/internal/handler/view/show.go | 40 ++++++++++++++++++++++++++++++++
pitr/agent/internal/pkg/opengauss.go | 16 ++++++++++---
pitr/agent/pkg/cmds/cmd.go | 4 ++--
7 files changed, 78 insertions(+), 7 deletions(-)
diff --git a/.gitignore b/.gitignore
index 1048a23..6fc2d05 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,4 @@ charts/apache-shardingsphere-operator-charts/charts/
test
certs
shardingsphere-operator/vendor/
+pitr/agent/agent
diff --git a/pitr/agent/internal/cons/error.go b/pitr/agent/internal/cons/error.go
index a86c0be..676fd11 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -46,4 +46,5 @@ var (
MissingDbName = xerror.New(10021, "Missing db name.")
DbConnectionFailed = xerror.New(10022, "Database connection failed.")
UnmatchBackupID = xerror.New(10023, "Unmatch any backup id.")
+ InvalidPgDataDir = xerror.New(10024, "Invalid PGDATA dir.")
)
diff --git a/pitr/agent/internal/handler/restore.go b/pitr/agent/internal/handler/restore.go
index 66f0043..be4c2f6 100644
--- a/pitr/agent/internal/handler/restore.go
+++ b/pitr/agent/internal/handler/restore.go
@@ -19,6 +19,7 @@ package handler
import (
"fmt"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/gofiber/fiber/v2"
@@ -34,7 +35,25 @@ func Restore(ctx *fiber.Ctx) error {
}
if err := in.Validate(); err != nil {
- return err
+ return fmt.Errorf("invalid parameter,err=%w", err)
+ }
+
+ if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort); err != nil {
+ efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
+ return fmt.Errorf(efmt, in.Username, len(in.Password), in.DbName, err)
+ }
+
+ if err := pkg.OG.Stop(); err != nil {
+ return fmt.Errorf("stop openGauss failure,err=%w", err)
+ }
+
+ if err := pkg.OG.Restore(in.DnBackupPath, in.Instance, in.DnBackupId); err != nil {
+ efmt := "pkg.OG.Restore failure[path=%s,instance=%s,backupID=%s],err=%w"
+ return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupId, err)
+ }
+
+ if err := pkg.OG.Start(); err != nil {
+ return fmt.Errorf("stop openGauss failure,err=%w", err)
}
return ctx.JSON(in)
diff --git a/pitr/agent/internal/handler/show.go b/pitr/agent/internal/handler/show.go
index 028073d..076eb7f 100644
--- a/pitr/agent/internal/handler/show.go
+++ b/pitr/agent/internal/handler/show.go
@@ -55,7 +55,7 @@ func Show(ctx *fiber.Ctx) error {
}
func ShowList(ctx *fiber.Ctx) error {
- in := &view.ShowIn{}
+ in := &view.ShowListIn{}
if err := ctx.BodyParser(in); err != nil {
return fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
diff --git a/pitr/agent/internal/handler/view/show.go b/pitr/agent/internal/handler/view/show.go
index c2643f9..6ebcfb2 100644
--- a/pitr/agent/internal/handler/view/show.go
+++ b/pitr/agent/internal/handler/view/show.go
@@ -33,6 +33,15 @@ type (
Instance string `json:"instance"`
}
+ ShowListIn struct {
+ DbPort uint16 `json:"db_port"`
+ DbName string `json:"db_name"`
+ Username string `json:"username"`
+ Password string `json:"password"`
+ DnBackupPath string `json:"dn_backup_path"`
+ Instance string `json:"instance"`
+ }
+
BackupInfo struct {
Id string `json:"dn_backup_id"`
Path string `json:"dn_backup_path"`
@@ -125,3 +134,34 @@ func statusTrans(status string) string {
return "Other"
}
}
+
+func (in *ShowListIn) Validate() error {
+ if in == nil {
+ return cons.Internal
+ }
+
+ if in.DbPort == 0 {
+ return cons.InvalidDbPort
+ }
+
+ if in.DbName == "" {
+ return cons.MissingDbName
+ }
+
+ if in.Username == "" {
+ return cons.MissingUsername
+ }
+
+ if in.Password == "" {
+ return cons.MissingPassword
+ }
+
+ if in.DnBackupPath == "" {
+ return cons.MissingDnBackupPath
+ }
+
+ if in.Instance == "" {
+ return cons.MissingInstance
+ }
+ return nil
+}
diff --git a/pitr/agent/internal/pkg/opengauss.go b/pitr/agent/internal/pkg/opengauss.go
index da44642..5d7b333 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -64,8 +64,8 @@ const (
_delBackupFmt = "gs_probackup delete --backup-path=%s --instance=%s --backup-id=%s 2>&1"
_restoreFmt = "gs_probackup restore --backup-path=%s --instance=%s --backup-id=%s --pgdata=%s 2>&1"
- _initFmt = "gs_probackup init --backup-path=%s 2>&1"
- _deinitFmt = "rm -r %s"
+ _initFmt = "gs_probackup init --backup-path=%s 2>&1"
+ _rmDirFmt = "rm -r %s"
_addInstanceFmt = "gs_probackup add-instance --backup-path=%s --instance=%s --pgdata=%s 2>&1"
_delInstanceFmt = "gs_probackup del-instance --backup-path=%s --instance=%s 2>&1"
@@ -152,7 +152,7 @@ func (og *openGauss) deinit(backupPath string) error {
return cons.NoPermission
}
- cmd := fmt.Sprintf(_deinitFmt, backupPath)
+ cmd := fmt.Sprintf(_rmDirFmt, backupPath)
if _, err := cmds.Exec(og.shell, cmd); err != nil {
return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
}
@@ -213,10 +213,20 @@ func (og *openGauss) Stop() error {
// Restore TODO:Dependent environments require integration testing
func (og *openGauss) Restore(backupPath, instance, backupID string) error {
+ if len(og.pgData) < 2 && strings.HasPrefix(og.pgData, "/") {
+ return fmt.Errorf("invalid pg data dir[path=%s],err=%w", og.pgData, cons.InvalidPgDataDir)
+ }
+
+ if _, err := cmds.Exec(og.shell, fmt.Sprintf(_rmDirFmt, og.pgData)); err != nil {
+ return fmt.Errorf("rm PGDATA dir failure,err=%s,wrap=%s", err, cons.RestoreFailed)
+ }
+
cmd := fmt.Sprintf(_restoreFmt, backupPath, instance, backupID, og.pgData)
outputs, err := cmds.AsyncExec(og.shell, cmd)
for output := range outputs {
+ // TODO just for dev,rm in next commit
+ fmt.Println(output.Message)
if errors.Is(err, cons.CmdOperateFailed) {
return fmt.Errorf("outputs get err=%s,wrap=%w", output.Error, cons.RestoreFailed)
}
diff --git a/pitr/agent/pkg/cmds/cmd.go b/pitr/agent/pkg/cmds/cmd.go
index 86dc1de..21b1be6 100644
--- a/pitr/agent/pkg/cmds/cmd.go
+++ b/pitr/agent/pkg/cmds/cmd.go
@@ -72,9 +72,9 @@ func AsyncExec(name string, args ...string) (chan *Output, error) {
}
if err = cmd.Wait(); err != nil {
- if _, ok := err.(*exec.ExitError); ok {
+ if ee, ok := err.(*exec.ExitError); ok {
output <- &Output{
- Error: cons.CmdOperateFailed,
+ Error: fmt.Errorf("exec failure[ee=%s],wrap=%w", ee, cons.CmdOperateFailed),
}
} else {
output <- &Output{