Skip to content

Commit 58b95ef

Browse files
authored
fix: Fix PostgreSQL database backup/restore failures (1Panel-dev#9854)
1 parent f617c9d commit 58b95ef

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

agent/app/dto/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type DBConfUpdateByFile struct {
1111
type ChangeDBInfo struct {
1212
ID uint `json:"id"`
1313
From string `json:"from" validate:"required,oneof=local remote"`
14-
Type string `json:"type" validate:"required,oneof=mysql mariadb postgresql mysql-cluster postgresql-cluster redis-cluster"`
14+
Type string `json:"type" validate:"required,oneof=mysql mariadb postgresql redis mysql-cluster postgresql-cluster redis-cluster"`
1515
Database string `json:"database" validate:"required"`
1616
Value string `json:"value" validate:"required"`
1717
}

agent/app/dto/database_postgresql.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,20 @@ type PostgresqlPrivileges struct {
6060

6161
type PostgresqlLoadDB struct {
6262
From string `json:"from" validate:"required,oneof=local remote"`
63-
Type string `json:"type" validate:"required,oneof=postgresql"`
63+
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
6464
Database string `json:"database" validate:"required"`
6565
}
6666

6767
type PostgresqlDBDeleteCheck struct {
6868
ID uint `json:"id" validate:"required"`
69-
Type string `json:"type" validate:"required,oneof=postgresql"`
69+
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
7070
Database string `json:"database" validate:"required"`
7171
}
7272

7373
type PostgresqlDBDelete struct {
7474
ID uint `json:"id" validate:"required"`
75-
Type string `json:"type" validate:"required,oneof=postgresql"`
75+
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
7676
Database string `json:"database" validate:"required"`
7777
ForceDelete bool `json:"forceDelete"`
7878
DeleteBackup bool `json:"deleteBackup"`
7979
}
80-
81-
type PostgresqlConfUpdateByFile struct {
82-
Type string `json:"type" validate:"required,oneof=postgresql mariadb"`
83-
Database string `json:"database" validate:"required"`
84-
File string `json:"file"`
85-
}

agent/app/service/database_redis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ func confSet(redisName string, redisType string, updateType string, changeConf [
244244
}
245245
newFiles = append(newFiles, files[i])
246246
}
247+
if startIndex == 0 {
248+
newFiles = append(newFiles, "# Redis configuration rewrite by 1Panel")
249+
startIndex = len(newFiles) - 1
250+
}
247251
endIndex = endIndex - emptyLine
248252
for _, item := range changeConf {
249253
if item.key == "save" {

agent/utils/mysql/client/local.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ func (r *Local) Backup(info BackupInfo) error {
234234
dumpCmd = "mariadb-dump"
235235
}
236236
global.LOG.Infof("start to %s | gzip > %s.gzip", dumpCmd, info.TargetDir+"/"+info.FileName)
237-
cmd := exec.Command("docker", "exec", r.ContainerName, dumpCmd, "--routines", "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
237+
238+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
239+
defer cancel()
240+
cmd := exec.CommandContext(ctx, "docker", "exec", r.ContainerName, dumpCmd, "--routines", "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
238241
var stderr bytes.Buffer
239242
cmd.Stderr = &stderr
240243

@@ -258,7 +261,9 @@ func (r *Local) Recover(info RecoverInfo) error {
258261
mysqlCli = "mysql"
259262
}
260263

261-
cmd := exec.Command("docker", "exec", "-i", r.ContainerName, mysqlCli, "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
264+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
265+
defer cancel()
266+
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", r.ContainerName, mysqlCli, "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
262267
if strings.HasSuffix(info.SourceFile, ".gz") {
263268
gzipFile, err := os.Open(info.SourceFile)
264269
if err != nil {

agent/utils/postgresql/client/local.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ func (r *Local) Backup(info BackupInfo) error {
136136
}
137137
defer outfile.Close()
138138
global.LOG.Infof("start to pg_dump | gzip > %s.gzip", info.TargetDir+"/"+info.FileName)
139-
cmd := exec.Command(
139+
140+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
141+
defer cancel()
142+
cmd := exec.CommandContext(
143+
ctx,
140144
"docker", "exec", "-i", r.ContainerName,
141145
"sh", "-c",
142146
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
@@ -159,8 +163,11 @@ func (r *Local) Backup(info BackupInfo) error {
159163
func (r *Local) Recover(info RecoverInfo) error {
160164
fi, _ := os.Open(info.SourceFile)
161165
defer fi.Close()
162-
cmd := exec.Command("docker", "exec", r.ContainerName, "sh", "-c",
163-
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
166+
167+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
168+
defer cancel()
169+
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", r.ContainerName, "sh", "-c",
170+
fmt.Sprintf("PGPASSWORD=%s pg_restore -F c -U %s -d %s", r.Password, r.Username, info.Name),
164171
)
165172
if strings.HasSuffix(info.SourceFile, ".gz") {
166173
gzipFile, err := os.Open(info.SourceFile)

0 commit comments

Comments
 (0)