2020-04-14 05:01:07 +00:00
|
|
|
package backup
|
|
|
|
|
|
|
|
import (
|
2020-11-28 23:57:10 +00:00
|
|
|
"errors"
|
2020-12-27 19:54:18 +00:00
|
|
|
"github.com/apex/log"
|
2020-12-25 19:52:57 +00:00
|
|
|
"github.com/pterodactyl/wings/server/filesystem"
|
2020-04-14 05:01:07 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
// 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.
|
2020-04-17 20:46:36 +00:00
|
|
|
func LocateLocal(uuid string) (*LocalBackup, os.FileInfo, error) {
|
|
|
|
b := &LocalBackup{
|
2020-05-02 22:02:02 +00:00
|
|
|
Backup{
|
2020-12-25 19:52:57 +00:00
|
|
|
Uuid: uuid,
|
|
|
|
Ignore: "",
|
2020-05-02 22:02:02 +00:00
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
// Removes a backup from the system.
|
|
|
|
func (b *LocalBackup) Remove() error {
|
|
|
|
return os.Remove(b.Path())
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
// Generates a backup of the selected files and pushes it to the defined location
|
|
|
|
// for this instance.
|
2020-12-25 19:52:57 +00:00
|
|
|
func (b *LocalBackup) Generate(basePath, ignore string) (*ArchiveDetails, error) {
|
|
|
|
a := &filesystem.Archive{
|
|
|
|
BasePath: basePath,
|
|
|
|
Ignore: ignore,
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 19:54:18 +00:00
|
|
|
l := log.WithFields(log.Fields{
|
|
|
|
"backup_id": b.Uuid,
|
|
|
|
"adapter": "local",
|
|
|
|
})
|
|
|
|
|
2020-12-27 20:01:34 +00:00
|
|
|
l.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-27 20:01:34 +00:00
|
|
|
l.Info("created backup successfully")
|
2020-04-19 06:26:23 +00:00
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
return b.Details(), nil
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|