fix: 计划任务下载文件增加前缀

This commit is contained in:
ssongliu
2023-02-14 14:19:26 +08:00
committed by ssongliu
parent b0e23ec2c7
commit 81df97be14
10 changed files with 58 additions and 67 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strings" "strings"
"time" "time"
@@ -208,29 +207,25 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
return err return err
} }
} }
exStr := []string{}
exStr = append(exStr, "zcvf")
exStr = append(exStr, targetDir+"/"+name)
excludes := strings.Split(exclusionRules, ";") excludes := strings.Split(exclusionRules, ";")
excludeRules := ""
for _, exclude := range excludes { for _, exclude := range excludes {
if len(exclude) == 0 { if len(exclude) == 0 {
continue continue
} }
exStr = append(exStr, "--exclude") excludeRules += (" --exclude" + exclude)
exStr = append(exStr, exclude)
} }
path := ""
if len(strings.Split(sourceDir, "/")) > 3 { if len(strings.Split(sourceDir, "/")) > 3 {
exStr = append(exStr, "-C")
itemDir := strings.ReplaceAll(sourceDir[strings.LastIndex(sourceDir, "/"):], "/", "") itemDir := strings.ReplaceAll(sourceDir[strings.LastIndex(sourceDir, "/"):], "/", "")
aheadDir := strings.ReplaceAll(sourceDir, itemDir, "") aheadDir := strings.ReplaceAll(sourceDir, itemDir, "")
exStr = append(exStr, aheadDir) path += fmt.Sprintf("-C %s %s", aheadDir, itemDir)
exStr = append(exStr, itemDir)
} else { } else {
exStr = append(exStr, sourceDir) path = sourceDir
} }
cmd := exec.Command("tar", exStr...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.Execf("tar zcvf %s %s %s", targetDir+"/"+name, excludeRules, path)
fmt.Println(string(stdout))
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
@@ -244,8 +239,7 @@ func handleUnTar(sourceFile, targetDir string) error {
} }
} }
cmd := exec.Command("tar", "zxvfC", sourceFile, targetDir) stdout, err := cmd.Execf("tar zxvfC %s %s", sourceFile, targetDir)
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }

View File

@@ -19,6 +19,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
@@ -588,8 +589,7 @@ func (u *MysqlService) LoadStatus() (*dto.MysqlStatus, error) {
} }
func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) { func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command) stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") { if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return nil, errors.New(stdStr) return nil, errors.New(stdStr)
@@ -607,8 +607,7 @@ func excuteSqlForMaps(containerName, password, command string) (map[string]strin
} }
func excuteSqlForRows(containerName, password, command string) ([]string, error) { func excuteSqlForRows(containerName, password, command string) ([]string, error) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command) stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") { if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return nil, errors.New(stdStr) return nil, errors.New(stdStr)
@@ -617,8 +616,7 @@ func excuteSqlForRows(containerName, password, command string) ([]string, error)
} }
func excuteSql(containerName, password, command string) error { func excuteSql(containerName, password, command string) error {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command) stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") { if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return errors.New(stdStr) return errors.New(stdStr)

View File

@@ -14,6 +14,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@@ -160,9 +161,8 @@ func (u *RedisService) Backup() error {
if err != nil { if err != nil {
return err return err
} }
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), "save") stdout, err := cmd.Execf("docker exec %s redis-cli -a %s --no-auth-warning save", redisInfo.ContainerName, redisInfo.Password)
cmd := exec.Command("docker", commands...) if err != nil {
if stdout, err := cmd.CombinedOutput(); err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
localDir, err := loadLocalDir() localDir, err := loadLocalDir()
@@ -195,9 +195,9 @@ func (u *RedisService) Backup() error {
} }
name := fmt.Sprintf("%s.rdb", time.Now().Format("20060102150405")) name := fmt.Sprintf("%s.rdb", time.Now().Format("20060102150405"))
cmd2 := exec.Command("docker", "cp", fmt.Sprintf("%s:/data/dump.rdb", redisInfo.ContainerName), fmt.Sprintf("%s/%s", fullDir, name)) stdout1, err1 := cmd.Execf("docker cp %s:/data/dump.rdb %s/%s", redisInfo.ContainerName, fullDir, name)
if stdout, err := cmd2.CombinedOutput(); err != nil { if err1 != nil {
return errors.New(string(stdout)) return errors.New(string(stdout1))
} }
return nil return nil
} }

