Skip to content

Commit

Permalink
refactor: add backup set size validation
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore committed Nov 17, 2023
1 parent 0570e2e commit dd3fb63
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 75 deletions.
9 changes: 8 additions & 1 deletion pitr/cli/internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
65 changes: 28 additions & 37 deletions pitr/cli/internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
66 changes: 29 additions & 37 deletions pitr/cli/internal/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit dd3fb63

Please sign in to comment.