From 3f84ee694b993c86b0e7ef0d069183ab02efe260 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 20 Jan 2021 20:03:14 -0800 Subject: [PATCH] Get backups restoring kinda --- router/router_server_backup.go | 14 +++++++++----- server/backup.go | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/router/router_server_backup.go b/router/router_server_backup.go index 9555383..cd34a0e 100644 --- a/router/router_server_backup.go +++ b/router/router_server_backup.go @@ -1,6 +1,7 @@ package router import ( + "context" "net/http" "os" "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. client := http.Client{} 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 { middleware.CaptureAndAbort(c, err) return @@ -123,19 +127,19 @@ func postServerRestoreBackup(c *gin.Context) { middleware.CaptureAndAbort(c, err) return } - defer res.Body.Close() - // 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")) { + res.Body.Close() 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.", }) return } + go func(uuid string, logger *log.Entry) { logger.Info("restoring server from remote S3 backup...") - if err := s.RestoreBackup(backup.NewS3(uuid, ""), nil); err != nil { - logger.WithField("error", err).Error("failed to restore remote S3 backup to server") + if err := s.RestoreBackup(backup.NewS3(uuid, ""), res.Body); err != nil { + logger.WithField("error", errors.WithStack(err)).Error("failed to restore remote S3 backup to server") } }(c.Param("backup"), logger) diff --git a/server/backup.go b/server/backup.go index f8e5fa3..5d567d7 100644 --- a/server/backup.go +++ b/server/backup.go @@ -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 // the Panel is informed of the restoration status of this backup. defer func() { - if err := api.New().SendRestorationStatus(b.Identifier(), err == nil); err != nil { - s.Log().WithField("error", err).WithField("backup", b.Identifier()).Error("failed to notify Panel of backup restoration status") + if rerr := api.New().SendRestorationStatus(b.Identifier(), err == nil); rerr != nil { + s.Log().WithField("error", rerr).WithField("backup", b.Identifier()).Error("failed to notify Panel of backup restoration status") } }()