2020-04-17 20:46:36 +00:00
|
|
|
package backup
|
|
|
|
|
2020-04-27 00:20:26 +00:00
|
|
|
import (
|
2020-10-31 23:47:41 +00:00
|
|
|
"bytes"
|
2020-04-27 00:20:26 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-06-13 17:40:26 +00:00
|
|
|
"github.com/apex/log"
|
2020-10-31 23:47:41 +00:00
|
|
|
"github.com/pterodactyl/wings/api"
|
2020-04-27 00:20:26 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-11-01 17:30:25 +00:00
|
|
|
"time"
|
2020-04-27 00:20:26 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
type S3Backup struct {
|
2020-05-02 22:02:02 +00:00
|
|
|
Backup
|
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-09-27 18:16:38 +00:00
|
|
|
if err := a.Create(s.Path(), context.Background()); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, 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-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
if err := s.generateRemoteRequest(rc); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 23:57:10 +00:00
|
|
|
return s.Details(), nil
|
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-10-31 23:47:41 +00:00
|
|
|
// Reader provides a wrapper around an existing io.Reader
|
|
|
|
// but implements io.Closer in order to satisfy an io.ReadCloser.
|
|
|
|
type Reader struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Reader) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
// Generates the remote S3 request and begins the upload.
|
2020-10-31 23:47:41 +00:00
|
|
|
func (s *S3Backup) generateRemoteRequest(rc io.ReadCloser) error {
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
size, err := s.Backup.Size()
|
2020-05-10 02:24:30 +00:00
|
|
|
if err != nil {
|
2020-10-31 23:47:41 +00:00
|
|
|
return err
|
2020-05-10 02:24:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
urls, err := api.New().GetBackupRemoteUploadURLs(s.Backup.Uuid, size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-01 22:36:16 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"backup_id": s.Uuid,
|
|
|
|
"adapter": "s3",
|
|
|
|
}).Info("attempting to upload backup..")
|
2020-10-31 23:47:41 +00:00
|
|
|
|
|
|
|
handlePart := func(part string, size int64) (string, error) {
|
|
|
|
r, err := http.NewRequest(http.MethodPut, part, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ContentLength = size
|
|
|
|
r.Header.Add("Content-Length", strconv.Itoa(int(size)))
|
2020-05-10 02:24:30 +00:00
|
|
|
r.Header.Add("Content-Type", "application/x-gzip")
|
2020-10-31 23:47:41 +00:00
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Limit the reader to the size of the part.
|
2020-10-31 23:47:41 +00:00
|
|
|
r.Body = Reader{io.LimitReader(rc, size)}
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// This http request can block forever due to it not having a timeout,
|
|
|
|
// but we are uploading up to 5GB of data, so there is not really
|
|
|
|
// a good way to handle a timeout on this.
|
2020-10-31 23:47:41 +00:00
|
|
|
res, err := http.DefaultClient.Do(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Handle non-200 status codes.
|
2020-10-31 23:47:41 +00:00
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("failed to put S3 object part, %d:%s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Get the ETag from the uploaded part, this should be sent with the CompleteMultipartUpload request.
|
2020-10-31 23:47:41 +00:00
|
|
|
return res.Header.Get("ETag"), nil
|
2020-04-27 00:20:26 +00:00
|
|
|
}
|
2020-05-10 02:24:30 +00:00
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Start assembling the body that will be sent as apart of the CompleteMultipartUpload request.
|
|
|
|
var completeUploadBody bytes.Buffer
|
|
|
|
completeUploadBody.WriteString("<CompleteMultipartUpload>\n")
|
2020-10-31 23:47:41 +00:00
|
|
|
|
|
|
|
partCount := len(urls.Parts)
|
|
|
|
for i, part := range urls.Parts {
|
2020-11-01 17:30:25 +00:00
|
|
|
// Get the size for the current part.
|
|
|
|
var partSize int64
|
2020-10-31 23:47:41 +00:00
|
|
|
if i+1 < partCount {
|
2020-11-01 17:30:25 +00:00
|
|
|
partSize = urls.PartSize
|
2020-10-31 23:47:41 +00:00
|
|
|
} else {
|
2020-11-01 17:30:25 +00:00
|
|
|
// This is the remaining size for the last part,
|
|
|
|
// there is not a minimum size limit for the last part.
|
|
|
|
partSize = size - (int64(i) * urls.PartSize)
|
2020-10-31 23:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Attempt to upload the part.
|
|
|
|
etag, err := handlePart(part, partSize)
|
2020-10-31 23:47:41 +00:00
|
|
|
if err != nil {
|
2020-11-01 17:30:25 +00:00
|
|
|
log.WithError(err).Warn("failed to upload part")
|
|
|
|
|
|
|
|
// Send an AbortMultipartUpload request.
|
|
|
|
if err := s.finishUpload(urls.AbortMultipartUpload, nil); err != nil {
|
|
|
|
log.WithError(err).Warn("failed to abort multipart backup upload")
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-09-05 19:08:40 +00:00
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Add the part to the CompleteMultipartUpload body.
|
|
|
|
completeUploadBody.WriteString("\t<Part>\n")
|
|
|
|
completeUploadBody.WriteString("\t\t<ETag>\"" + etag + "\"</ETag>\n")
|
|
|
|
completeUploadBody.WriteString("\t\t<PartNumber>" + strconv.Itoa(i+1) + "</PartNumber>\n")
|
|
|
|
completeUploadBody.WriteString("\t</Part>\n")
|
|
|
|
}
|
|
|
|
completeUploadBody.WriteString("</CompleteMultipartUpload>")
|
|
|
|
|
|
|
|
// Send a CompleteMultipartUpload request.
|
|
|
|
if err := s.finishUpload(urls.CompleteMultipartUpload, &completeUploadBody); err != nil {
|
|
|
|
return err
|
2020-10-31 23:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 22:36:16 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"backup_id": s.Uuid,
|
|
|
|
"adapter": "s3",
|
|
|
|
}).Info("backup has been successfully uploaded")
|
2020-11-01 17:30:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-31 23:47:41 +00:00
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// finishUpload sends a requests to the specified url to either complete or abort the upload.
|
|
|
|
func (s *S3Backup) finishUpload(url string, body io.Reader) error {
|
|
|
|
r, err := http.NewRequest(http.MethodPost, url, body)
|
2020-10-31 23:47:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Create a new http client with a 10 second timeout.
|
|
|
|
c := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.Do(r)
|
2020-10-31 23:47:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2020-11-01 17:30:25 +00:00
|
|
|
// Handle non-200 status codes.
|
2020-10-31 23:47:41 +00:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2020-11-01 17:30:25 +00:00
|
|
|
// If no body was sent, we were aborting the upload.
|
|
|
|
if body == nil {
|
|
|
|
return fmt.Errorf("failed to abort S3 multipart upload, %d:%s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a body was sent we were completing the upload.
|
|
|
|
// TODO: Attempt to send abort request?
|
2020-10-31 23:47:41 +00:00
|
|
|
return fmt.Errorf("failed to complete S3 multipart upload, %d:%s", res.StatusCode, res.Status)
|
|
|
|
}
|
2020-05-10 02:24:30 +00:00
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
return nil
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|