Include more (and customizable) context with backup logs

This commit is contained in:
Dane Everitt
2020-12-27 16:16:40 -08:00
parent b26db99ee7
commit 902f9f5944
6 changed files with 65 additions and 51 deletions

View File

@@ -2,7 +2,6 @@ package backup
import (
"fmt"
"github.com/apex/log"
"github.com/pterodactyl/wings/api"
"github.com/pterodactyl/wings/server/filesystem"
"io"
@@ -17,6 +16,16 @@ type S3Backup struct {
var _ BackupInterface = (*S3Backup)(nil)
// Removes a backup from the system.
func (s *S3Backup) Remove() error {
return os.Remove(s.Path())
}
// Attaches additional context to the log output for this backup.
func (s *S3Backup) WithLogContext(c map[string]interface{}) {
s.logContext = c
}
// Generates a new backup on the disk, moves it into the S3 bucket via the provided
// presigned URL, and then deletes the backup from the disk.
func (s *S3Backup) Generate(basePath, ignore string) (*ArchiveDetails, error) {
@@ -27,16 +36,11 @@ func (s *S3Backup) Generate(basePath, ignore string) (*ArchiveDetails, error) {
Ignore: ignore,
}
l := log.WithFields(log.Fields{
"backup_id": s.Uuid,
"adapter": "s3",
})
l.Info("creating backup for server...")
s.log().Info("creating backup for server...")
if err := a.Create(s.Path()); err != nil {
return nil, err
}
l.Info("created backup successfully")
s.log().Info("created backup successfully")
rc, err := os.Open(s.Path())
if err != nil {
@@ -51,11 +55,6 @@ func (s *S3Backup) Generate(basePath, ignore string) (*ArchiveDetails, error) {
return s.Details(), nil
}
// Removes a backup from the system.
func (s *S3Backup) Remove() error {
return os.Remove(s.Path())
}
// Reader provides a wrapper around an existing io.Reader
// but implements io.Closer in order to satisfy an io.ReadCloser.
type Reader struct {
@@ -70,26 +69,20 @@ func (Reader) Close() error {
func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) error {
defer rc.Close()
l := log.WithFields(log.Fields{
"backup_id": s.Uuid,
"adapter": "s3",
})
l.Debug("attempting to get size of backup...")
s.log().Debug("attempting to get size of backup...")
size, err := s.Backup.Size()
if err != nil {
return err
}
l.WithField("size", size).Debug("got size of backup")
s.log().WithField("size", size).Debug("got size of backup")
l.Debug("attempting to get S3 upload urls from Panel...")
s.log().Debug("attempting to get S3 upload urls from Panel...")
urls, err := api.New().GetBackupRemoteUploadURLs(s.Backup.Uuid, size)
if err != nil {
return err
}
l.Debug("got S3 upload urls from the Panel")
partCount := len(urls.Parts)
l.WithField("parts", partCount).Info("attempting to upload backup...")
s.log().Debug("got S3 upload urls from the Panel")
s.log().WithField("parts", len(urls.Parts)).Info("attempting to upload backup to s3 endpoint...")
handlePart := func(part string, size int64) (string, error) {
r, err := http.NewRequest(http.MethodPut, part, nil)
@@ -125,7 +118,7 @@ func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) error {
for i, part := range urls.Parts {
// Get the size for the current part.
var partSize int64
if i+1 < partCount {
if i+1 < len(urls.Parts) {
partSize = urls.PartSize
} else {
// This is the remaining size for the last part,
@@ -135,14 +128,14 @@ func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) error {
// Attempt to upload the part.
if _, err := handlePart(part, partSize); err != nil {
l.WithField("part_id", i+1).WithError(err).Warn("failed to upload part")
s.log().WithField("part_id", i+1).WithError(err).Warn("failed to upload part")
return err
}
l.WithField("part_id", i+1).Info("successfully uploaded backup part")
s.log().WithField("part_id", i+1).Info("successfully uploaded backup part")
}
l.WithField("parts", partCount).Info("backup has been successfully uploaded")
s.log().WithField("parts", len(urls.Parts)).Info("backup has been successfully uploaded")
return nil
}