mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-11-02 05:14:32 +08:00
fix: Fix PostgreSQL database backup/restore failures (#9854)
This commit is contained in:
parent
f617c9d5dd
commit
58b95ef2ba
5 changed files with 25 additions and 15 deletions
|
|
@ -11,7 +11,7 @@ type DBConfUpdateByFile struct {
|
||||||
type ChangeDBInfo struct {
|
type ChangeDBInfo struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
From string `json:"from" validate:"required,oneof=local remote"`
|
From string `json:"from" validate:"required,oneof=local remote"`
|
||||||
Type string `json:"type" validate:"required,oneof=mysql mariadb postgresql mysql-cluster postgresql-cluster redis-cluster"`
|
Type string `json:"type" validate:"required,oneof=mysql mariadb postgresql redis mysql-cluster postgresql-cluster redis-cluster"`
|
||||||
Database string `json:"database" validate:"required"`
|
Database string `json:"database" validate:"required"`
|
||||||
Value string `json:"value" validate:"required"`
|
Value string `json:"value" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,26 +60,20 @@ type PostgresqlPrivileges struct {
|
||||||
|
|
||||||
type PostgresqlLoadDB struct {
|
type PostgresqlLoadDB struct {
|
||||||
From string `json:"from" validate:"required,oneof=local remote"`
|
From string `json:"from" validate:"required,oneof=local remote"`
|
||||||
Type string `json:"type" validate:"required,oneof=postgresql"`
|
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
|
||||||
Database string `json:"database" validate:"required"`
|
Database string `json:"database" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostgresqlDBDeleteCheck struct {
|
type PostgresqlDBDeleteCheck struct {
|
||||||
ID uint `json:"id" validate:"required"`
|
ID uint `json:"id" validate:"required"`
|
||||||
Type string `json:"type" validate:"required,oneof=postgresql"`
|
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
|
||||||
Database string `json:"database" validate:"required"`
|
Database string `json:"database" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostgresqlDBDelete struct {
|
type PostgresqlDBDelete struct {
|
||||||
ID uint `json:"id" validate:"required"`
|
ID uint `json:"id" validate:"required"`
|
||||||
Type string `json:"type" validate:"required,oneof=postgresql"`
|
Type string `json:"type" validate:"required,oneof=postgresql postgresql-cluster"`
|
||||||
Database string `json:"database" validate:"required"`
|
Database string `json:"database" validate:"required"`
|
||||||
ForceDelete bool `json:"forceDelete"`
|
ForceDelete bool `json:"forceDelete"`
|
||||||
DeleteBackup bool `json:"deleteBackup"`
|
DeleteBackup bool `json:"deleteBackup"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostgresqlConfUpdateByFile struct {
|
|
||||||
Type string `json:"type" validate:"required,oneof=postgresql mariadb"`
|
|
||||||
Database string `json:"database" validate:"required"`
|
|
||||||
File string `json:"file"`
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,10 @@ func confSet(redisName string, redisType string, updateType string, changeConf [
|
||||||
}
|
}
|
||||||
newFiles = append(newFiles, files[i])
|
newFiles = append(newFiles, files[i])
|
||||||
}
|
}
|
||||||
|
if startIndex == 0 {
|
||||||
|
newFiles = append(newFiles, "# Redis configuration rewrite by 1Panel")
|
||||||
|
startIndex = len(newFiles) - 1
|
||||||
|
}
|
||||||
endIndex = endIndex - emptyLine
|
endIndex = endIndex - emptyLine
|
||||||
for _, item := range changeConf {
|
for _, item := range changeConf {
|
||||||
if item.key == "save" {
|
if item.key == "save" {
|
||||||
|
|
|
||||||
|
|
@ -234,7 +234,10 @@ func (r *Local) Backup(info BackupInfo) error {
|
||||||
dumpCmd = "mariadb-dump"
|
dumpCmd = "mariadb-dump"
|
||||||
}
|
}
|
||||||
global.LOG.Infof("start to %s | gzip > %s.gzip", dumpCmd, info.TargetDir+"/"+info.FileName)
|
global.LOG.Infof("start to %s | gzip > %s.gzip", dumpCmd, info.TargetDir+"/"+info.FileName)
|
||||||
cmd := exec.Command("docker", "exec", r.ContainerName, dumpCmd, "--routines", "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(ctx, "docker", "exec", r.ContainerName, dumpCmd, "--routines", "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
|
|
@ -258,7 +261,9 @@ func (r *Local) Recover(info RecoverInfo) error {
|
||||||
mysqlCli = "mysql"
|
mysqlCli = "mysql"
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("docker", "exec", "-i", r.ContainerName, mysqlCli, "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", r.ContainerName, mysqlCli, "-uroot", "-p"+r.Password, "--default-character-set="+info.Format, info.Name)
|
||||||
if strings.HasSuffix(info.SourceFile, ".gz") {
|
if strings.HasSuffix(info.SourceFile, ".gz") {
|
||||||
gzipFile, err := os.Open(info.SourceFile)
|
gzipFile, err := os.Open(info.SourceFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,11 @@ func (r *Local) Backup(info BackupInfo) error {
|
||||||
}
|
}
|
||||||
defer outfile.Close()
|
defer outfile.Close()
|
||||||
global.LOG.Infof("start to pg_dump | gzip > %s.gzip", info.TargetDir+"/"+info.FileName)
|
global.LOG.Infof("start to pg_dump | gzip > %s.gzip", info.TargetDir+"/"+info.FileName)
|
||||||
cmd := exec.Command(
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(
|
||||||
|
ctx,
|
||||||
"docker", "exec", "-i", r.ContainerName,
|
"docker", "exec", "-i", r.ContainerName,
|
||||||
"sh", "-c",
|
"sh", "-c",
|
||||||
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
|
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 {
|
||||||
func (r *Local) Recover(info RecoverInfo) error {
|
func (r *Local) Recover(info RecoverInfo) error {
|
||||||
fi, _ := os.Open(info.SourceFile)
|
fi, _ := os.Open(info.SourceFile)
|
||||||
defer fi.Close()
|
defer fi.Close()
|
||||||
cmd := exec.Command("docker", "exec", r.ContainerName, "sh", "-c",
|
|
||||||
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", r.ContainerName, "sh", "-c",
|
||||||
|
fmt.Sprintf("PGPASSWORD=%s pg_restore -F c -U %s -d %s", r.Password, r.Username, info.Name),
|
||||||
)
|
)
|
||||||
if strings.HasSuffix(info.SourceFile, ".gz") {
|
if strings.HasSuffix(info.SourceFile, ".gz") {
|
||||||
gzipFile, err := os.Open(info.SourceFile)
|
gzipFile, err := os.Open(info.SourceFile)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue