2020-04-14 05:01:07 +00:00
|
|
|
package backup
|
|
|
|
|
|
|
|
import (
|
2021-05-02 19:28:36 +00:00
|
|
|
"context"
|
2021-01-19 05:20:58 +00:00
|
|
|
"io"
|
2020-04-14 05:01:07 +00:00
|
|
|
"os"
|
2021-01-19 05:20:58 +00:00
|
|
|
|
2021-05-02 19:28:36 +00:00
|
|
|
"emperror.dev/errors"
|
2022-11-06 20:38:30 +00:00
|
|
|
"github.com/juju/ratelimit"
|
|
|
|
"github.com/mholt/archiver/v4"
|
2021-07-15 21:37:38 +00:00
|
|
|
|
2022-11-06 20:38:30 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2021-02-02 05:28:46 +00:00
|
|
|
"github.com/pterodactyl/wings/remote"
|
2021-08-02 21:07:00 +00:00
|
|
|
"github.com/pterodactyl/wings/server/filesystem"
|
2020-04-14 05:01:07 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
type LocalBackup 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 = (*LocalBackup)(nil)
|
2020-04-17 20:46:36 +00:00
|
|
|
|
2021-02-02 05:28:46 +00:00
|
|
|
func NewLocal(client remote.Client, uuid string, ignore string) *LocalBackup {
|
2021-01-18 05:05:51 +00:00
|
|
|
return &LocalBackup{
|
|
|
|
Backup{
|
2021-03-07 18:02:03 +00:00
|
|
|
client: client,
|
2021-01-18 05:05:51 +00:00
|
|
|
Uuid: uuid,
|
|
|
|
Ignore: ignore,
|
|
|
|
adapter: LocalBackupAdapter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// LocateLocal finds the backup for a server and returns the local path. This
|
|
|
|
// will obviously only work if the backup was created as a local backup.
|
2021-02-02 05:28:46 +00:00
|
|
|
func LocateLocal(client remote.Client, uuid string) (*LocalBackup, os.FileInfo, error) {
|
|
|
|
b := NewLocal(client, uuid, "")
|
2020-04-14 05:01:07 +00:00
|
|
|
st, err := os.Stat(b.Path())
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, nil, err
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if st.IsDir() {
|
2020-09-01 03:24:07 +00:00
|
|
|
return nil, nil, errors.New("invalid archive, is directory")
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b, st, nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// Remove removes a backup from the system.
|
2020-04-17 20:46:36 +00:00
|
|
|
func (b *LocalBackup) Remove() error {
|
|
|
|
return os.Remove(b.Path())
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// WithLogContext attaches additional context to the log output for this backup.
|
2020-12-28 00:16:40 +00:00
|
|
|
func (b *LocalBackup) WithLogContext(c map[string]interface{}) {
|
|
|
|
b.logContext = c
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// Generate generates a backup of the selected files and pushes it to the
|
|
|
|
// defined location for this instance.
|
2021-05-02 19:28:36 +00:00
|
|
|
func (b *LocalBackup) Generate(ctx context.Context, basePath, ignore string) (*ArchiveDetails, error) {
|
2021-03-07 18:02:03 +00:00
|
|
|
a := &filesystem.Archive{
|
2020-12-25 19:52:57 +00:00
|
|
|
BasePath: basePath,
|
|
|
|
Ignore: ignore,
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 19:28:36 +00:00
|
|
|
b.log().WithField("path", b.Path()).Info("creating backup for server")
|
2022-11-15 01:25:01 +00:00
|
|
|
if err := a.Create(ctx, b.Path()); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-05-10 02:24:30 +00:00
|
|
|
}
|
2020-12-28 00:16:40 +00:00
|
|
|
b.log().Info("created backup successfully")
|
2020-04-19 06:26:23 +00:00
|
|
|
|
2022-09-26 17:14:57 +00:00
|
|
|
ad, err := b.Details(ctx, nil)
|
2021-05-02 19:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WrapIf(err, "backup: failed to get archive details for local backup")
|
|
|
|
}
|
|
|
|
return ad, nil
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
2021-01-17 02:06:22 +00:00
|
|
|
|
2021-01-19 05:20:58 +00:00
|
|
|
// Restore will walk over the archive and call the callback function for each
|
|
|
|
// file encountered.
|
2021-05-02 19:28:36 +00:00
|
|
|
func (b *LocalBackup) Restore(ctx context.Context, _ io.Reader, callback RestoreCallback) error {
|
2022-11-06 20:38:30 +00:00
|
|
|
f, err := os.Open(b.Path())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 01:34:08 +00:00
|
|
|
defer f.Close()
|
2022-11-06 20:38:30 +00:00
|
|
|
|
|
|
|
var reader io.Reader = f
|
|
|
|
// Steal the logic we use for making backups which will be applied when restoring
|
|
|
|
// this specific backup. This allows us to prevent overloading the disk unintentionally.
|
|
|
|
if writeLimit := int64(config.Get().System.Backups.WriteLimit * 1024 * 1024); writeLimit > 0 {
|
|
|
|
reader = ratelimit.Reader(f, ratelimit.NewBucketWithRate(float64(writeLimit), writeLimit))
|
|
|
|
}
|
|
|
|
if err := format.Extract(ctx, reader, nil, func(ctx context.Context, f archiver.File) error {
|
|
|
|
r, err := f.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-19 05:20:58 +00:00
|
|
|
}
|
2022-11-06 20:38:30 +00:00
|
|
|
defer r.Close()
|
|
|
|
|
2023-10-18 02:47:36 +00:00
|
|
|
return callback(f.NameInArchive, f.FileInfo, r)
|
2022-11-06 20:38:30 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-19 05:20:58 +00:00
|
|
|
}
|