From dd3fb63729c3af0bfdea1df6219739aa041b0853 Mon Sep 17 00:00:00 2001 From: mlycore Date: Fri, 17 Nov 2023 18:57:12 +0800 Subject: [PATCH] refactor: add backup set size validation Signed-off-by: mlycore --- pitr/cli/internal/cmd/common.go | 9 ++++- pitr/cli/internal/cmd/delete.go | 65 ++++++++++++++----------------- pitr/cli/internal/cmd/restore.go | 66 ++++++++++++++------------------ 3 files changed, 65 insertions(+), 75 deletions(-) diff --git a/pitr/cli/internal/cmd/common.go b/pitr/cli/internal/cmd/common.go index dda2c52a..035dd765 100644 --- a/pitr/cli/internal/cmd/common.go +++ b/pitr/cli/internal/cmd/common.go @@ -31,7 +31,6 @@ func validate(ls pkg.ILocalStorage, csn, recordID string) ([]*model.LsBackup, er err error ) if CSN != "" { - // bak, err = ls.ReadByCSN(csn) baks, err = ls.ReadAllByCSN(csn) if err != nil { return baks, xerr.NewCliErr(fmt.Sprintf("read backup record by csn failed. err: %s", err)) @@ -45,5 +44,13 @@ func validate(ls pkg.ILocalStorage, csn, recordID string) ([]*model.LsBackup, er return baks, xerr.NewCliErr(fmt.Sprintf("read backup record by id failed. err: %s", err)) } } + + if len(baks) == 0 { + return baks, xerr.NewCliErr(fmt.Sprintf("backup record not found. err: %s", err)) + } + if len(baks) > 1 { + return baks, xerr.NewCliErr("multiple backup records found. please using ID to submit one specific record.") + } + return baks, nil } diff --git a/pitr/cli/internal/cmd/delete.go b/pitr/cli/internal/cmd/delete.go index 415b9b76..e5006bc0 100644 --- a/pitr/cli/internal/cmd/delete.go +++ b/pitr/cli/internal/cmd/delete.go @@ -94,49 +94,40 @@ func deleteRecord() error { return err } - if len(baks) == 0 { - return xerr.NewCliErr(fmt.Sprintf("backup record not found. err: %s", err)) + bak := baks[0] + // check agent server status + logging.Info("Checking agent server status...") + if available := checkAgentServerStatus(bak); !available { + return xerr.NewCliErr("one or more agent server are not available.") } - if len(baks) > 1 { - return xerr.NewCliErr("multiple backup records found. please using ID to submit one specific record.") - } - - if len(baks) == 1 { - bak := baks[0] - // check agent server status - logging.Info("Checking agent server status...") - if available := checkAgentServerStatus(bak); !available { - return xerr.NewCliErr("one or more agent server are not available.") - } - prompt := fmt.Sprintf( - "The backup record(ID: %s, CSN: %s) will be deleted forever.\n"+ - "Are you sure to continue? (Y/N)", bak.Info.ID, bak.Info.CSN) - err = getUserApproveInTerminal(prompt) - if err != nil { - return xerr.NewCliErr(fmt.Sprintf("%s", err)) - } - - // mark the target backup record to be deleted - // meanwhile this record cannot be restored - if err := ls.HideByName(bak.Info.FileName); err != nil { - return xerr.NewCliErr("cannot mark backup record.") - } + prompt := fmt.Sprintf( + "The backup record(ID: %s, CSN: %s) will be deleted forever.\n"+ + "Are you sure to continue? (Y/N)", bak.Info.ID, bak.Info.CSN) + err = getUserApproveInTerminal(prompt) + if err != nil { + return xerr.NewCliErr(fmt.Sprintf("%s", err)) + } - // exec delete - logging.Info("Start delete backup data to openGauss...") - if err := _execDelete(bak); err != nil { - return xerr.NewCliErr(fmt.Sprintf("exec delete failed. err: %s", err)) - } - logging.Info("Delete backup data success!") + // mark the target backup record to be deleted + // meanwhile this record cannot be restored + if err := ls.HideByName(bak.Info.FileName); err != nil { + return xerr.NewCliErr("cannot mark backup record.") + } - // delete the backup record - if err := ls.DeleteByHidedName(bak.Info.FileName); err != nil { - return xerr.NewCliErr(fmt.Sprintf("exec delete backup record failed. err: %s", err)) - } + // exec delete + logging.Info("Start delete backup data to openGauss...") + if err := _execDelete(bak); err != nil { + return xerr.NewCliErr(fmt.Sprintf("exec delete failed. err: %s", err)) + } + logging.Info("Delete backup data success!") - logging.Info("Delete success!") + // delete the backup record + if err := ls.DeleteByHidedName(bak.Info.FileName); err != nil { + return xerr.NewCliErr(fmt.Sprintf("exec delete backup record failed. err: %s", err)) } + + logging.Info("Delete success!") return nil } diff --git a/pitr/cli/internal/cmd/restore.go b/pitr/cli/internal/cmd/restore.go index e076dca7..5e7f5926 100644 --- a/pitr/cli/internal/cmd/restore.go +++ b/pitr/cli/internal/cmd/restore.go @@ -104,49 +104,41 @@ func restore() error { if err != nil { return err } - if len(baks) == 0 { - return xerr.NewCliErr(fmt.Sprintf("backup record not found. err: %s", err)) - } - if len(baks) > 1 { - return xerr.NewCliErr("multiple backup records found. please using ID to submit one specific record.") - } - if len(baks) == 1 { - bak := baks[0] - // check if the backup logic database exits, - // if exits, we need to warning user that we will drop the database. - if err := checkDatabaseExist(proxy, bak); err != nil { - return xerr.NewCliErr(fmt.Sprintf("check database exist failed. err: %s", err)) - } + bak := baks[0] + // check if the backup logic database exits, + // if exits, we need to warning user that we will drop the database. + if err := checkDatabaseExist(proxy, bak); err != nil { + return xerr.NewCliErr(fmt.Sprintf("check database exist failed. err: %s", err)) + } - prompt := fmt.Sprintf( - "Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n"+ - "The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n"+ - "Are you sure to continue? (Y/N)", strings.Join(databaseNamesExist, ",")) - err = getUserApproveInTerminal(prompt) - if err != nil { - return xerr.NewCliErr(fmt.Sprintf("%s", err)) - } + prompt := fmt.Sprintf( + "Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n"+ + "The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n"+ + "Are you sure to continue? (Y/N)", strings.Join(databaseNamesExist, ",")) + err = getUserApproveInTerminal(prompt) + if err != nil { + return xerr.NewCliErr(fmt.Sprintf("%s", err)) + } - // check agent server status - logging.Info("Checking agent server status...") - if available := checkAgentServerStatus(bak); !available { - return xerr.NewCliErr("one or more agent server are not available.") - } + // check agent server status + logging.Info("Checking agent server status...") + if available := checkAgentServerStatus(bak); !available { + return xerr.NewCliErr("one or more agent server are not available.") + } - // exec restore - logging.Info("Start restore backup data to openGauss...") - if err := execRestore(bak); err != nil { - return xerr.NewCliErr(fmt.Sprintf("exec restore failed. err: %s", err)) - } + // exec restore + logging.Info("Start restore backup data to openGauss...") + if err := execRestore(bak); err != nil { + return xerr.NewCliErr(fmt.Sprintf("exec restore failed. err: %s", err)) + } - logging.Info("Restore backup data to openGauss success!") - // restore metadata to ss-proxy - if err := restoreDataToSSProxy(proxy, bak); err != nil { - return xerr.NewCliErr(fmt.Sprintf("restore metadata to shardingsphere proxy failed. err: %s", err)) - } - logging.Info("Restore success!") + logging.Info("Restore backup data to openGauss success!") + // restore metadata to ss-proxy + if err := restoreDataToSSProxy(proxy, bak); err != nil { + return xerr.NewCliErr(fmt.Sprintf("restore metadata to shardingsphere proxy failed. err: %s", err)) } + logging.Info("Restore success!") return nil }