View File

@@ -6,12 +6,12 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path" "path"
"strings" "strings"
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@@ -50,8 +50,7 @@ func (u *DockerService) LoadDockerStatus() string {
func (u *DockerService) LoadDockerConf() *dto.DaemonJsonConf { func (u *DockerService) LoadDockerConf() *dto.DaemonJsonConf {
status := constant.StatusRunning status := constant.StatusRunning
cmd := exec.Command("systemctl", "is-active", "docker") stdout, err := cmd.Exec("systemctl is-active docker")
stdout, err := cmd.CombinedOutput()
if string(stdout) != "active\n" || err != nil { if string(stdout) != "active\n" || err != nil {
status = constant.Stopped status = constant.Stopped
} }
@@ -158,8 +157,7 @@ func (u *DockerService) UpdateConf(req dto.DaemonJsonConf) error {
return err return err
} }
cmd := exec.Command("systemctl", "restart", "docker") stdout, err := cmd.Exec("systemctl restart docker")
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
@@ -176,8 +174,7 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error {
_, _ = write.WriteString(req.File) _, _ = write.WriteString(req.File)
write.Flush() write.Flush()
cmd := exec.Command("systemctl", "restart", "docker") stdout, err := cmd.Exec("systemctl restart docker")
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
@@ -185,8 +182,7 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error {
} }
func (u *DockerService) OperateDocker(req dto.DockerOperation) error { func (u *DockerService) OperateDocker(req dto.DockerOperation) error {
cmd := exec.Command("systemctl", req.Operation, "docker") stdout, err := cmd.Execf("systemctl %s docker ", req.Operation)
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }

View File

@@ -2,10 +2,8 @@ package service
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path" "path"
"strings" "strings"
@@ -109,15 +107,13 @@ func (u *ImageRepoService) BatchDelete(req dto.ImageRepoDelete) error {
_ = u.handleRegistries("", repo.DownloadUrl, "delete") _ = u.handleRegistries("", repo.DownloadUrl, "delete")
} }
if repo.Auth { if repo.Auth {
cmd := exec.Command("docker", "logout", fmt.Sprintf("%s://%s", repo.Protocol, repo.DownloadUrl)) _, _ = cmd.Execf("docker logout %s://%s", repo.Protocol, repo.DownloadUrl)
_, _ = cmd.CombinedOutput()
} }
} }
if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil { if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil {
return err return err
} }
cmd := exec.Command("systemctl", "restart", "docker") stdout, err := cmd.Exec("systemctl restart docker")
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
@@ -136,11 +132,9 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
if repo.DownloadUrl != req.DownloadUrl { if repo.DownloadUrl != req.DownloadUrl {
_ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update") _ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update")
if repo.Auth { if repo.Auth {
cmd := exec.Command("docker", "logout", repo.DownloadUrl) _, _ = cmd.Execf("docker logout %s", repo.DownloadUrl)
_, _ = cmd.CombinedOutput()
} }
cmd := exec.Command("systemctl", "restart", "docker") stdout, err := cmd.Exec("systemctl restart docker")
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }
@@ -163,8 +157,7 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
} }
func (u *ImageRepoService) checkConn(host, user, password string) error { func (u *ImageRepoService) checkConn(host, user, password string) error {
cmd := exec.Command("docker", "login", "-u", user, "-p", password, host) stdout, err := cmd.Execf("docker login -u %s -p %s %s", user, password, host)
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }

View File

@@ -17,6 +17,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/1Panel-dev/1Panel/backend/utils/nginx" "github.com/1Panel-dev/1Panel/backend/utils/nginx"
@@ -501,8 +502,7 @@ func handleWebsiteRecover(website *model.Website, fileDir string) error {
if err := handleUnTar(fmt.Sprintf("%s/%s.web.tar.gz", fileDir, website.Alias), siteDir); err != nil { if err := handleUnTar(fmt.Sprintf("%s/%s.web.tar.gz", fileDir, website.Alias), siteDir); err != nil {
return err return err
} }
cmd := exec.Command("docker", "exec", "-i", nginxInfo.ContainerName, "nginx", "-s", "reload") stdout, err := cmd.Execf("docker exec -i %s nginx -s reload", nginxInfo.ContainerName)
stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
} }

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"bytes" "bytes"
"fmt"
"os/exec" "os/exec"
) )
@@ -16,3 +17,15 @@ func Exec(cmdStr string) (string, error) {
} }
return stdout.String(), nil return stdout.String(), nil
} }
func Execf(cmdStr string, a ...interface{}) (string, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return stderr.String(), err
}
return stdout.String(), nil
}

