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"
|
2021-01-19 05:20:58 +00:00
|
|
|
"github.com/mholt/archiver/v3"
|
2021-07-15 21:37:38 +00:00
|
|
|
|
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")
|
2020-12-25 19:52:57 +00:00
|
|
|
if err := a.Create(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
|
|
|
|
2021-05-02 19:28:36 +00:00
|
|
|
ad, err := b.Details(ctx)
|
|
|
|
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 {
|
2021-01-19 05:20:58 +00:00
|
|
|
return archiver.Walk(b.Path(), func(f archiver.File) error {
|
2021-05-02 19:28:36 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Stop walking if the context is canceled.
|
|
|
|
return archiver.ErrStopWalk
|
|
|
|
default:
|
2021-07-15 21:37:38 +00:00
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
2021-05-02 19:28:36 +00:00
|
|
|
}
|
2021-07-15 21:37:38 +00:00
|
|
|
return callback(filesystem.ExtractNameFromArchive(f), f, f.Mode())
|
2021-01-19 05:20:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|