Get backups restoring kinda

This commit is contained in:
Dane Everitt 2021-01-20 20:03:14 -08:00
parent 5021ea6a86
commit 3f84ee694b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 11 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package router package router
import ( import (
"context"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -113,7 +114,10 @@ func postServerRestoreBackup(c *gin.Context) {
// parse over the contents as we go in order to restore it to the server. // parse over the contents as we go in order to restore it to the server.
client := http.Client{} client := http.Client{}
logger.Info("downloading backup from remote location...") logger.Info("downloading backup from remote location...")
req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, data.DownloadUrl, nil) // TODO: this will hang if there is an issue. We can't use c.Request.Context() (or really any)
// since it will be canceled when the request is closed which happens quickly since we push
// this into the background.
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, data.DownloadUrl, nil)
if err != nil { if err != nil {
middleware.CaptureAndAbort(c, err) middleware.CaptureAndAbort(c, err)
return return
@ -123,19 +127,19 @@ func postServerRestoreBackup(c *gin.Context) {
middleware.CaptureAndAbort(c, err) middleware.CaptureAndAbort(c, err)
return return
} }
defer res.Body.Close()
// Don't allow content types that we know are going to give us problems. // Don't allow content types that we know are going to give us problems.
if res.Header.Get("Content-Type") == "" || !strings.Contains("application/x-gzip application/gzip", res.Header.Get("Content-Type")) { if res.Header.Get("Content-Type") == "" || !strings.Contains("application/x-gzip application/gzip", res.Header.Get("Content-Type")) {
res.Body.Close()
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "The provided backup link is not a supported content type. \"" + res.Header.Get("Content-Type") + "\" is not application/x-gzip.", "error": "The provided backup link is not a supported content type. \"" + res.Header.Get("Content-Type") + "\" is not application/x-gzip.",
}) })
return return
} }
go func(uuid string, logger *log.Entry) { go func(uuid string, logger *log.Entry) {
logger.Info("restoring server from remote S3 backup...") logger.Info("restoring server from remote S3 backup...")
if err := s.RestoreBackup(backup.NewS3(uuid, ""), nil); err != nil { if err := s.RestoreBackup(backup.NewS3(uuid, ""), res.Body); err != nil {
logger.WithField("error", err).Error("failed to restore remote S3 backup to server") logger.WithField("error", errors.WithStack(err)).Error("failed to restore remote S3 backup to server")
} }
}(c.Param("backup"), logger) }(c.Param("backup"), logger)

View File

@ -136,8 +136,8 @@ func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (
// Send an API call to the Panel as soon as this function is done running so that // Send an API call to the Panel as soon as this function is done running so that
// the Panel is informed of the restoration status of this backup. // the Panel is informed of the restoration status of this backup.
defer func() { defer func() {
if err := api.New().SendRestorationStatus(b.Identifier(), err == nil); err != nil { if rerr := api.New().SendRestorationStatus(b.Identifier(), err == nil); rerr != nil {
s.Log().WithField("error", err).WithField("backup", b.Identifier()).Error("failed to notify Panel of backup restoration status") s.Log().WithField("error", rerr).WithField("backup", b.Identifier()).Error("failed to notify Panel of backup restoration status")
} }
}() }()