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"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
@ -208,29 +207,25 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
return err
}
}
exStr := []string{}
exStr = append(exStr, "zcvf")
exStr = append(exStr, targetDir+"/"+name)
excludes := strings.Split(exclusionRules, ";")
excludeRules := ""
for _, exclude := range excludes {
if len(exclude) == 0 {
continue
}
exStr = append(exStr, "--exclude")
exStr = append(exStr, exclude)
excludeRules += (" --exclude" + exclude)
}
path := ""
if len(strings.Split(sourceDir, "/")) > 3 {
exStr = append(exStr, "-C")
itemDir := strings.ReplaceAll(sourceDir[strings.LastIndex(sourceDir, "/"):], "/", "")
aheadDir := strings.ReplaceAll(sourceDir, itemDir, "")
exStr = append(exStr, aheadDir)
exStr = append(exStr, itemDir)
path += fmt.Sprintf("-C %s %s", aheadDir, itemDir)
} else {
exStr = append(exStr, sourceDir)
path = sourceDir
}
cmd := exec.Command("tar", exStr...)
stdout, err := cmd.CombinedOutput()
fmt.Println(string(stdout))
stdout, err := cmd.Execf("tar zcvf %s %s %s", targetDir+"/"+name, excludeRules, path)
if err != nil {
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.CombinedOutput()
stdout, err := cmd.Execf("tar zxvfC %s %s", sourceFile, targetDir)
if err != nil {
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/constant"
"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/compose"
"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) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
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 ") {
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) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
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 ") {
return nil, errors.New(stdStr)
@ -617,8 +616,7 @@ func excuteSqlForRows(containerName, password, command string) ([]string, error)
}
func excuteSql(containerName, password, command string) error {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker exec %s mysql -uroot -p%s -e %s", containerName, password, command)
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 ") {
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/constant"
"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/go-sql-driver/mysql"
)
@ -160,9 +161,8 @@ func (u *RedisService) Backup() error {
if err != nil {
return err
}
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), "save")
cmd := exec.Command("docker", commands...)
if stdout, err := cmd.CombinedOutput(); err != nil {
stdout, err := cmd.Execf("docker exec %s redis-cli -a %s --no-auth-warning save", redisInfo.ContainerName, redisInfo.Password)
if err != nil {
return errors.New(string(stdout))
}
localDir, err := loadLocalDir()
@ -195,9 +195,9 @@ func (u *RedisService) Backup() error {
}
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))
if stdout, err := cmd2.CombinedOutput(); err != nil {
return errors.New(string(stdout))
stdout1, err1 := cmd.Execf("docker cp %s:/data/dump.rdb %s/%s", redisInfo.ContainerName, fullDir, name)
if err1 != nil {
return errors.New(string(stdout1))
}
return nil
}

View file

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

View file

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

View file

@ -2,6 +2,7 @@ package cmd
import (
"bytes"
"fmt"
"os/exec"
)
@ -16,3 +17,15 @@ func Exec(cmdStr string) (string, error) {
}
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
import (
"os/exec"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
)
func Up(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "up", "-d")
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
return string(stdout), err
}
func Down(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "down")
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s down", filePath)
return string(stdout), err
}
func Stop(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "stop")
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
return string(stdout), err
}
func Restart(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "restart")
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s restart", filePath)
return string(stdout), err
}
func Operate(filePath, operation string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, operation)
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s %s", filePath, operation)
return string(stdout), err
}
func Rmf(filePath string) (string, error) {
cmd := exec.Command("docker-compose", "-f", filePath, "rm", "-f")
stdout, err := cmd.CombinedOutput()
stdout, err := cmd.Execf("docker-compose -f %s rm -f", filePath)
return string(stdout), err
}

View file

@ -444,9 +444,13 @@ const onDownload = async (recordID: number, backupID: number) => {
a.style.display = 'none';
a.href = downloadUrl;
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 {
a.download = dateFormatForName(currentRecord.value?.startTime) + '.tar.gz';
a.download = dateFormatForName(currentRecord.value?.startTime) + '.sql.gz';
}
const event = new MouseEvent('click');
a.dispatchEvent(event);

View file

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