View File

@@ -1,45 +1,38 @@
package compose package compose
import ( import (
"os/exec" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
) )
func Up(filePath string) (string, error) { func Up(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "up", "-d") stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
func Down(filePath string) (string, error) { func Down(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "down") stdout, err := cmd.Execf("docker-compose -f %s down", filePath)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
func Stop(filePath string) (string, error) { func Stop(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "stop") stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
func Restart(filePath string) (string, error) { func Restart(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "restart") stdout, err := cmd.Execf("docker-compose -f %s restart", filePath)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
func Operate(filePath, operation string) (string, error) { func Operate(filePath, operation string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, operation) stdout, err := cmd.Execf("docker-compose -f %s %s", filePath, operation)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
func Rmf(filePath string) (string, error) { func Rmf(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "rm", "-f") stdout, err := cmd.Execf("docker-compose -f %s rm -f", filePath)
stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }

View File

@@ -444,9 +444,13 @@ const onDownload = async (recordID: number, backupID: number) => {
a.style.display = 'none'; a.style.display = 'none';
a.href = downloadUrl; a.href = downloadUrl;
if (dialogData.value.rowData!.type === 'database') { if (dialogData.value.rowData!.type === 'database') {
a.download = dateFormatForName(currentRecord.value?.startTime) + '.sql.gz'; a.download =
dialogData.value.rowData!.dbName + '_' + dateFormatForName(currentRecord.value?.startTime) + '.sql.gz';
} else if (dialogData.value.rowData!.type === 'website') {
a.download =
dialogData.value.rowData!.website + '_' + dateFormatForName(currentRecord.value?.startTime) + '.tar.gz';
} else { } else {
a.download = dateFormatForName(currentRecord.value?.startTime) + '.tar.gz'; a.download = dateFormatForName(currentRecord.value?.startTime) + '.sql.gz';
} }
const event = new MouseEvent('click'); const event = new MouseEvent('click');
a.dispatchEvent(event); a.dispatchEvent(event);

View File

@@ -7,7 +7,7 @@
<el-button type="primary" @click="onCreate()"> <el-button type="primary" @click="onCreate()">
{{ $t('setting.createSnapshot') }} {{ $t('setting.createSnapshot') }}
</el-button> </el-button>
<el-button @click="onImport()"> <el-button type="primary" plain @click="onImport()">
{{ $t('setting.importSnapshot') }} {{ $t('setting.importSnapshot') }}
</el-button> </el-button>
<el-button type="primary" plain :disabled="selects.length === 0" @click="batchDelete(null)"> <el-button type="primary" plain :disabled="selects.length === 0" @click="batchDelete(null)">