PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FIX (restores): Validate PG version via DB and do not allow to restore from lower version
Rostislav Dugin6e6b642f4ac1
diff --git a/backend/internal/features/databases/databases/postgresql/model.go b/backend/internal/features/databases/databases/postgresql/model.goindex 0a3eafd9..37830825 100644--- a/backend/internal/features/databases/databases/postgresql/model.go+++ b/backend/internal/features/databases/databases/postgresql/model.go@@ -6,6 +6,7 @@ import ("fmt""log/slog""postgresus-backend/internal/util/tools"+ "regexp""time""github.com/google/uuid"@@ -93,6 +94,11 @@ func testSingleDatabaseConnection(}}()+ // Check version after successful connection+ if err := verifyDatabaseVersion(ctx, conn, postgresDb.Version); err != nil {+ return err+ }+// Test if we can perform basic operations (like pg_dump would need)if err := testBasicOperations(ctx, conn, *postgresDb.Database); err != nil {return fmt.Errorf(@@ -105,6 +111,37 @@ func testSingleDatabaseConnection(return nil}+// verifyDatabaseVersion checks if the actual database version matches the specified version+func verifyDatabaseVersion(+ ctx context.Context,+ conn *pgx.Conn,+ expectedVersion tools.PostgresqlVersion,+) error {+ var versionStr string+ err := conn.QueryRow(ctx, "SELECT version()").Scan(&versionStr)+ if err != nil {+ return fmt.Errorf("failed to query database version: %w", err)+ }++ // Parse version from string like "PostgreSQL 14.2 on x86_64-pc-linux-gnu..."+ re := regexp.MustCompile(`PostgreSQL (\d+)\.`)+ matches := re.FindStringSubmatch(versionStr)+ if len(matches) < 2 {+ return fmt.Errorf("could not parse version from: %s", versionStr)+ }++ actualVersion := tools.GetPostgresqlVersionEnum(matches[1])+ if actualVersion != expectedVersion {+ return fmt.Errorf(+ "you specified wrong version. Real version is %s, but you specified %s",+ actualVersion,+ expectedVersion,+ )+ }++ return nil+}+// testBasicOperations tests basic operations that backup tools needfunc testBasicOperations(ctx context.Context, conn *pgx.Conn, dbName string) error {var hasCreatePriv booldiff --git a/backend/internal/features/notifiers/models/discord/model.go b/backend/internal/features/notifiers/models/discord/model.goindex 13177754..6ebecb96 100644--- a/backend/internal/features/notifiers/models/discord/model.go+++ b/backend/internal/features/notifiers/models/discord/model.go@@ -13,7 +13,7 @@ import ()type DiscordNotifier struct {- NotifierID uuid.UUID `json:"notifierId" gorm:"primaryKey;column:notifier_id"`+ NotifierID uuid.UUID `json:"notifierId" gorm:"primaryKey;column:notifier_id"`ChannelWebhookURL string `json:"channelWebhookUrl" gorm:"not null;column:channel_webhook_url"`}diff --git a/backend/internal/features/restores/di.go b/backend/internal/features/restores/di.goindex d107fe8f..157fb16d 100644--- a/backend/internal/features/restores/di.go+++ b/backend/internal/features/restores/di.go@@ -3,6 +3,7 @@ package restoresimport ("postgresus-backend/internal/features/backups/backups"backups_config "postgresus-backend/internal/features/backups/config"+ "postgresus-backend/internal/features/databases""postgresus-backend/internal/features/restores/usecases""postgresus-backend/internal/features/storages""postgresus-backend/internal/features/users"@@ -16,6 +17,7 @@ var restoreService = &RestoreService{storages.GetStorageService(),backups_config.GetBackupConfigService(),usecases.GetRestoreBackupUsecase(),+ databases.GetDatabaseService(),logger.GetLogger(),}var restoreController = &RestoreController{diff --git a/backend/internal/features/restores/service.go b/backend/internal/features/restores/service.goindex cb08b6fa..5327be9e 100644--- a/backend/internal/features/restores/service.go+++ b/backend/internal/features/restores/service.go@@ -2,6 +2,7 @@ package restoresimport ("errors"+ "fmt""log/slog""postgresus-backend/internal/features/backups/backups"backups_config "postgresus-backend/internal/features/backups/config"@@ -11,6 +12,7 @@ import ("postgresus-backend/internal/features/restores/usecases""postgresus-backend/internal/features/storages"users_models "postgresus-backend/internal/features/users/models"+ "postgresus-backend/internal/util/tools""time""github.com/google/uuid"@@ -22,6 +24,7 @@ type RestoreService struct {storageService *storages.StorageServicebackupConfigService *backups_config.BackupConfigServicerestoreBackupUsecase *usecases.RestoreBackupUsecase+ databaseService *databases.DatabaseServicelogger *slog.Logger}@@ -76,6 +79,26 @@ func (s *RestoreService) RestoreBackupWithAuth(return errors.New("user does not have access to this backup")}+ backupDatabase, err := s.databaseService.GetDatabase(user, backup.DatabaseID)+ if err != nil {+ return err+ }++ fmt.Printf(+ "restore from %s to %s\n",+ backupDatabase.Postgresql.Version,+ requestDTO.PostgresqlDatabase.Version,+ )++ if tools.IsBackupDbVersionHigherThanRestoreDbVersion(+ backupDatabase.Postgresql.Version,+ requestDTO.PostgresqlDatabase.Version,+ ) {+ return errors.New(`backup database version is higher than restore database version. ` ++ `Should be restored to the same version as the backup database or higher. ` ++ `For example, you can restore PG 15 backup to PG 15, 16 or higher. But cannot restore to 14 and lower`)+ }+go func() {if err := s.RestoreBackup(backup, requestDTO); err != nil {s.logger.Error("Failed to restore backup", "error", err)diff --git a/backend/internal/util/tools/enums.go b/backend/internal/util/tools/enums.goindex 27e3ee53..f3d44eb0 100644--- a/backend/internal/util/tools/enums.go+++ b/backend/internal/util/tools/enums.go@@ -1,6 +1,9 @@package tools-import "fmt"+import (+ "fmt"+ "strconv"+)type PostgresqlVersion string@@ -35,3 +38,19 @@ func GetPostgresqlVersionEnum(version string) PostgresqlVersion {panic(fmt.Sprintf("invalid postgresql version: %s", version))}}++func IsBackupDbVersionHigherThanRestoreDbVersion(+ backupDbVersion, restoreDbVersion PostgresqlVersion,+) bool {+ backupDbVersionInt, err := strconv.Atoi(string(backupDbVersion))+ if err != nil {+ return false+ }++ restoreDbVersionInt, err := strconv.Atoi(string(restoreDbVersion))+ if err != nil {+ return false+ }++ return backupDbVersionInt > restoreDbVersionInt+}diff --git a/frontend/src/features/databases/ui/edit/EditDatabaseSpecificDataComponent.tsx b/frontend/src/features/databases/ui/edit/EditDatabaseSpecificDataComponent.tsxindex 3bbc42fc..40fde254 100644--- a/frontend/src/features/databases/ui/edit/EditDatabaseSpecificDataComponent.tsx+++ b/frontend/src/features/databases/ui/edit/EditDatabaseSpecificDataComponent.tsx@@ -26,7 +26,6 @@ interface Props {isShowDbVersionHint?: boolean;isShowDbName?: boolean;- isBlockDbName?: boolean;}export const EditDatabaseSpecificDataComponent = ({@@ -44,8 +43,6 @@ export const EditDatabaseSpecificDataComponent = ({isShowDbVersionHint = true,isShowDbName = true,-- isBlockDbName = false,}: Props) => {const [editingDatabase, setEditingDatabase] = useState<Database>();const [isSaving, setIsSaving] = useState(false);@@ -266,7 +263,6 @@ export const EditDatabaseSpecificDataComponent = ({size="small"className="max-w-[200px] grow"placeholder="Enter PG database name (optional)"- disabled={isBlockDbName}/></div>)}diff --git a/frontend/src/features/restores/ui/RestoresComponent.tsx b/frontend/src/features/restores/ui/RestoresComponent.tsxindex 73ae4e2c..7cc75c79 100644--- a/frontend/src/features/restores/ui/RestoresComponent.tsx+++ b/frontend/src/features/restores/ui/RestoresComponent.tsx@@ -88,8 +88,9 @@ export const RestoresComponent = ({ database, backup }: Props) => {return (<><div className="my-5 text-sm">- Enter info of the database we will restore backup to. During the restore,{' '}- <u>all the current data will be cleared</u>+ Enter info of the database we will restore backup to.{' '}+ <u>The empty database for restore should be created before the restore</u>. During the+ restore, all the current data will be cleared<br /><br />Make sure the database is not used right now (most likely you do not want to restore the@@ -108,7 +109,6 @@ export const RestoresComponent = ({ database, backup }: Props) => {restore(database);}}isShowDbVersionHint={false}- isBlockDbName/></>);@@ -203,20 +203,22 @@ export const RestoresComponent = ({ database, backup }: Props) => {</div></div>- <div className="flex">- <div className="w-[75px] min-w-[75px]">Duration</div>- <div>- <div>{duration}</div>- <div className="mt-2 text-xs text-gray-500">- Expected restoration time usually 3x-5x longer than the backup duration- (sometimes less, sometimes more depending on data type)- <br />- <br />- So it is expected to take up to {expectedRestoreDuration} (usually- significantly faster)+ {restore.status === RestoreStatus.IN_PROGRESS && (+ <div className="flex">+ <div className="w-[75px] min-w-[75px]">Duration</div>+ <div>+ <div>{duration}</div>+ <div className="mt-2 text-xs text-gray-500">+ Expected restoration time usually 3x-5x longer than the backup duration+ (sometimes less, sometimes more depending on data type)+ <br />+ <br />+ So it is expected to take up to {expectedRestoreDuration} (usually+ significantly faster)+ </div></div></div>- </div>+ )}</div>);})}