feat: add multi backup

This commit is contained in:
Jonas Kaninda
2024-10-09 08:32:51 +02:00
parent 0c3a9b323b
commit 1df1c46a96
5 changed files with 144 additions and 7 deletions

1
go.mod
View File

@@ -10,6 +10,7 @@ require (
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.28.0 golang.org/x/crypto v0.28.0
gopkg.in/yaml.v3 v3.0.1
) )
require ( require (

View File

@@ -20,17 +20,22 @@ import (
func StartBackup(cmd *cobra.Command) { func StartBackup(cmd *cobra.Command) {
intro() intro()
dbConf = initDbConfig(cmd)
//Initialize backup configs //Initialize backup configs
config := initBackupConfig(cmd) config := initBackupConfig(cmd)
//Load backup configuration file
if config.cronExpression == "" { configFile, err := loadConfigFile()
BackupTask(dbConf, config) if err == nil {
startMultiBackup(config, configFile)
} else { } else {
if utils.IsValidCronExpression(config.cronExpression) { dbConf = initDbConfig(cmd)
scheduledMode(dbConf, config) if config.cronExpression == "" {
BackupTask(dbConf, config)
} else { } else {
utils.Fatal("Cron expression is not valid: %s", config.cronExpression) if utils.IsValidCronExpression(config.cronExpression) {
scheduledMode(dbConf, config)
} else {
utils.Fatal("Cron expression is not valid: %s", config.cronExpression)
}
} }
} }
@@ -63,6 +68,15 @@ func scheduledMode(db *dbConfig, config *BackupConfig) {
defer c.Stop() defer c.Stop()
select {} select {}
} }
func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
for _, db := range databases {
//Check if path is defined in config file
if db.Path != "" {
bkConfig.remotePath = db.Path
}
BackupTask(getDatabase(db), bkConfig)
}
}
func BackupTask(db *dbConfig, config *BackupConfig) { func BackupTask(db *dbConfig, config *BackupConfig) {
utils.Info("Starting backup task...") utils.Info("Starting backup task...")
//Generate file name //Generate file name
@@ -85,6 +99,55 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
localBackup(db, config) localBackup(db, config)
} }
} }
func startMultiBackup(bkConfig *BackupConfig, configFile string) {
utils.Info("Starting multiple backup jobs...")
var conf = &Config{}
conf, err := readConf(configFile)
if err != nil {
utils.Fatal("Error reading config file: %s", err)
}
//Check if cronExpression is defined in config file
if conf.CronExpression != "" {
bkConfig.cronExpression = conf.CronExpression
}
// Check if cronExpression is defined
if bkConfig.cronExpression == "" {
multiBackupTask(conf.Databases, bkConfig)
} else {
// Check if cronExpression is valid
if utils.IsValidCronExpression(bkConfig.cronExpression) {
utils.Info("Running MultiBackup in Scheduled mode")
utils.Info("Backup cron expression: %s", bkConfig.cronExpression)
utils.Info("Storage type %s ", bkConfig.storage)
//Test backup
utils.Info("Testing backup configurations...")
multiBackupTask(conf.Databases, bkConfig)
utils.Info("Testing backup configurations...done")
utils.Info("Creating multi backup job...")
// Create a new cron instance
c := cron.New()
_, err := c.AddFunc(bkConfig.cronExpression, func() {
// Create a channel
multiBackupTask(conf.Databases, bkConfig)
})
if err != nil {
return
}
// Start the cron scheduler
c.Start()
utils.Info("Creating multi backup job...done")
utils.Info("Backup job started")
defer c.Stop()
select {}
} else {
utils.Fatal("Cron expression is not valid: %s", bkConfig.cronExpression)
}
}
}
func intro() { func intro() {
utils.Info("Starting PostgreSQL Backup...") utils.Info("Starting PostgreSQL Backup...")
utils.Info("Copyright (c) 2024 Jonas Kaninda ") utils.Info("Copyright (c) 2024 Jonas Kaninda ")

View File

@@ -7,6 +7,7 @@
package pkg package pkg
import ( import (
"errors"
"fmt" "fmt"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -14,7 +15,17 @@ import (
"strconv" "strconv"
) )
type Database struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Name string `yaml:"name"`
User string `yaml:"user"`
Password string `yaml:"password"`
Path string `yaml:"path"`
}
type Config struct { type Config struct {
Databases []Database `yaml:"databases"`
CronExpression string `yaml:"cronExpression"`
} }
type dbConfig struct { type dbConfig struct {
@@ -92,6 +103,16 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
return &dConf return &dConf
} }
func getDatabase(database Database) *dbConfig {
return &dbConfig{
dbHost: database.Host,
dbPort: database.Port,
dbName: database.Name,
dbUserName: database.User,
dbPassword: database.Password,
}
}
// loadSSHConfig loads the SSH configuration from environment variables // loadSSHConfig loads the SSH configuration from environment variables
func loadSSHConfig() (*SSHConfig, error) { func loadSSHConfig() (*SSHConfig, error) {
utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME") utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME")
@@ -245,3 +266,10 @@ func initTargetDbConfig() *targetDbConfig {
} }
return &tdbConfig return &tdbConfig
} }
func loadConfigFile() (string, error) {
backupConfigFile, err := checkConfigFile(os.Getenv("BACKUP_CONFIG_FILE"))
if err == nil {
return backupConfigFile, nil
}
return "", errors.New("backup config file not found")
}

View File

@@ -10,6 +10,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"gopkg.in/yaml.v3"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -139,6 +140,8 @@ func testDatabaseConnection(db *dbConfig) {
utils.Info("Successfully connected to %s database", db.dbName) utils.Info("Successfully connected to %s database", db.dbName)
} }
// checkPubKeyFile checks gpg public key
func checkPubKeyFile(pubKey string) (string, error) { func checkPubKeyFile(pubKey string) (string, error) {
// Define possible key file names // Define possible key file names
keyFiles := []string{filepath.Join(gpgHome, "public_key.asc"), filepath.Join(gpgHome, "public_key.gpg"), pubKey} keyFiles := []string{filepath.Join(gpgHome, "public_key.asc"), filepath.Join(gpgHome, "public_key.gpg"), pubKey}
@@ -160,6 +163,8 @@ func checkPubKeyFile(pubKey string) (string, error) {
// Return an error if neither file exists // Return an error if neither file exists
return "", fmt.Errorf("no public key file found") return "", fmt.Errorf("no public key file found")
} }
// checkPrKeyFile checks private key
func checkPrKeyFile(prKey string) (string, error) { func checkPrKeyFile(prKey string) (string, error) {
// Define possible key file names // Define possible key file names
keyFiles := []string{filepath.Join(gpgHome, "private_key.asc"), filepath.Join(gpgHome, "private_key.gpg"), prKey} keyFiles := []string{filepath.Join(gpgHome, "private_key.asc"), filepath.Join(gpgHome, "private_key.gpg"), prKey}
@@ -181,3 +186,42 @@ func checkPrKeyFile(prKey string) (string, error) {
// Return an error if neither file exists // Return an error if neither file exists
return "", fmt.Errorf("no public key file found") return "", fmt.Errorf("no public key file found")
} }
func readConf(configFile string) (*Config, error) {
//configFile := filepath.Join("./", filename)
if utils.FileExists(configFile) {
buf, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
c := &Config{}
err = yaml.Unmarshal(buf, c)
if err != nil {
return nil, fmt.Errorf("in file %q: %w", configFile, err)
}
return c, err
}
return nil, fmt.Errorf("config file %q not found", configFile)
}
func checkConfigFile(filePath string) (string, error) {
// Define possible config file names
configFiles := []string{filepath.Join(workingDir, "config.yaml"), filepath.Join(workingDir, "config.yml"), filePath}
// Loop through config file names and check if they exist
for _, configFile := range configFiles {
if _, err := os.Stat(configFile); err == nil {
// File exists
return configFile, nil
} else if os.IsNotExist(err) {
// File does not exist, continue to the next one
continue
} else {
// An unexpected error occurred
return "", err
}
}
// Return an error if neither file exists
return "", fmt.Errorf("no config file found")
}

View File

@@ -16,6 +16,7 @@ var (
file = "" file = ""
storagePath = "/backup" storagePath = "/backup"
workingDir = "/config"
disableCompression = false disableCompression = false
encryption = false encryption = false
usingKey = false usingKey = false