Break out the backup functions of the daemon in prep for S3 support
This commit is contained in:
103
server/backup/backup.go
Normal file
103
server/backup/backup.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/api"
|
||||
"github.com/pterodactyl/wings/config"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Backup struct {
|
||||
// The UUID of this backup object. This must line up with a backup from
|
||||
// the panel instance.
|
||||
Uuid string `json:"uuid"`
|
||||
|
||||
// An array of files to ignore when generating this backup. This should be
|
||||
// compatible with a standard .gitignore structure.
|
||||
IgnoredFiles []string `json:"ignored_files"`
|
||||
}
|
||||
|
||||
type ArchiveDetails struct {
|
||||
Checksum string `json:"checksum"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// Returns a request object.
|
||||
func (ad *ArchiveDetails) ToRequest(successful bool) api.BackupRequest {
|
||||
return api.BackupRequest{
|
||||
Checksum: ad.Checksum,
|
||||
Size: ad.Size,
|
||||
Successful: successful,
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the path for this specific backup.
|
||||
func (b *Backup) Path() string {
|
||||
return path.Join(config.Get().System.BackupDirectory, b.Uuid+".tar.gz")
|
||||
}
|
||||
|
||||
// Returns the SHA256 checksum of a backup.
|
||||
func (b *Backup) Checksum() ([]byte, error) {
|
||||
h := sha256.New()
|
||||
|
||||
f, err := os.Open(b.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 (b *Backup) Remove() error {
|
||||
return os.Remove(b.Path())
|
||||
}
|
||||
|
||||
// Notifies the panel of a backup's state and returns an error if one is encountered
|
||||
// while performing this action.
|
||||
func (b *Backup) NotifyPanel(ad *ArchiveDetails, successful bool) error {
|
||||
r := api.NewRequester()
|
||||
|
||||
rerr, err := r.SendBackupStatus(b.Uuid, ad.ToRequest(successful))
|
||||
if rerr != nil || err != nil {
|
||||
if err != nil {
|
||||
zap.S().Errorw(
|
||||
"failed to notify panel of backup status due to internal code error",
|
||||
zap.String("backup", b.Uuid),
|
||||
zap.Error(err),
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
zap.S().Warnw(rerr.String(), zap.String("backup", b.Uuid))
|
||||
|
||||
return errors.New(rerr.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ensures that the local backup destination for files exists.
|
||||
func (b *Backup) ensureLocalBackupLocation() error {
|
||||
d := config.Get().System.BackupDirectory
|
||||
|
||||
if _, err := os.Stat(d); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return os.MkdirAll(d, 0700)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
87
server/backup/backup_local.go
Normal file
87
server/backup/backup_local.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"github.com/mholt/archiver/v3"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Locates the backup for a server and returns the local path. This will obviously only
|
||||
// work if the backup was created as a local backup.
|
||||
func LocateLocal(uuid string) (*Backup, os.FileInfo, error) {
|
||||
b := &Backup{
|
||||
Uuid: uuid,
|
||||
IgnoredFiles: nil,
|
||||
}
|
||||
|
||||
st, err := os.Stat(b.Path())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if st.IsDir() {
|
||||
return nil, nil, errors.New("invalid archive found; is directory")
|
||||
}
|
||||
|
||||
return b, st, nil
|
||||
}
|
||||
|
||||
// Generates a backup of the selected files and pushes it to the defined location
|
||||
// for this instance.
|
||||
func (b *Backup) LocalBackup(dir string) (*ArchiveDetails, error) {
|
||||
if err := archiver.Archive([]string{dir}, b.Path()); err != nil {
|
||||
if strings.HasPrefix(err.Error(), "file already exists") {
|
||||
if rerr := os.Remove(b.Path()); rerr != nil {
|
||||
return nil, errors.WithStack(rerr)
|
||||
}
|
||||
|
||||
// Re-attempt this backup by calling it with the same information.
|
||||
return b.LocalBackup(dir)
|
||||
}
|
||||
|
||||
// If there was some error with the archive, just go ahead and ensure the backup
|
||||
// is completely destroyed at this point. Ignore any errors from this function.
|
||||
os.Remove(b.Path())
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
var checksum string
|
||||
// Calculate the checksum for the file.
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
resp, err := b.Checksum()
|
||||
if err != nil {
|
||||
zap.S().Errorw("failed to calculate checksum for backup", zap.String("backup", b.Uuid), zap.Error(err))
|
||||
}
|
||||
|
||||
checksum = hex.EncodeToString(resp)
|
||||
}()
|
||||
|
||||
var sz int64
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
st, err := os.Stat(b.Path())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sz = st.Size()
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return &ArchiveDetails{
|
||||
Checksum: checksum,
|
||||
Size: sz,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user