Skip to content

Commit aa6b495

Browse files
FEATURE (backups): Move backups to separate backup config and make feature optional
1 parent 7c9faf7 commit aa6b495

File tree

76 files changed

+2248
-1249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2248
-1249
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="assets/logo.svg" alt="Postgresus Logo" width="250"/>
33

44
<h3>PostgreSQL monitoring and backup</h3>
5-
<p>Free, open source and self-hosted solution for automated PostgreSQL monitoring and backups with multiple storage options and notifications</p>
5+
<p>Free, open source and self-hosted solution for automated PostgreSQL monitoring and backups. With multiple storage options and notifications</p>
66

77
<p>
88
<a href="#-features">Features</a> •

assets/healthchecks.svg

Lines changed: 197 additions & 120 deletions
Loading

backend/.env.development.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ GOOSE_MIGRATION_DIR=./migrations
1515
# to get Google Drive env variables: add storage in UI and copy data from added storage here
1616
TEST_GOOGLE_DRIVE_CLIENT_ID=
1717
TEST_GOOGLE_DRIVE_CLIENT_SECRET=
18-
TEST_GOOGLE_DRIVE_TOKEN_JSON=
18+
TEST_GOOGLE_DRIVE_TOKEN_JSON="{\"access_token\":\"ya29..."
1919
# testing DBs
2020
TEST_POSTGRES_13_PORT=5001
2121
TEST_POSTGRES_14_PORT=5002

