Skip to content

Commit

Permalink
feat: add multi database backup
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaninda committed Oct 9, 2024
1 parent 1df1c46 commit ad9d7a0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions docs/how-tos/deprecated-configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Update deprecated configurations
layout: default
parent: How Tos
nav_order: 11
---
40 changes: 40 additions & 0 deletions docs/how-tos/mutli-backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Run multiple database backup schedules in the same container
layout: default
parent: How Tos
nav_order: 11
---

Multiple backup schedules with different configuration can be configured by mounting a configuration file into `/config/config.yaml` `/config/config.yml` or by defining an environment variable `BACKUP_CONFIG_FILE=/backup/config.yaml`.

## Configuration file

```yaml
#cronExpression: "@every 20m" //Optional, for scheduled backups
cronExpression: ""
databases:
- host: postgres1
port: 5432
name: database1
user: database1
password: password
path: /s3-path/database1 #For SSH or FTP you need to define the full path (/home/toto/backup/)
- host: postgres2
port: 5432
name: lldap
user: lldap
password: password
path: /s3-path/lldap #For SSH or FTP you need to define the full path (/home/toto/backup/)
- host: postgres3
port: 5432
name: keycloak
user: keycloak
password: password
path: /s3-path/keycloak #For SSH or FTP you need to define the full path (/home/toto/backup/)
- host: postgres4
port: 5432
name: joplin
user: joplin
password: password
path: /s3-path/joplin #For SSH or FTP you need to define the full path (/home/toto/backup/)
```
26 changes: 15 additions & 11 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func StartBackup(cmd *cobra.Command) {
config := initBackupConfig(cmd)
//Load backup configuration file
configFile, err := loadConfigFile()
if err == nil {
startMultiBackup(config, configFile)
} else {
if err != nil {
dbConf = initDbConfig(cmd)
if config.cronExpression == "" {
BackupTask(dbConf, config)
Expand All @@ -37,11 +35,13 @@ func StartBackup(cmd *cobra.Command) {
utils.Fatal("Cron expression is not valid: %s", config.cronExpression)
}
}
} else {
startMultiBackup(config, configFile)
}

}

// Run in scheduled mode
// scheduledMode Runs backup in scheduled mode
func scheduledMode(db *dbConfig, config *BackupConfig) {
utils.Info("Running in Scheduled mode")
utils.Info("Backup cron expression: %s", config.cronExpression)
Expand All @@ -68,6 +68,8 @@ func scheduledMode(db *dbConfig, config *BackupConfig) {
defer c.Stop()
select {}
}

// multiBackupTask backup multi database
func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
for _, db := range databases {
//Check if path is defined in config file
Expand Down Expand Up @@ -100,7 +102,7 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
}
}
func startMultiBackup(bkConfig *BackupConfig, configFile string) {
utils.Info("Starting multiple backup jobs...")
utils.Info("Starting multiple backup job...")
var conf = &Config{}
conf, err := readConf(configFile)
if err != nil {
Expand Down Expand Up @@ -148,10 +150,6 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
}

}
func intro() {
utils.Info("Starting PostgreSQL Backup...")
utils.Info("Copyright (c) 2024 Jonas Kaninda ")
}

// BackupDatabase backup database
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {
Expand Down Expand Up @@ -182,7 +180,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
log.Fatal(err)
}
// save output
file, err := os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
file, err := os.Create(filepath.Join(tmpPath, backupFileName))
if err != nil {
log.Fatal(err)
}
Expand All @@ -208,7 +206,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
gzipCmd := exec.Command("gzip")
gzipCmd.Stdin = stdout
// save output
gzipCmd.Stdout, err = os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
gzipCmd.Stdout, err = os.Create(filepath.Join(tmpPath, backupFileName))
gzipCmd.Start()
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -243,6 +241,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
}
//Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
}

func s3Backup(db *dbConfig, config *BackupConfig) {
Expand Down Expand Up @@ -283,6 +282,8 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
utils.NotifySuccess(finalFileName)
//Delete temp
deleteTemp()
utils.Info("Backup completed successfully")

}
func sshBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to Remote server")
Expand Down Expand Up @@ -318,6 +319,8 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
utils.NotifySuccess(finalFileName)
//Delete temp
deleteTemp()
utils.Info("Backup completed successfully")

}
func ftpBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to the remote FTP server")
Expand Down Expand Up @@ -353,6 +356,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
utils.NotifySuccess(finalFileName)
//Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
}

func encryptBackup(config *BackupConfig) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package pkg

import (
"errors"
"fmt"
"github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -271,5 +270,5 @@ func loadConfigFile() (string, error) {
if err == nil {
return backupConfigFile, nil
}
return "", errors.New("backup config file not found")
return "", fmt.Errorf("backup config file not found")
}
10 changes: 9 additions & 1 deletion pkg/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"time"
)

func intro() {
utils.Info("Starting PostgreSQL Backup...")
utils.Info("Copyright (c) 2024 Jonas Kaninda ")
}

// copyToTmp copy file to temporary directory
func copyToTmp(sourcePath string, backupFileName string) {
//Copy backup from storage to /tmp
Expand Down Expand Up @@ -186,8 +191,9 @@ func checkPrKeyFile(prKey string) (string, error) {
// Return an error if neither file exists
return "", fmt.Errorf("no public key file found")
}

// readConf reads config file and returns Config
func readConf(configFile string) (*Config, error) {
//configFile := filepath.Join("./", filename)
if utils.FileExists(configFile) {
buf, err := os.ReadFile(configFile)
if err != nil {
Expand All @@ -204,6 +210,8 @@ func readConf(configFile string) (*Config, error) {
}
return nil, fmt.Errorf("config file %q not found", configFile)
}

// checkConfigFile checks config files and returns one config file
func checkConfigFile(filePath string) (string, error) {
// Define possible config file names
configFiles := []string{filepath.Join(workingDir, "config.yaml"), filepath.Join(workingDir, "config.yml"), filePath}
Expand Down
1 change: 1 addition & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"
)

// FileExists checks if the file does exist
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
Expand Down

0 comments on commit ad9d7a0

Please sign in to comment.