mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 07:57:21 +08:00
feat: postgres添加密码支持
This commit is contained in:
@@ -531,7 +531,11 @@ func (r *backupRepo) createMySQL(name string, storage storage.Storage, target st
|
||||
|
||||
// createPostgres 创建 PostgreSQL 备份
|
||||
func (r *backupRepo) createPostgres(name string, storage storage.Storage, target string) error {
|
||||
postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", 5432)
|
||||
postgresPassword, err := r.setting.Get(biz.SettingKeyPostgresPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
postgres, err := db.NewPostgres("postgres", postgresPassword, "127.0.0.1", 5432)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -553,9 +557,11 @@ func (r *backupRepo) createPostgres(name string, storage storage.Storage, target
|
||||
|
||||
// 导出数据库
|
||||
name = name + ".sql"
|
||||
if _, err = shell.Execf(`su - postgres -c "pg_dump '%s'" > '%s'`, target, filepath.Join(tmpDir, name)); err != nil {
|
||||
_ = os.Setenv("PGPASSWORD", postgresPassword)
|
||||
if _, err = shell.Execf(`pg_dump -h 127.0.0.1 -U postgres '%s' > '%s'`, target, filepath.Join(tmpDir, name)); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = os.Unsetenv("PGPASSWORD")
|
||||
|
||||
// 压缩备份文件
|
||||
if err = io.Compress(tmpDir, []string{name}, filepath.Join(tmpDir, name+".zip")); err != nil {
|
||||
@@ -618,9 +624,6 @@ func (r *backupRepo) restoreMySQL(backup, target string) error {
|
||||
if exist, _ := mysql.DatabaseExists(target); !exist {
|
||||
return errors.New(r.t.Get("database does not exist: %s", target))
|
||||
}
|
||||
if err = os.Setenv("MYSQL_PWD", rootPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clean := false
|
||||
if !strings.HasSuffix(backup, ".sql") {
|
||||
@@ -631,12 +634,11 @@ func (r *backupRepo) restoreMySQL(backup, target string) error {
|
||||
clean = true
|
||||
}
|
||||
|
||||
_ = os.Setenv("MYSQL_PWD", rootPassword)
|
||||
if _, err = shell.Execf(`mysql -u root '%s' < '%s'`, target, backup); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = os.Unsetenv("MYSQL_PWD"); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = os.Unsetenv("MYSQL_PWD")
|
||||
if clean {
|
||||
_ = io.Remove(filepath.Dir(backup))
|
||||
}
|
||||
@@ -646,7 +648,11 @@ func (r *backupRepo) restoreMySQL(backup, target string) error {
|
||||
|
||||
// restorePostgres 恢复 PostgreSQL 备份
|
||||
func (r *backupRepo) restorePostgres(backup, target string) error {
|
||||
postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", 5432)
|
||||
postgresPassword, err := r.setting.Get(biz.SettingKeyPostgresPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
postgres, err := db.NewPostgres("postgres", postgresPassword, "127.0.0.1", 5432)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -664,9 +670,11 @@ func (r *backupRepo) restorePostgres(backup, target string) error {
|
||||
clean = true
|
||||
}
|
||||
|
||||
if _, err = shell.Execf(`su - postgres -c "psql '%s'" < '%s'`, target, backup); err != nil {
|
||||
_ = os.Setenv("PGPASSWORD", postgresPassword)
|
||||
if _, err = shell.Execf(`psql -h 127.0.0.1 -U postgres '%s' < '%s'`, target, backup); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = os.Unsetenv("PGPASSWORD")
|
||||
if clean {
|
||||
_ = io.Remove(filepath.Dir(backup))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user