Merge pull request #84 from jkaninda/refactor
refactor: replace function params by config struct
This commit is contained in:
@@ -74,16 +74,16 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
|
|||||||
}
|
}
|
||||||
config.backupFileName = backupFileName
|
config.backupFileName = backupFileName
|
||||||
switch config.storage {
|
switch config.storage {
|
||||||
case "s3":
|
|
||||||
s3Backup(db, config.backupFileName, config.disableCompression, config.prune, config.backupRetention, config.encryption)
|
|
||||||
case "local":
|
case "local":
|
||||||
localBackup(db, config.backupFileName, config.disableCompression, config.prune, config.backupRetention, config.encryption)
|
localBackup(db, config)
|
||||||
|
case "s3":
|
||||||
|
s3Backup(db, config)
|
||||||
case "ssh", "remote":
|
case "ssh", "remote":
|
||||||
sshBackup(db, config.backupFileName, config.remotePath, config.disableCompression, config.prune, config.backupRetention, config.encryption)
|
sshBackup(db, config)
|
||||||
case "ftp":
|
case "ftp":
|
||||||
utils.Fatal("Not supported storage type: %s", config.storage)
|
utils.Fatal("Not supported storage type: %s", config.storage)
|
||||||
default:
|
default:
|
||||||
localBackup(db, config.backupFileName, config.disableCompression, config.prune, config.backupRetention, config.encryption)
|
localBackup(db, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func intro() {
|
func intro() {
|
||||||
@@ -162,36 +162,36 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
|||||||
utils.Info("Database has been backed up")
|
utils.Info("Database has been backed up")
|
||||||
|
|
||||||
}
|
}
|
||||||
func localBackup(db *dbConfig, backupFileName string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
|
func localBackup(db *dbConfig, config *BackupConfig) {
|
||||||
utils.Info("Backup database to local storage")
|
utils.Info("Backup database to local storage")
|
||||||
BackupDatabase(db, backupFileName, disableCompression)
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||||
finalFileName := backupFileName
|
finalFileName := config.backupFileName
|
||||||
if encrypt {
|
if config.encryption {
|
||||||
encryptBackup(backupFileName)
|
encryptBackup(config.backupFileName, config.passphrase)
|
||||||
finalFileName = fmt.Sprintf("%s.%s", backupFileName, gpgExtension)
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, gpgExtension)
|
||||||
}
|
}
|
||||||
utils.Info("Backup name is %s", finalFileName)
|
utils.Info("Backup name is %s", finalFileName)
|
||||||
moveToBackup(finalFileName, storagePath)
|
moveToBackup(finalFileName, storagePath)
|
||||||
//Send notification
|
//Send notification
|
||||||
utils.NotifySuccess(finalFileName)
|
utils.NotifySuccess(finalFileName)
|
||||||
//Delete old backup
|
//Delete old backup
|
||||||
if prune {
|
if config.prune {
|
||||||
deleteOldBackup(backupRetention)
|
deleteOldBackup(config.backupRetention)
|
||||||
}
|
}
|
||||||
//Delete temp
|
//Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func s3Backup(db *dbConfig, backupFileName string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
|
func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||||
bucket := utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
|
bucket := utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
|
||||||
s3Path := utils.GetEnvVariable("AWS_S3_PATH", "S3_PATH")
|
s3Path := utils.GetEnvVariable("AWS_S3_PATH", "S3_PATH")
|
||||||
utils.Info("Backup database to s3 storage")
|
utils.Info("Backup database to s3 storage")
|
||||||
//Backup database
|
//Backup database
|
||||||
BackupDatabase(db, backupFileName, disableCompression)
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||||
finalFileName := backupFileName
|
finalFileName := config.backupFileName
|
||||||
if encrypt {
|
if config.encryption {
|
||||||
encryptBackup(backupFileName)
|
encryptBackup(config.backupFileName, config.passphrase)
|
||||||
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
|
||||||
}
|
}
|
||||||
utils.Info("Uploading backup archive to remote storage S3 ... ")
|
utils.Info("Uploading backup archive to remote storage S3 ... ")
|
||||||
|
|
||||||
@@ -203,14 +203,14 @@ func s3Backup(db *dbConfig, backupFileName string, disableCompression bool, prun
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Delete backup file from tmp folder
|
//Delete backup file from tmp folder
|
||||||
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
err = utils.DeleteFile(filepath.Join(tmpPath, config.backupFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error deleting file: ", err)
|
fmt.Println("Error deleting file: ", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
// Delete old backup
|
// Delete old backup
|
||||||
if prune {
|
if config.prune {
|
||||||
err := utils.DeleteOldBackup(bucket, s3Path, backupRetention)
|
err := utils.DeleteOldBackup(bucket, s3Path, config.backupRetention)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error deleting old backup from S3: %s ", err)
|
utils.Fatal("Error deleting old backup from S3: %s ", err)
|
||||||
}
|
}
|
||||||
@@ -221,18 +221,18 @@ func s3Backup(db *dbConfig, backupFileName string, disableCompression bool, prun
|
|||||||
//Delete temp
|
//Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
}
|
}
|
||||||
func sshBackup(db *dbConfig, backupFileName, remotePath string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
|
func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||||
utils.Info("Backup database to Remote server")
|
utils.Info("Backup database to Remote server")
|
||||||
//Backup database
|
//Backup database
|
||||||
BackupDatabase(db, backupFileName, disableCompression)
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||||
finalFileName := backupFileName
|
finalFileName := config.backupFileName
|
||||||
if encrypt {
|
if config.encryption {
|
||||||
encryptBackup(backupFileName)
|
encryptBackup(config.backupFileName, config.passphrase)
|
||||||
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
|
||||||
}
|
}
|
||||||
utils.Info("Uploading backup archive to remote storage ... ")
|
utils.Info("Uploading backup archive to remote storage ... ")
|
||||||
utils.Info("Backup name is %s", finalFileName)
|
utils.Info("Backup name is %s", finalFileName)
|
||||||
err := CopyToRemote(finalFileName, remotePath)
|
err := CopyToRemote(finalFileName, config.remotePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error uploading file to the remote server: %s ", err)
|
utils.Fatal("Error uploading file to the remote server: %s ", err)
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ func sshBackup(db *dbConfig, backupFileName, remotePath string, disableCompressi
|
|||||||
utils.Error("Error deleting file: %v", err)
|
utils.Error("Error deleting file: %v", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
if prune {
|
if config.prune {
|
||||||
//TODO: Delete old backup from remote server
|
//TODO: Delete old backup from remote server
|
||||||
utils.Info("Deleting old backup from a remote server is not implemented yet")
|
utils.Info("Deleting old backup from a remote server is not implemented yet")
|
||||||
|
|
||||||
@@ -257,9 +257,8 @@ func sshBackup(db *dbConfig, backupFileName, remotePath string, disableCompressi
|
|||||||
deleteTemp()
|
deleteTemp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func encryptBackup(backupFileName string) {
|
func encryptBackup(backupFileName, gpqPassphrase string) {
|
||||||
gpgPassphrase := os.Getenv("GPG_PASSPHRASE")
|
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpqPassphrase)
|
||||||
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpgPassphrase)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error during encrypting backup %v", err)
|
utils.Fatal("Error during encrypting backup %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ type BackupConfig struct {
|
|||||||
prune bool
|
prune bool
|
||||||
encryption bool
|
encryption bool
|
||||||
remotePath string
|
remotePath string
|
||||||
gpqPassphrase string
|
passphrase string
|
||||||
storage string
|
storage string
|
||||||
cronExpression string
|
cronExpression string
|
||||||
}
|
}
|
||||||
@@ -74,11 +74,11 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
|||||||
prune, _ := cmd.Flags().GetBool("prune")
|
prune, _ := cmd.Flags().GetBool("prune")
|
||||||
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
||||||
_, _ = cmd.Flags().GetString("mode")
|
_, _ = cmd.Flags().GetString("mode")
|
||||||
gpqPassphrase := os.Getenv("GPG_PASSPHRASE")
|
passphrase := os.Getenv("GPG_PASSPHRASE")
|
||||||
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
|
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
|
||||||
cronExpression := os.Getenv("BACKUP_CRON_EXPRESSION")
|
cronExpression := os.Getenv("BACKUP_CRON_EXPRESSION")
|
||||||
|
|
||||||
if gpqPassphrase != "" {
|
if passphrase != "" {
|
||||||
encryption = true
|
encryption = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
|||||||
config.storage = storage
|
config.storage = storage
|
||||||
config.encryption = encryption
|
config.encryption = encryption
|
||||||
config.remotePath = remotePath
|
config.remotePath = remotePath
|
||||||
config.gpqPassphrase = gpqPassphrase
|
config.passphrase = passphrase
|
||||||
config.cronExpression = cronExpression
|
config.cronExpression = cronExpression
|
||||||
return &config
|
return &config
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user