Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (security): Clean PostgreSQL creds after restore

Rostislav Dugin82eca7501bc9
diff --git a/README.md b/README.md
index 475ff1b1..0e26a4f5 100644
--- a/README.md
+++ b/README.md
@@ -58,12 +58,12 @@
- **SSL support**: Secure connections available
- **Easy restoration**: One-click restore from any backup
-### πŸ”’ **Backup Encryption** <a href="https://postgresus.com/encryption">(docs)</a>
+### πŸ”’ **Enterprise-grade security** <a href="https://postgresus.com/security">(docs)</a>
- **AES-256-GCM encryption**: Enterprise-grade protection for backup files
-- **Zero-trust storage**: Encrypted backups are useless so you can keep in shared storages like S3, Azure Blob Storage, etc.
-- **Optionality**: Encrypted backups are optional and can be enabled or disabled if you wish
-- **Download unencrypted**: You can still download unencrypted backups via the 'Download' button to use them in `pg_restore` or other tools.
+- **Zero-trust storage**: Backups are encrypted and they are useless to attackers, so you can keep them in shared storages like S3, Azure Blob Storage, etc.
+- **Encryption for secrets**: Any sensitive data is encrypted and never exposed, even in logs or error messages
+- **Read-only user**: Postgresus uses by default a read-only user for backups and never stores anything that can change your data
### πŸ‘₯ **Suitable for Teams** <a href="https://postgresus.com/access-management">(docs)</a>
diff --git a/backend/internal/features/databases/databases/postgresql/model.go b/backend/internal/features/databases/databases/postgresql/model.go
index e51a1673..30682b4a 100644
--- a/backend/internal/features/databases/databases/postgresql/model.go
+++ b/backend/internal/features/databases/databases/postgresql/model.go
@@ -19,7 +19,6 @@ type PostgresqlDatabase struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
DatabaseID *uuid.UUID `json:"databaseId" gorm:"type:uuid;column:database_id"`
- RestoreID *uuid.UUID `json:"restoreId" gorm:"type:uuid;column:restore_id"`
Version tools.PostgresqlVersion `json:"version" gorm:"type:text;not null"`
diff --git a/backend/internal/features/restores/models/model.go b/backend/internal/features/restores/models/model.go
index acaf46bc..effb0323 100644
--- a/backend/internal/features/restores/models/model.go
+++ b/backend/internal/features/restores/models/model.go
@@ -2,7 +2,6 @@ package models
import (
"postgresus-backend/internal/features/backups/backups"
- "postgresus-backend/internal/features/databases/databases/postgresql"
"postgresus-backend/internal/features/restores/enums"
"time"
@@ -16,8 +15,6 @@ type Restore struct {
BackupID uuid.UUID `json:"backupId" gorm:"column:backup_id;type:uuid;not null"`
Backup *backups.Backup
- Postgresql *postgresql.PostgresqlDatabase `json:"postgresql,omitempty" gorm:"foreignKey:RestoreID"`
-
FailMessage *string `json:"failMessage" gorm:"column:fail_message"`
RestoreDurationMs int64 `json:"restoreDurationMs" gorm:"column:restore_duration_ms;default:0"`
diff --git a/backend/internal/features/restores/repository.go b/backend/internal/features/restores/repository.go
index edc157ca..5061bdd2 100644
--- a/backend/internal/features/restores/repository.go
+++ b/backend/internal/features/restores/repository.go
@@ -32,7 +32,6 @@ func (r *RestoreRepository) FindByBackupID(backupID uuid.UUID) ([]*models.Restor
if err := storage.
GetDb().
Preload("Backup").
- Preload("Postgresql").
Where("backup_id = ?", backupID).
Order("created_at DESC").
Find(&restores).Error; err != nil {
@@ -48,7 +47,6 @@ func (r *RestoreRepository) FindByID(id uuid.UUID) (*models.Restore, error) {
if err := storage.
GetDb().
Preload("Backup").
- Preload("Postgresql").
Where("id = ?", id).
First(&restore).Error; err != nil {
return nil, err
@@ -63,7 +61,6 @@ func (r *RestoreRepository) FindByStatus(status enums.RestoreStatus) ([]*models.
if err := storage.
GetDb().
Preload("Backup").
- Preload("Postgresql").
Where("status = ?", status).
Order("created_at DESC").
Find(&restores).Error; err != nil {
diff --git a/backend/internal/features/restores/service.go b/backend/internal/features/restores/service.go
index 52831d2b..0b1e28ce 100644
--- a/backend/internal/features/restores/service.go
+++ b/backend/internal/features/restores/service.go
@@ -191,15 +191,9 @@ func (s *RestoreService) RestoreBackup(
return err
}
- // Set the RestoreID on the PostgreSQL database and save it
- if requestDTO.PostgresqlDatabase != nil {
- requestDTO.PostgresqlDatabase.RestoreID = &restore.ID
- restore.Postgresql = requestDTO.PostgresqlDatabase
-
- // Save the restore again to include the postgresql database
- if err := s.restoreRepository.Save(&restore); err != nil {
- return err
- }
+ // Save the restore again to include the postgresql database
+ if err := s.restoreRepository.Save(&restore); err != nil {
+ return err
}
storage, err := s.storageService.GetStorageByID(backup.StorageID)
@@ -216,10 +210,15 @@ func (s *RestoreService) RestoreBackup(
start := time.Now().UTC()
+ restoringToDB := &databases.Database{
+ Postgresql: requestDTO.PostgresqlDatabase,
+ }
+
err = s.restoreBackupUsecase.Execute(
backupConfig,
restore,
database,
+ restoringToDB,
backup,
storage,
)
diff --git a/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go b/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
index c18190a2..f19b23b7 100644
--- a/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
+++ b/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
@@ -36,13 +36,14 @@ type RestorePostgresqlBackupUsecase struct {
}
func (uc *RestorePostgresqlBackupUsecase) Execute(
- database *databases.Database,
+ originalDB *databases.Database,
+ restoringToDB *databases.Database,
backupConfig *backups_config.BackupConfig,
restore models.Restore,
backup *backups.Backup,
storage *storages.Storage,
) error {
- if database.Type != databases.DatabaseTypePostgres {
+ if originalDB.Type != databases.DatabaseTypePostgres {
return errors.New("database type not supported")
}
@@ -54,7 +55,7 @@ func (uc *RestorePostgresqlBackupUsecase) Execute(
backup.ID,
)
- pg := restore.Postgresql
+ pg := restoringToDB.Postgresql
if pg == nil {
return fmt.Errorf("postgresql configuration is required for restore")
}
@@ -83,7 +84,7 @@ func (uc *RestorePostgresqlBackupUsecase) Execute(
}
return uc.restoreFromStorage(
- database,
+ originalDB,
tools.GetPostgresqlExecutable(
pg.Version,
"pg_restore",
diff --git a/backend/internal/features/restores/usecases/restore_backup_uc.go b/backend/internal/features/restores/usecases/restore_backup_uc.go
index 6c79951c..92fd16c7 100644
--- a/backend/internal/features/restores/usecases/restore_backup_uc.go
+++ b/backend/internal/features/restores/usecases/restore_backup_uc.go
@@ -17,13 +17,15 @@ type RestoreBackupUsecase struct {
func (uc *RestoreBackupUsecase) Execute(
backupConfig *backups_config.BackupConfig,
restore models.Restore,
- database *databases.Database,
+ originalDB *databases.Database,
+ restoringToDB *databases.Database,
backup *backups.Backup,
storage *storages.Storage,
) error {
- if database.Type == databases.DatabaseTypePostgres {
+ if originalDB.Type == databases.DatabaseTypePostgres {
return uc.restorePostgresqlBackupUsecase.Execute(
- database,
+ originalDB,
+ restoringToDB,
backupConfig,
restore,
backup,
diff --git a/backend/migrations/20251121171746_remove_restore_id_from_postgresql_databases.sql b/backend/migrations/20251121171746_remove_restore_id_from_postgresql_databases.sql
new file mode 100644
index 00000000..d61c6b53
--- /dev/null
+++ b/backend/migrations/20251121171746_remove_restore_id_from_postgresql_databases.sql
@@ -0,0 +1,28 @@
+-- +goose Up
+-- +goose StatementBegin
+
+ALTER TABLE postgresql_databases
+ DROP CONSTRAINT IF EXISTS fk_postgresql_databases_restore_id;
+
+DROP INDEX IF EXISTS idx_postgresql_databases_restore_id;
+
+ALTER TABLE postgresql_databases
+ DROP COLUMN IF EXISTS restore_id;
+
+-- +goose StatementEnd
+
+-- +goose Down
+-- +goose StatementBegin
+
+ALTER TABLE postgresql_databases
+ ADD COLUMN restore_id UUID;
+
+CREATE INDEX idx_postgresql_databases_restore_id ON postgresql_databases (restore_id);
+
+ALTER TABLE postgresql_databases
+ ADD CONSTRAINT fk_postgresql_databases_restore_id
+ FOREIGN KEY (restore_id)
+ REFERENCES restores (id)
+ ON DELETE CASCADE;
+
+-- +goose StatementEnd