backend/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ swagger/docs.go
1010
swagger/swagger.json
1111
swagger/swagger.yaml
1212
postgresus-backend.exe
13-
ui/build/*
13+
ui/build/*
14+
pgdata-for-restore/

backend/cmd/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414

1515
"postgresus-backend/internal/config"
1616
"postgresus-backend/internal/downdetect"
17-
"postgresus-backend/internal/features/backups"
17+
"postgresus-backend/internal/features/backups/backups"
18+
backups_config "postgresus-backend/internal/features/backups/config"
1819
"postgresus-backend/internal/features/databases"
1920
"postgresus-backend/internal/features/disk"
2021
healthcheck_attempt "postgresus-backend/internal/features/healthcheck/attempt"
@@ -135,6 +136,7 @@ func setUpRoutes(r *gin.Engine) {
135136
healthcheckConfigController := healthcheck_config.GetHealthcheckConfigController()
136137
healthcheckAttemptController := healthcheck_attempt.GetHealthcheckAttemptController()
137138
diskController := disk.GetDiskController()
139+
backupConfigController := backups_config.GetBackupConfigController()
138140

139141
downdetectContoller.RegisterRoutes(v1)
140142
userController.RegisterRoutes(v1)
@@ -147,10 +149,13 @@ func setUpRoutes(r *gin.Engine) {
147149
diskController.RegisterRoutes(v1)
148150
healthcheckConfigController.RegisterRoutes(v1)
149151
healthcheckAttemptController.RegisterRoutes(v1)
152+
backupConfigController.RegisterRoutes(v1)
150153
}
151154

152155
func setUpDependencies() {
153156
backups.SetupDependencies()
157+
backups.SetupDependencies()
158+
restores.SetupDependencies()
154159
healthcheck_config.SetupDependencies()
155160
}
156161

backend/internal/features/backups/background_service.go renamed to backend/internal/features/backups/backups/background_service.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ package backups
33
import (
44
"log/slog"
55
"postgresus-backend/internal/config"
6+
backups_config "postgresus-backend/internal/features/backups/config"
67
"postgresus-backend/internal/features/databases"
78
"postgresus-backend/internal/features/storages"
9+
"postgresus-backend/internal/util/period"
810
"time"
911
)
1012

1113
type BackupBackgroundService struct {
12-
backupService *BackupService
13-
backupRepository *BackupRepository
14-
databaseService *databases.DatabaseService
15-
storageService *storages.StorageService
14+
backupService *BackupService
15+
backupRepository *BackupRepository
16+
databaseService *databases.DatabaseService
17+
storageService *storages.StorageService
18+
backupConfigService *backups_config.BackupConfigService
1619

1720
lastBackupTime time.Time
1821
logger *slog.Logger
@@ -60,15 +63,21 @@ func (s *BackupBackgroundService) failBackupsInProgress() error {
6063
}
6164

6265
for _, backup := range backupsInProgress {
66+
backupConfig, err := s.backupConfigService.GetBackupConfigByDbId(backup.DatabaseID)
67+
if err != nil {
68+
s.logger.Error("Failed to get backup config by database ID", "error", err)
69+
continue
70+
}
71+
6372
failMessage := "Backup failed due to application restart"
6473
backup.FailMessage = &failMessage
6574
backup.Status = BackupStatusFailed
6675
backup.BackupSizeMb = 0
6776

6877
s.backupService.SendBackupNotification(
69-
backup.Database,
78+
backupConfig,
7079
backup,
71-
databases.NotificationBackupFailed,
80+
backups_config.NotificationBackupFailed,
7281
&failMessage,
7382
)
7483

@@ -87,9 +96,15 @@ func (s *BackupBackgroundService) cleanOldBackups() error {
8796
}
8897

8998
for _, database := range allDatabases {
90-
backupStorePeriod := database.StorePeriod
99+
backupConfig, err := s.backupConfigService.GetBackupConfigByDbId(database.ID)
100+
if err != nil {
101+
s.logger.Error("Failed to get backup config by database ID", "error", err)
102+
continue
103+
}
104+
105+
backupStorePeriod := backupConfig.StorePeriod
91106

92-
if backupStorePeriod == databases.PeriodForever {
107+
if backupStorePeriod == period.PeriodForever {
93108
continue
94109
}
95110

@@ -148,7 +163,13 @@ func (s *BackupBackgroundService) runPendingBackups() error {
148163
}
149164

150165
for _, database := range allDatabases {
151-
if database.BackupInterval == nil {
166+
backupConfig, err := s.backupConfigService.GetBackupConfigByDbId(database.ID)
167+
if err != nil {
168+
s.logger.Error("Failed to get backup config by database ID", "error", err)
169+
continue
170+
}
171+
172+
if backupConfig.BackupInterval == nil {
152173
continue
153174
}
154175

@@ -169,13 +190,13 @@ func (s *BackupBackgroundService) runPendingBackups() error {
169190
lastBackupTime = &lastBackup.CreatedAt
170191
}
171192

172-
if database.BackupInterval.ShouldTriggerBackup(time.Now().UTC(), lastBackupTime) {
193+
if backupConfig.BackupInterval.ShouldTriggerBackup(time.Now().UTC(), lastBackupTime) {
173194
s.logger.Info(
174195
"Triggering scheduled backup",
175196
"databaseId",
176197
database.ID,
177198
"intervalType",
178-
database.BackupInterval.Interval,
199+
backupConfig.BackupInterval.Interval,
179200
)
180201

181202
go s.backupService.MakeBackup(database.ID)
File renamed without changes.

backend/internal/features/backups/di.go renamed to backend/internal/features/backups/backups/di.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package backups
22

33
import (
4-
"postgresus-backend/internal/features/backups/usecases"
4+
"postgresus-backend/internal/features/backups/backups/usecases"
5+
backups_config "postgresus-backend/internal/features/backups/config"
56
"postgresus-backend/internal/features/databases"
67
"postgresus-backend/internal/features/notifiers"
78
"postgresus-backend/internal/features/storages"
@@ -17,15 +18,18 @@ var backupService = &BackupService{
1718
backupRepository,
1819
notifiers.GetNotifierService(),
1920
notifiers.GetNotifierService(),
21+
backups_config.GetBackupConfigService(),
2022
usecases.GetCreateBackupUsecase(),
2123
logger.GetLogger(),
24+
[]BackupRemoveListener{},
2225
}
2326

2427
var backupBackgroundService = &BackupBackgroundService{
2528
backupService,
2629
backupRepository,
2730
databases.GetDatabaseService(),
2831
storages.GetStorageService(),
32+
backups_config.GetBackupConfigService(),
2933
time.Now().UTC(),
3034
logger.GetLogger(),
3135
}
@@ -36,9 +40,11 @@ var backupController = &BackupController{
3640
}
3741

3842
func SetupDependencies() {
39-
databases.
40-
GetDatabaseService().
43+
backups_config.
44+
GetBackupConfigService().
4145
SetDatabaseStorageChangeListener(backupService)
46+
47+
databases.GetDatabaseService().AddDbRemoveListener(backupService)
4248
}
4349

4450
func GetBackupService() *BackupService {

backend/internal/features/backups/enums.go renamed to backend/internal/features/backups/backups/enums.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ const (
66
BackupStatusInProgress BackupStatus = "IN_PROGRESS"
77
BackupStatusCompleted BackupStatus = "COMPLETED"
88
BackupStatusFailed BackupStatus = "FAILED"
9-
BackupStatusDeleted BackupStatus = "DELETED"
109
)

backend/internal/features/backups/interfaces.go renamed to backend/internal/features/backups/backups/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package backups
22

33
import (
4+
backups_config "postgresus-backend/internal/features/backups/config"
45
"postgresus-backend/internal/features/databases"
56
"postgresus-backend/internal/features/notifiers"
67
"postgresus-backend/internal/features/storages"
@@ -19,10 +20,15 @@ type NotificationSender interface {
1920
type CreateBackupUsecase interface {
2021
Execute(
2122
backupID uuid.UUID,
23+
backupConfig *backups_config.BackupConfig,
2224
database *databases.Database,
2325
storage *storages.Storage,
2426
backupProgressListener func(
2527
completedMBs float64,
2628
),
2729
) error
2830
}
31+
32+
type BackupRemoveListener interface {
33+
OnBeforeBackupRemove(backup *Backup) error
34+
}

0 commit comments

Comments
 (0)