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)
}
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 {
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
// 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 {
b.Remove()

View File

@ -49,7 +49,7 @@ type BackupInterface interface {
// Generates a backup in whatever the configured source for the specific
// implementation is.
Generate(*IncludedFiles, string) error
Generate(*IncludedFiles, string) (*ArchiveDetails, error)
// Returns the ignored files for this backup instance.
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
// for this instance.
func (b *LocalBackup) Generate(included *IncludedFiles, prefix string) error {
func (b *LocalBackup) Generate(included *IncludedFiles, prefix string) (*ArchiveDetails, error) {
a := &Archive{
TrimPrefix: prefix,
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 (
"context"
"fmt"
"go.uber.org/zap"
"io"
"net/http"
"os"
@ -21,7 +22,9 @@ type S3Backup struct {
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()
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 {
return err
return nil, err
}
fmt.Println(s.PresignedUrl)
r, err := http.NewRequest(http.MethodPut, s.PresignedUrl, nil)
rc, err := os.Open(s.Path())
if err != nil {
return 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
return nil, err
}
defer rc.Close()
r.Body = rc
resp, err := http.DefaultClient.Do(r)
if err != nil {
return err
}
defer resp.Body.Close()
if resp, err := s.generateRemoteRequest(rc); err != nil {
return nil, err
} else {
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
io.Copy(os.Stdout, resp.Body)
return fmt.Errorf("failed to put S3 object, %d:%s", resp.StatusCode, resp.Status)
return nil, 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.
@ -76,9 +60,24 @@ func (s *S3Backup) Remove() error {
return os.Remove(s.Path())
}
func (s *S3Backup) Details() *ArchiveDetails {
return &ArchiveDetails{
Checksum: "checksum",
Size: 1024,
// Generates the remote S3 request and begins the upload.
func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) (*http.Response, error) {
r, err := http.NewRequest(http.MethodPut, s.PresignedUrl, nil)
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)
}