2020-04-17 20:46:36 +00:00
|
|
|
package backup
|
|
|
|
|
2020-04-27 00:20:26 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-06-13 17:40:26 +00:00
|
|
|
"github.com/apex/log"
|
2020-08-28 02:57:22 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-27 00:20:26 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
type S3Backup struct {
|
2020-05-02 22:02:02 +00:00
|
|
|
Backup
|
2020-04-26 23:43:18 +00:00
|
|
|
|
|
|
|
// The pre-signed upload endpoint for the generated backup. This must be
|
|
|
|
// provided otherwise this request will fail. This allows us to keep all
|
|
|
|
// of the keys off the daemon instances and the panel can handle generating
|
|
|
|
// the credentials for us.
|
|
|
|
PresignedUrl string
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
var _ BackupInterface = (*S3Backup)(nil)
|
2020-04-17 20:46:36 +00:00
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
// 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) {
|
2020-04-27 00:20:26 +00:00
|
|
|
defer s.Remove()
|
2020-04-17 20:46:36 +00:00
|
|
|
|
2020-04-27 00:20:26 +00:00
|
|
|
a := &Archive{
|
|
|
|
TrimPrefix: prefix,
|
|
|
|
Files: included,
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:13:49 +00:00
|
|
|
if _, err := a.Create(s.Path(), context.Background()); err != nil {
|
2020-08-28 02:57:22 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
rc, err := os.Open(s.Path())
|
2020-04-27 00:20:26 +00:00
|
|
|
if err != nil {
|
2020-08-28 02:57:22 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
if resp, err := s.generateRemoteRequest(rc); err != nil {
|
2020-08-28 02:57:22 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2020-05-10 02:24:30 +00:00
|
|
|
} else {
|
|
|
|
resp.Body.Close()
|
2020-04-27 00:20:26 +00:00
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("failed to put S3 object, %d:%s", resp.StatusCode, resp.Status)
|
|
|
|
}
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
return s.Details(), err
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 00:20:26 +00:00
|
|
|
// Removes a backup from the system.
|
|
|
|
func (s *S3Backup) Remove() error {
|
|
|
|
return os.Remove(s.Path())
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
// 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")
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
2020-05-10 02:24:30 +00:00
|
|
|
|
|
|
|
r.Body = rc
|
2020-06-13 17:40:26 +00:00
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": s.PresignedUrl,
|
|
|
|
"headers": r.Header,
|
|
|
|
}).Debug("uploading backup to remote S3 endpoint")
|
2020-05-10 02:24:30 +00:00
|
|
|
|
|
|
|
return http.DefaultClient.Do(r)
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|