Cleanup S3 support; send actual backup details in response

This commit is contained in:
Dane Everitt 2020-05-09 19:24:30 -07:00
parent 326b5b6554
commit 0bd28a4480
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
4 changed files with 42 additions and 41 deletions

View File

@ -86,7 +86,8 @@ func (s *Server) Backup(b backup.BackupInterface) error {
return errors.WithStack(err) return errors.WithStack(err)
} }
if err := b.Generate(inc, s.Filesystem.Path()); err != nil { ad, err := b.Generate(inc, s.Filesystem.Path())
if err != nil {
if notifyError := s.notifyPanelOfBackup(b.Identifier(), &backup.ArchiveDetails{}, false); notifyError != nil { if notifyError := s.notifyPanelOfBackup(b.Identifier(), &backup.ArchiveDetails{}, false); notifyError != nil {
zap.S().Warnw("failed to notify panel of failed backup state", zap.String("backup", b.Identifier()), zap.Error(err)) zap.S().Warnw("failed to notify panel of failed backup state", zap.String("backup", b.Identifier()), zap.Error(err))
} }
@ -96,7 +97,6 @@ func (s *Server) Backup(b backup.BackupInterface) error {
// Try to notify the panel about the status of this backup. If for some reason this request // Try to notify the panel about the status of this backup. If for some reason this request
// fails, delete the archive from the daemon and return that error up the chain to the caller. // fails, delete the archive from the daemon and return that error up the chain to the caller.
ad := b.Details()
if notifyError := s.notifyPanelOfBackup(b.Identifier(), ad, true); notifyError != nil { if notifyError := s.notifyPanelOfBackup(b.Identifier(), ad, true); notifyError != nil {
b.Remove() b.Remove()

View File

@ -49,7 +49,7 @@ type BackupInterface interface {
// Generates a backup in whatever the configured source for the specific // Generates a backup in whatever the configured source for the specific
// implementation is. // implementation is.
Generate(*IncludedFiles, string) error Generate(*IncludedFiles, string) (*ArchiveDetails, error)
// Returns the ignored files for this backup instance. // Returns the ignored files for this backup instance.
Ignored() []string Ignored() []string

View File

@ -41,13 +41,15 @@ func (b *LocalBackup) Remove() error {
// Generates a backup of the selected files and pushes it to the defined location // Generates a backup of the selected files and pushes it to the defined location
// for this instance. // for this instance.
func (b *LocalBackup) Generate(included *IncludedFiles, prefix string) error { func (b *LocalBackup) Generate(included *IncludedFiles, prefix string) (*ArchiveDetails, error) {
a := &Archive{ a := &Archive{
TrimPrefix: prefix, TrimPrefix: prefix,
Files: included, Files: included,
} }
err := a.Create(b.Path(), context.Background()) if err := a.Create(b.Path(), context.Background()); err != nil {
return nil, err
return err }
return b.Details(), nil
} }

View File

@ -3,6 +3,7 @@ package backup
import ( import (
"context" "context"
"fmt" "fmt"
"go.uber.org/zap"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -21,7 +22,9 @@ type S3Backup struct {
var _ BackupInterface = (*S3Backup)(nil) var _ BackupInterface = (*S3Backup)(nil)
func (s *S3Backup) Generate(included *IncludedFiles, prefix string) error { // 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(included *IncludedFiles, prefix string) (*ArchiveDetails, error) {
defer s.Remove() defer s.Remove()
a := &Archive{ a := &Archive{
@ -30,45 +33,26 @@ func (s *S3Backup) Generate(included *IncludedFiles, prefix string) error {
} }
if err := a.Create(s.Path(), context.Background()); err != nil { if err := a.Create(s.Path(), context.Background()); err != nil {
return err return nil, err
} }
fmt.Println(s.PresignedUrl) rc, err := os.Open(s.Path())
r, err := http.NewRequest(http.MethodPut, s.PresignedUrl, nil)
if err != nil { if err != nil {
return err return nil, err
}
if sz, err := s.Size(); err != nil {
return err
} else {
r.ContentLength = sz
r.Header.Add("Content-Length", strconv.Itoa(int(sz)))
r.Header.Add("Content-Type", "application/x-gzip")
}
var rc io.ReadCloser
if f, err := os.Open(s.Path()); err != nil {
return err
} else {
rc = f
} }
defer rc.Close() defer rc.Close()
r.Body = rc if resp, err := s.generateRemoteRequest(rc); err != nil {
resp, err := http.DefaultClient.Do(r) return nil, err
if err != nil { } else {
return err resp.Body.Close()
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
io.Copy(os.Stdout, resp.Body) return nil, fmt.Errorf("failed to put S3 object, %d:%s", resp.StatusCode, resp.Status)
return fmt.Errorf("failed to put S3 object, %d:%s", resp.StatusCode, resp.Status) }
} }
return nil return s.Details(), err
} }
// Removes a backup from the system. // Removes a backup from the system.
@ -76,9 +60,24 @@ func (s *S3Backup) Remove() error {
return os.Remove(s.Path()) return os.Remove(s.Path())
} }
func (s *S3Backup) Details() *ArchiveDetails { // Generates the remote S3 request and begins the upload.
return &ArchiveDetails{ func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) (*http.Response, error) {
Checksum: "checksum", r, err := http.NewRequest(http.MethodPut, s.PresignedUrl, nil)
Size: 1024, if err != nil {
return nil, err
} }
if sz, err := s.Size(); err != nil {
return nil, err
} else {
r.ContentLength = sz
r.Header.Add("Content-Length", strconv.Itoa(int(sz)))
r.Header.Add("Content-Type", "application/x-gzip")
}
r.Body = rc
zap.S().Debugw("uploading backup to remote S3 endpoint", zap.String("endpoint", s.PresignedUrl), zap.Any("headers", r.Header))
return http.DefaultClient.Do(r)
} }