2020-04-06 02:07:16 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-04-26 23:21:58 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-06 02:07:16 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/pterodactyl/wings/server"
|
2020-04-14 05:01:07 +00:00
|
|
|
"github.com/pterodactyl/wings/server/backup"
|
2020-04-06 02:07:16 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Backs up a server.
|
|
|
|
func postServerBackup(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-04-26 23:21:58 +00:00
|
|
|
data := &backup.Request{}
|
2020-05-29 15:44:49 +00:00
|
|
|
// BindJSON sends 400 if the request fails, all we need to do is return
|
|
|
|
if err := c.BindJSON(&data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 02:07:16 +00:00
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
var adapter backup.BackupInterface
|
2020-04-26 23:43:18 +00:00
|
|
|
var err error
|
|
|
|
|
2020-04-26 23:21:58 +00:00
|
|
|
switch data.Adapter {
|
|
|
|
case backup.LocalBackupAdapter:
|
2020-04-26 23:43:18 +00:00
|
|
|
adapter, err = data.NewLocalBackup()
|
2020-04-26 23:21:58 +00:00
|
|
|
case backup.S3BackupAdapter:
|
2020-04-26 23:43:18 +00:00
|
|
|
adapter, err = data.NewS3Backup()
|
2020-04-26 23:21:58 +00:00
|
|
|
default:
|
2020-04-26 23:43:18 +00:00
|
|
|
err = errors.New(fmt.Sprintf("unknown backup adapter [%s] provided", data.Adapter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
2020-04-26 23:21:58 +00:00
|
|
|
}
|
2020-04-06 02:07:16 +00:00
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
go func(b backup.BackupInterface, serv *server.Server) {
|
2020-04-26 23:43:18 +00:00
|
|
|
if err := serv.Backup(b); err != nil {
|
|
|
|
zap.S().Errorw("failed to generate backup for server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}(adapter, s)
|
|
|
|
|
2020-04-06 02:07:16 +00:00
|
|
|
c.Status(http.StatusAccepted)
|
|
|
|
}
|
2020-04-10 05:07:48 +00:00
|
|
|
|
|
|
|
// Deletes a local backup of a server.
|
|
|
|
func deleteServerBackup(c *gin.Context) {
|
|
|
|
s := GetServer(c.Param("server"))
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
b, _, err := backup.LocateLocal(c.Param("backup"))
|
2020-04-10 05:07:48 +00:00
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
if err := b.Remove(); err != nil {
|
2020-04-10 05:07:48 +00:00
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
2020-04-26 23:21:58 +00:00
|
|
|
}
|