2024-08-07 13:52:34 +08:00
|
|
|
package v2
|
2024-07-23 14:48:37 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2024-08-07 13:52:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
|
2024-07-23 14:48:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-08-19 18:04:43 +08:00
|
|
|
func (b *BaseApi) CheckBackupUsed(c *gin.Context) {
|
|
|
|
id, err := helper.GetIntParamByKey(c, "id")
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := backupService.CheckUsed(id); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithOutData(c)
|
|
|
|
}
|
|
|
|
|
2024-07-23 14:48:37 +08:00
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Page backup records
|
|
|
|
// @Description 获取备份记录列表分页
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RecordSearch true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/record/search [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
func (b *BaseApi) SearchBackupRecords(c *gin.Context) {
|
|
|
|
var req dto.RecordSearch
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := backupService.SearchRecordsWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Page backup records by cronjob
|
|
|
|
// @Description 通过计划任务获取备份记录列表分页
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RecordSearchByCronjob true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/record/search/bycronjob [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
func (b *BaseApi) SearchBackupRecordsByCronjob(c *gin.Context) {
|
|
|
|
var req dto.RecordSearchByCronjob
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := backupService.SearchRecordsByCronjobWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Download backup record
|
|
|
|
// @Description 下载备份记录
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.DownloadRecord true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/record/download [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["source","fileName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"下载备份记录 [source][fileName]","formatEN":"download backup records [source][fileName]"}
|
|
|
|
func (b *BaseApi) DownloadRecord(c *gin.Context) {
|
|
|
|
var req dto.DownloadRecord
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err := backupService.DownloadRecord(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Delete backup record
|
|
|
|
// @Description 删除备份记录
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.BatchDeleteReq true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /record/del [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"backup_records","output_column":"file_name","output_value":"files"}],"formatZH":"删除备份记录 [files]","formatEN":"delete backup records [files]"}
|
|
|
|
func (b *BaseApi) DeleteBackupRecord(c *gin.Context) {
|
|
|
|
var req dto.BatchDeleteReq
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := backupService.BatchDeleteRecord(req.Ids); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary List files from backup accounts
|
|
|
|
// @Description 获取备份账号内文件列表
|
|
|
|
// @Accept json
|
2024-08-13 15:33:34 +08:00
|
|
|
// @Param request body dto.OperateByID true "request"
|
2024-07-23 14:48:37 +08:00
|
|
|
// @Success 200 {array} string
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/search/files [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
func (b *BaseApi) LoadFilesFromBackup(c *gin.Context) {
|
2024-08-13 15:33:34 +08:00
|
|
|
var req dto.OperateByID
|
2024-07-23 14:48:37 +08:00
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := backupService.ListFiles(req)
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Backup system data
|
|
|
|
// @Description 备份系统数据
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.CommonBackup true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/backup [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["type","name","detailName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"备份 [type] 数据 [name][detailName]","formatEN":"backup [type] data [name][detailName]"}
|
|
|
|
func (b *BaseApi) Backup(c *gin.Context) {
|
|
|
|
var req dto.CommonBackup
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Type {
|
|
|
|
case "app":
|
|
|
|
if _, err := backupService.AppBackup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "mysql", "mariadb":
|
|
|
|
if err := backupService.MysqlBackup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case constant.AppPostgresql:
|
|
|
|
if err := backupService.PostgresqlBackup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "website":
|
|
|
|
if err := backupService.WebsiteBackup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "redis":
|
|
|
|
if err := backupService.RedisBackup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Recover system data
|
|
|
|
// @Description 恢复系统数据
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.CommonRecover true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/recover [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["type","name","detailName","file"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [file] 恢复 [type] 数据 [name][detailName]","formatEN":"recover [type] data [name][detailName] from [file]"}
|
|
|
|
func (b *BaseApi) Recover(c *gin.Context) {
|
|
|
|
var req dto.CommonRecover
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-13 15:33:34 +08:00
|
|
|
downloadPath, err := backupService.DownloadRecord(dto.DownloadRecord{
|
2024-08-14 10:15:55 +08:00
|
|
|
DownloadAccountID: req.DownloadAccountID,
|
2024-08-13 15:33:34 +08:00
|
|
|
FileDir: path.Dir(req.File),
|
|
|
|
FileName: path.Base(req.File),
|
|
|
|
})
|
2024-07-23 14:48:37 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, fmt.Errorf("download file failed, err: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.File = downloadPath
|
|
|
|
switch req.Type {
|
|
|
|
case "mysql", "mariadb":
|
|
|
|
if err := backupService.MysqlRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case constant.AppPostgresql:
|
|
|
|
if err := backupService.PostgresqlRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "website":
|
|
|
|
if err := backupService.WebsiteRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "redis":
|
|
|
|
if err := backupService.RedisRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "app":
|
|
|
|
if err := backupService.AppRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Recover system data by upload
|
|
|
|
// @Description 从上传恢复系统数据
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.CommonRecover true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-21 15:14:50 +08:00
|
|
|
// @Router /backup/recover/byupload [post]
|
2024-07-23 14:48:37 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["type","name","detailName","file"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [file] 恢复 [type] 数据 [name][detailName]","formatEN":"recover [type] data [name][detailName] from [file]"}
|
|
|
|
func (b *BaseApi) RecoverByUpload(c *gin.Context) {
|
|
|
|
var req dto.CommonRecover
|
|
|
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Type {
|
|
|
|
case "mysql", "mariadb":
|
|
|
|
if err := backupService.MysqlRecoverByUpload(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case constant.AppPostgresql:
|
|
|
|
if err := backupService.PostgresqlRecoverByUpload(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "app":
|
|
|
|
if err := backupService.AppRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "website":
|
|
|
|
if err := backupService.WebsiteRecover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|