Abstract out shared backup functionality

This commit is contained in:
Dane Everitt
2020-05-02 15:02:02 -07:00
parent 507d0100cf
commit b2797ed292
6 changed files with 166 additions and 200 deletions

View File

@@ -2,25 +2,15 @@ package backup
import (
"context"
"crypto/sha256"
"fmt"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"io"
"net/http"
"os"
"path"
"strconv"
)
type S3Backup struct {
// The UUID of this backup object. This must line up with a backup from
// the panel instance.
Uuid string
// An array of files to ignore when generating this backup. This should be
// compatible with a standard .gitignore structure.
IgnoredFiles []string
Backup
// The pre-signed upload endpoint for the generated backup. This must be
// provided otherwise this request will fail. This allows us to keep all
@@ -29,13 +19,9 @@ type S3Backup struct {
PresignedUrl string
}
var _ Backup = (*S3Backup)(nil)
var _ BackupInterface = (*S3Backup)(nil)
func (s *S3Backup) Identifier() string {
return s.Uuid
}
func (s *S3Backup) Backup(included *IncludedFiles, prefix string) error {
func (s *S3Backup) Generate(included *IncludedFiles, prefix string) error {
defer s.Remove()
a := &Archive{
@@ -85,39 +71,6 @@ func (s *S3Backup) Backup(included *IncludedFiles, prefix string) error {
return nil
}
// Return the size of the generated backup.
func (s *S3Backup) Size() (int64, error) {
st, err := os.Stat(s.Path())
if err != nil {
return 0, errors.WithStack(err)
}
return st.Size(), nil
}
// Returns the path for this specific backup. S3 backups are only stored on the disk
// long enough for us to get the details we need before uploading them to S3.
func (s *S3Backup) Path() string {
return path.Join(config.Get().System.BackupDirectory, s.Uuid+".tmp")
}
// Returns the SHA256 checksum of a backup.
func (s *S3Backup) Checksum() ([]byte, error) {
h := sha256.New()
f, err := os.Open(s.Path())
if err != nil {
return []byte{}, errors.WithStack(err)
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
return []byte{}, errors.WithStack(err)
}
return h.Sum(nil), nil
}
// Removes a backup from the system.
func (s *S3Backup) Remove() error {
return os.Remove(s.Path())
@@ -129,7 +82,3 @@ func (s *S3Backup) Details() *ArchiveDetails {
Size: 1024,
}
}
func (s *S3Backup) Ignored() []string {
return s.IgnoredFiles
}