2024-11-04 09:00:14 +01:00
|
|
|
package internal
|
2024-01-19 06:32:30 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-10-13 14:20:43 +02:00
|
|
|
"github.com/jkaninda/encryptor"
|
2024-11-19 02:54:31 +01:00
|
|
|
"github.com/jkaninda/pg-bkup/pkg/logger"
|
2024-11-19 04:03:41 +01:00
|
|
|
"github.com/jkaninda/pg-bkup/pkg/storage/ftp"
|
|
|
|
|
"github.com/jkaninda/pg-bkup/pkg/storage/local"
|
|
|
|
|
"github.com/jkaninda/pg-bkup/pkg/storage/s3"
|
|
|
|
|
"github.com/jkaninda/pg-bkup/pkg/storage/ssh"
|
2024-01-19 06:32:30 +01:00
|
|
|
"github.com/jkaninda/pg-bkup/utils"
|
2024-09-28 09:20:35 +02:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-01-20 14:03:06 +01:00
|
|
|
"github.com/spf13/cobra"
|
2024-01-19 06:32:30 +01:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2024-02-17 18:20:35 +01:00
|
|
|
"path/filepath"
|
2024-01-19 06:32:30 +01:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-06 03:08:17 +01:00
|
|
|
/*
|
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2023 Jonas Kaninda
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-01-20 14:03:06 +01:00
|
|
|
func StartBackup(cmd *cobra.Command) {
|
2024-09-11 04:33:41 +02:00
|
|
|
intro()
|
2024-11-19 02:54:31 +01:00
|
|
|
// Initialize backup configs
|
2024-09-28 09:20:35 +02:00
|
|
|
config := initBackupConfig(cmd)
|
2024-11-19 02:54:31 +01:00
|
|
|
// Load backup configuration file
|
2024-10-09 08:32:51 +02:00
|
|
|
configFile, err := loadConfigFile()
|
2024-10-09 12:05:37 +02:00
|
|
|
if err != nil {
|
2024-10-09 08:32:51 +02:00
|
|
|
dbConf = initDbConfig(cmd)
|
|
|
|
|
if config.cronExpression == "" {
|
|
|
|
|
BackupTask(dbConf, config)
|
2024-09-28 09:20:35 +02:00
|
|
|
} else {
|
2024-10-09 08:32:51 +02:00
|
|
|
if utils.IsValidCronExpression(config.cronExpression) {
|
|
|
|
|
scheduledMode(dbConf, config)
|
|
|
|
|
} else {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Cron expression is not valid: %s", config.cronExpression)
|
2024-10-09 08:32:51 +02:00
|
|
|
}
|
2024-09-28 09:20:35 +02:00
|
|
|
}
|
2024-10-09 12:05:37 +02:00
|
|
|
} else {
|
|
|
|
|
startMultiBackup(config, configFile)
|
2024-01-20 14:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 12:05:37 +02:00
|
|
|
// scheduledMode Runs backup in scheduled mode
|
2024-09-28 09:20:35 +02:00
|
|
|
func scheduledMode(db *dbConfig, config *BackupConfig) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Running in Scheduled mode")
|
|
|
|
|
logger.Info("Backup cron expression: %s", config.cronExpression)
|
|
|
|
|
logger.Info("The next scheduled time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
|
|
|
|
|
logger.Info("Storage type %s ", config.storage)
|
2024-01-20 14:03:06 +01:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Test backup
|
|
|
|
|
logger.Info("Testing backup configurations...")
|
2024-11-19 03:18:03 +01:00
|
|
|
testDatabaseConnection(db)
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Testing backup configurations...done")
|
|
|
|
|
logger.Info("Creating backup job...")
|
2024-09-28 09:20:35 +02:00
|
|
|
// Create a new cron instance
|
|
|
|
|
c := cron.New()
|
2024-01-20 14:03:06 +01:00
|
|
|
|
2024-09-28 09:20:35 +02:00
|
|
|
_, err := c.AddFunc(config.cronExpression, func() {
|
|
|
|
|
BackupTask(db, config)
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Next backup time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
|
2024-10-20 06:01:30 +02:00
|
|
|
|
2024-09-28 09:20:35 +02:00
|
|
|
})
|
2024-07-28 16:59:26 +02:00
|
|
|
if err != nil {
|
2024-09-28 09:20:35 +02:00
|
|
|
return
|
2024-07-28 16:59:26 +02:00
|
|
|
}
|
2024-09-28 09:20:35 +02:00
|
|
|
// Start the cron scheduler
|
|
|
|
|
c.Start()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Creating backup job...done")
|
|
|
|
|
logger.Info("Backup job started")
|
2024-09-28 09:20:35 +02:00
|
|
|
defer c.Stop()
|
|
|
|
|
select {}
|
|
|
|
|
}
|
2024-10-09 12:05:37 +02:00
|
|
|
|
|
|
|
|
// multiBackupTask backup multi database
|
2024-10-09 08:32:51 +02:00
|
|
|
func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
|
|
|
|
|
for _, db := range databases {
|
2024-11-19 02:54:31 +01:00
|
|
|
// Check if path is defined in config file
|
2024-10-09 08:32:51 +02:00
|
|
|
if db.Path != "" {
|
|
|
|
|
bkConfig.remotePath = db.Path
|
|
|
|
|
}
|
|
|
|
|
BackupTask(getDatabase(db), bkConfig)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-20 06:01:30 +02:00
|
|
|
|
|
|
|
|
// BackupTask backups database
|
2024-09-28 09:20:35 +02:00
|
|
|
func BackupTask(db *dbConfig, config *BackupConfig) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Starting backup task...")
|
|
|
|
|
// Generate file name
|
2024-09-30 01:59:28 +02:00
|
|
|
backupFileName := fmt.Sprintf("%s_%s.sql.gz", db.dbName, time.Now().Format("20060102_150405"))
|
2024-09-28 09:20:35 +02:00
|
|
|
if config.disableCompression {
|
2024-09-30 01:59:28 +02:00
|
|
|
backupFileName = fmt.Sprintf("%s_%s.sql", db.dbName, time.Now().Format("20060102_150405"))
|
2024-07-28 16:59:26 +02:00
|
|
|
}
|
2024-09-28 09:20:35 +02:00
|
|
|
config.backupFileName = backupFileName
|
|
|
|
|
switch config.storage {
|
|
|
|
|
case "local":
|
2024-09-29 20:00:30 +02:00
|
|
|
localBackup(db, config)
|
2024-10-03 18:05:50 +02:00
|
|
|
case "s3", "S3":
|
2024-09-29 20:00:30 +02:00
|
|
|
s3Backup(db, config)
|
2024-10-03 18:05:50 +02:00
|
|
|
case "ssh", "SSH", "remote":
|
2024-09-29 20:00:30 +02:00
|
|
|
sshBackup(db, config)
|
2024-10-03 18:05:50 +02:00
|
|
|
case "ftp", "FTP":
|
2024-09-29 23:53:19 +02:00
|
|
|
ftpBackup(db, config)
|
2024-09-28 09:20:35 +02:00
|
|
|
default:
|
2024-09-29 20:00:30 +02:00
|
|
|
localBackup(db, config)
|
2024-01-20 14:03:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 08:32:51 +02:00
|
|
|
func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Starting backup task...")
|
2024-10-09 08:32:51 +02:00
|
|
|
conf, err := readConf(configFile)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error reading config file: %s", err)
|
2024-10-09 08:32:51 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
// Check if cronExpression is defined in config file
|
2024-10-09 08:32:51 +02:00
|
|
|
if conf.CronExpression != "" {
|
|
|
|
|
bkConfig.cronExpression = conf.CronExpression
|
|
|
|
|
}
|
2024-11-19 03:27:33 +01:00
|
|
|
if len(conf.Databases) == 0 {
|
|
|
|
|
logger.Fatal("No databases found")
|
|
|
|
|
}
|
2024-10-09 08:32:51 +02:00
|
|
|
// Check if cronExpression is defined
|
|
|
|
|
if bkConfig.cronExpression == "" {
|
|
|
|
|
multiBackupTask(conf.Databases, bkConfig)
|
|
|
|
|
} else {
|
|
|
|
|
// Check if cronExpression is valid
|
|
|
|
|
if utils.IsValidCronExpression(bkConfig.cronExpression) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Running backup in Scheduled mode")
|
|
|
|
|
logger.Info("Backup cron expression: %s", bkConfig.cronExpression)
|
|
|
|
|
logger.Info("The next scheduled time is: %v", utils.CronNextTime(bkConfig.cronExpression).Format(timeFormat))
|
|
|
|
|
logger.Info("Storage type %s ", bkConfig.storage)
|
2024-10-09 08:32:51 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Test backup
|
|
|
|
|
logger.Info("Testing backup configurations...")
|
2024-11-19 03:18:03 +01:00
|
|
|
for _, db := range conf.Databases {
|
|
|
|
|
testDatabaseConnection(getDatabase(db))
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Testing backup configurations...done")
|
|
|
|
|
logger.Info("Creating backup job...")
|
2024-10-09 08:32:51 +02:00
|
|
|
// Create a new cron instance
|
|
|
|
|
c := cron.New()
|
|
|
|
|
|
|
|
|
|
_, err := c.AddFunc(bkConfig.cronExpression, func() {
|
|
|
|
|
multiBackupTask(conf.Databases, bkConfig)
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Next backup time is: %v", utils.CronNextTime(bkConfig.cronExpression).Format(timeFormat))
|
2024-10-20 06:01:30 +02:00
|
|
|
|
2024-10-09 08:32:51 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Start the cron scheduler
|
|
|
|
|
c.Start()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Creating backup job...done")
|
|
|
|
|
logger.Info("Backup job started")
|
2024-10-09 08:32:51 +02:00
|
|
|
defer c.Stop()
|
|
|
|
|
select {}
|
|
|
|
|
|
|
|
|
|
} else {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Cron expression is not valid: %s", bkConfig.cronExpression)
|
2024-10-09 08:32:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-19 06:32:30 +01:00
|
|
|
|
2024-01-19 06:49:29 +01:00
|
|
|
// BackupDatabase backup database
|
2024-08-29 21:49:35 +02:00
|
|
|
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {
|
|
|
|
|
|
2024-01-19 06:32:30 +01:00
|
|
|
storagePath = os.Getenv("STORAGE_PATH")
|
|
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Starting database backup...")
|
2024-08-04 01:20:30 +02:00
|
|
|
|
2024-08-29 21:49:35 +02:00
|
|
|
err := os.Setenv("PGPASSWORD", db.dbPassword)
|
2024-08-04 01:20:30 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-29 21:49:35 +02:00
|
|
|
testDatabaseConnection(db)
|
2024-08-04 01:20:30 +02:00
|
|
|
// Backup Database database
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backing up database...")
|
2024-08-04 01:20:30 +02:00
|
|
|
|
|
|
|
|
// Verify is compression is disabled
|
|
|
|
|
if disableCompression {
|
|
|
|
|
// Execute pg_dump
|
|
|
|
|
cmd := exec.Command("pg_dump",
|
2024-08-29 21:49:35 +02:00
|
|
|
"-h", db.dbHost,
|
|
|
|
|
"-p", db.dbPort,
|
|
|
|
|
"-U", db.dbUserName,
|
|
|
|
|
"-d", db.dbName,
|
2024-08-04 01:20:30 +02:00
|
|
|
)
|
|
|
|
|
output, err := cmd.Output()
|
2024-01-19 06:32:30 +01:00
|
|
|
if err != nil {
|
2024-08-04 01:20:30 +02:00
|
|
|
log.Fatal(err)
|
2024-01-19 06:32:30 +01:00
|
|
|
}
|
2024-08-04 01:20:30 +02:00
|
|
|
// save output
|
2024-10-09 12:05:37 +02:00
|
|
|
file, err := os.Create(filepath.Join(tmpPath, backupFileName))
|
2024-08-04 01:20:30 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
defer func(file *os.File) {
|
|
|
|
|
err := file.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}(file)
|
2024-01-19 06:32:30 +01:00
|
|
|
|
2024-08-04 01:20:30 +02:00
|
|
|
_, err = file.Write(output)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-01-19 06:32:30 +01:00
|
|
|
|
2024-08-04 01:20:30 +02:00
|
|
|
} else {
|
|
|
|
|
// Execute pg_dump
|
|
|
|
|
cmd := exec.Command("pg_dump",
|
2024-08-29 21:49:35 +02:00
|
|
|
"-h", db.dbHost,
|
|
|
|
|
"-p", db.dbPort,
|
|
|
|
|
"-U", db.dbUserName,
|
|
|
|
|
"-d", db.dbName,
|
2024-08-04 01:20:30 +02:00
|
|
|
)
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
gzipCmd := exec.Command("gzip")
|
|
|
|
|
gzipCmd.Stdin = stdout
|
|
|
|
|
// save output
|
2024-10-09 12:05:37 +02:00
|
|
|
gzipCmd.Stdout, err = os.Create(filepath.Join(tmpPath, backupFileName))
|
2024-11-19 02:54:31 +01:00
|
|
|
err2 := gzipCmd.Start()
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-04 01:20:30 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := gzipCmd.Wait(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
2024-02-20 07:55:55 +01:00
|
|
|
}
|
2024-01-19 06:32:30 +01:00
|
|
|
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Database has been backed up")
|
2024-01-19 06:32:30 +01:00
|
|
|
|
|
|
|
|
}
|
2024-09-29 20:00:30 +02:00
|
|
|
func localBackup(db *dbConfig, config *BackupConfig) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup database to local storage")
|
2024-10-10 04:32:03 +02:00
|
|
|
startTime = time.Now().Format(utils.TimeFormat())
|
2024-09-29 20:00:30 +02:00
|
|
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
|
|
|
|
finalFileName := config.backupFileName
|
|
|
|
|
if config.encryption {
|
2024-10-08 11:04:46 +02:00
|
|
|
encryptBackup(config)
|
2024-09-29 20:00:30 +02:00
|
|
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, gpgExtension)
|
2024-07-29 07:33:26 +02:00
|
|
|
}
|
2024-10-09 22:39:44 +02:00
|
|
|
fileInfo, err := os.Stat(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error: %s", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
backupSize = fileInfo.Size()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup name is %s", finalFileName)
|
2024-10-22 16:48:42 +02:00
|
|
|
localStorage := local.NewStorage(local.Config{
|
|
|
|
|
LocalPath: tmpPath,
|
|
|
|
|
RemotePath: storagePath,
|
|
|
|
|
})
|
|
|
|
|
err = localStorage.Copy(finalFileName)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error copying backup file: %s", err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup saved in %s", filepath.Join(storagePath, finalFileName))
|
|
|
|
|
// Send notification
|
2024-10-09 22:39:44 +02:00
|
|
|
utils.NotifySuccess(&utils.NotificationData{
|
|
|
|
|
File: finalFileName,
|
|
|
|
|
BackupSize: backupSize,
|
|
|
|
|
Database: db.dbName,
|
|
|
|
|
Storage: config.storage,
|
2024-10-22 16:48:42 +02:00
|
|
|
BackupLocation: filepath.Join(storagePath, finalFileName),
|
2024-10-09 22:39:44 +02:00
|
|
|
StartTime: startTime,
|
2024-10-10 04:32:03 +02:00
|
|
|
EndTime: time.Now().Format(utils.TimeFormat()),
|
2024-10-09 22:39:44 +02:00
|
|
|
})
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete old backup
|
2024-09-29 20:00:30 +02:00
|
|
|
if config.prune {
|
2024-10-22 16:48:42 +02:00
|
|
|
err = localStorage.Prune(config.backupRetention)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error deleting old backup from %s storage: %s ", config.storage, err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-29 07:33:26 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete temp
|
2024-08-11 09:39:44 +02:00
|
|
|
deleteTemp()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup completed successfully")
|
2024-07-29 07:33:26 +02:00
|
|
|
}
|
2024-07-29 23:03:28 +02:00
|
|
|
|
2024-09-29 20:00:30 +02:00
|
|
|
func s3Backup(db *dbConfig, config *BackupConfig) {
|
2024-10-22 16:48:42 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup database to s3 storage")
|
2024-10-10 04:32:03 +02:00
|
|
|
startTime = time.Now().Format(utils.TimeFormat())
|
2024-11-19 02:54:31 +01:00
|
|
|
// Backup database
|
2024-09-29 20:00:30 +02:00
|
|
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
|
|
|
|
finalFileName := config.backupFileName
|
|
|
|
|
if config.encryption {
|
2024-10-08 11:04:46 +02:00
|
|
|
encryptBackup(config)
|
2024-09-29 20:00:30 +02:00
|
|
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
|
2024-07-29 23:03:28 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Uploading backup archive to remote storage S3 ... ")
|
2024-10-22 16:48:42 +02:00
|
|
|
awsConfig := initAWSConfig()
|
|
|
|
|
if config.remotePath == "" {
|
|
|
|
|
config.remotePath = awsConfig.remotePath
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup name is %s", finalFileName)
|
2024-10-22 16:48:42 +02:00
|
|
|
s3Storage, err := s3.NewStorage(s3.Config{
|
|
|
|
|
Endpoint: awsConfig.endpoint,
|
|
|
|
|
Bucket: awsConfig.bucket,
|
|
|
|
|
AccessKey: awsConfig.accessKey,
|
|
|
|
|
SecretKey: awsConfig.secretKey,
|
|
|
|
|
Region: awsConfig.region,
|
|
|
|
|
DisableSsl: awsConfig.disableSsl,
|
|
|
|
|
ForcePathStyle: awsConfig.forcePathStyle,
|
|
|
|
|
RemotePath: awsConfig.remotePath,
|
|
|
|
|
LocalPath: tmpPath,
|
|
|
|
|
})
|
2024-07-29 07:33:26 +02:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error creating s3 storage: %s", err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
err = s3Storage.Copy(finalFileName)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error copying backup file: %s", err)
|
2024-07-29 07:33:26 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
// Get backup info
|
2024-10-09 22:39:44 +02:00
|
|
|
fileInfo, err := os.Stat(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error: %s", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
backupSize = fileInfo.Size()
|
2024-07-29 07:33:26 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete backup file from tmp folder
|
2024-09-29 20:00:30 +02:00
|
|
|
err = utils.DeleteFile(filepath.Join(tmpPath, config.backupFileName))
|
2024-07-29 07:33:26 +02:00
|
|
|
if err != nil {
|
2024-08-04 01:20:30 +02:00
|
|
|
fmt.Println("Error deleting file: ", err)
|
2024-07-29 07:33:26 +02:00
|
|
|
|
|
|
|
|
}
|
2024-07-29 23:03:28 +02:00
|
|
|
// Delete old backup
|
2024-09-29 20:00:30 +02:00
|
|
|
if config.prune {
|
2024-10-22 16:48:42 +02:00
|
|
|
err := s3Storage.Prune(config.backupRetention)
|
2024-02-18 13:33:05 +01:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error deleting old backup from %s storage: %s ", config.storage, err)
|
2024-02-18 13:33:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
|
|
|
|
|
logger.Info("Uploading backup archive to remote storage S3 ... done ")
|
|
|
|
|
// Send notification
|
2024-10-09 22:39:44 +02:00
|
|
|
utils.NotifySuccess(&utils.NotificationData{
|
|
|
|
|
File: finalFileName,
|
|
|
|
|
BackupSize: backupSize,
|
|
|
|
|
Database: db.dbName,
|
|
|
|
|
Storage: config.storage,
|
2024-10-22 16:48:42 +02:00
|
|
|
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
2024-10-09 22:39:44 +02:00
|
|
|
StartTime: startTime,
|
2024-10-10 04:32:03 +02:00
|
|
|
EndTime: time.Now().Format(utils.TimeFormat()),
|
2024-10-09 22:39:44 +02:00
|
|
|
})
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete temp
|
2024-08-11 09:39:44 +02:00
|
|
|
deleteTemp()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup completed successfully")
|
2024-10-09 12:05:37 +02:00
|
|
|
|
2024-07-29 23:03:28 +02:00
|
|
|
}
|
2024-09-29 20:00:30 +02:00
|
|
|
func sshBackup(db *dbConfig, config *BackupConfig) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup database to Remote server")
|
2024-10-10 04:32:03 +02:00
|
|
|
startTime = time.Now().Format(utils.TimeFormat())
|
2024-11-19 02:54:31 +01:00
|
|
|
// Backup database
|
2024-09-29 20:00:30 +02:00
|
|
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
|
|
|
|
finalFileName := config.backupFileName
|
|
|
|
|
if config.encryption {
|
2024-10-08 11:04:46 +02:00
|
|
|
encryptBackup(config)
|
2024-09-29 20:00:30 +02:00
|
|
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
|
2024-07-30 19:18:34 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Uploading backup archive to remote storage ... ")
|
|
|
|
|
logger.Info("Backup name is %s", finalFileName)
|
2024-10-22 16:48:42 +02:00
|
|
|
sshConfig, err := loadSSHConfig()
|
2024-07-30 19:18:34 +02:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error loading ssh config: %s", err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
2024-07-30 19:18:34 +02:00
|
|
|
|
2024-10-22 16:48:42 +02:00
|
|
|
sshStorage, err := ssh.NewStorage(ssh.Config{
|
|
|
|
|
Host: sshConfig.hostName,
|
|
|
|
|
Port: sshConfig.port,
|
|
|
|
|
User: sshConfig.user,
|
|
|
|
|
Password: sshConfig.password,
|
|
|
|
|
RemotePath: config.remotePath,
|
|
|
|
|
LocalPath: tmpPath,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error creating SSH storage: %s", err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
err = sshStorage.Copy(finalFileName)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error copying backup file: %s", err)
|
2024-07-30 19:18:34 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
// Get backup info
|
2024-10-09 22:39:44 +02:00
|
|
|
fileInfo, err := os.Stat(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error: %s", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
backupSize = fileInfo.Size()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
|
2024-08-01 07:14:40 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete backup file from tmp folder
|
2024-07-30 19:18:34 +02:00
|
|
|
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error deleting file: %v", err)
|
2024-07-30 19:18:34 +02:00
|
|
|
|
|
|
|
|
}
|
2024-09-29 20:00:30 +02:00
|
|
|
if config.prune {
|
2024-10-22 16:48:42 +02:00
|
|
|
err := sshStorage.Prune(config.backupRetention)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error deleting old backup from %s storage: %s ", config.storage, err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
2024-07-30 19:18:34 +02:00
|
|
|
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Uploading backup archive to remote storage ... done ")
|
|
|
|
|
// Send notification
|
2024-10-09 22:39:44 +02:00
|
|
|
utils.NotifySuccess(&utils.NotificationData{
|
|
|
|
|
File: finalFileName,
|
|
|
|
|
BackupSize: backupSize,
|
|
|
|
|
Database: db.dbName,
|
|
|
|
|
Storage: config.storage,
|
|
|
|
|
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
|
|
|
|
StartTime: startTime,
|
2024-10-10 04:32:03 +02:00
|
|
|
EndTime: time.Now().Format(utils.TimeFormat()),
|
2024-10-09 22:39:44 +02:00
|
|
|
})
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete temp
|
2024-08-11 09:39:44 +02:00
|
|
|
deleteTemp()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup completed successfully")
|
2024-10-09 12:05:37 +02:00
|
|
|
|
2024-07-30 08:59:15 +02:00
|
|
|
}
|
2024-09-29 23:53:19 +02:00
|
|
|
func ftpBackup(db *dbConfig, config *BackupConfig) {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup database to the remote FTP server")
|
2024-10-10 04:32:03 +02:00
|
|
|
startTime = time.Now().Format(utils.TimeFormat())
|
2024-10-09 22:39:44 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Backup database
|
2024-09-29 23:53:19 +02:00
|
|
|
BackupDatabase(db, config.backupFileName, disableCompression)
|
|
|
|
|
finalFileName := config.backupFileName
|
|
|
|
|
if config.encryption {
|
2024-10-08 11:04:46 +02:00
|
|
|
encryptBackup(config)
|
2024-09-29 23:53:19 +02:00
|
|
|
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
|
|
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Uploading backup archive to the remote FTP server ... ")
|
|
|
|
|
logger.Info("Backup name is %s", finalFileName)
|
2024-10-22 16:48:42 +02:00
|
|
|
ftpConfig := loadFtpConfig()
|
|
|
|
|
ftpStorage, err := ftp.NewStorage(ftp.Config{
|
|
|
|
|
Host: ftpConfig.host,
|
|
|
|
|
Port: ftpConfig.port,
|
|
|
|
|
User: ftpConfig.user,
|
|
|
|
|
Password: ftpConfig.password,
|
|
|
|
|
RemotePath: config.remotePath,
|
|
|
|
|
LocalPath: tmpPath,
|
|
|
|
|
})
|
2024-09-29 23:53:19 +02:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error creating SSH storage: %s", err)
|
2024-09-29 23:53:19 +02:00
|
|
|
}
|
2024-10-22 16:48:42 +02:00
|
|
|
err = ftpStorage.Copy(finalFileName)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error copying backup file: %s", err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
|
|
|
|
|
// Get backup info
|
2024-10-09 22:39:44 +02:00
|
|
|
fileInfo, err := os.Stat(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error: %s", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
backupSize = fileInfo.Size()
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete backup file from tmp folder
|
2024-09-29 23:53:19 +02:00
|
|
|
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Error("Error deleting file: %v", err)
|
2024-09-29 23:53:19 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if config.prune {
|
2024-10-22 16:48:42 +02:00
|
|
|
err := ftpStorage.Prune(config.backupRetention)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error deleting old backup from %s storage: %s ", config.storage, err)
|
2024-10-22 16:48:42 +02:00
|
|
|
}
|
2024-09-29 23:53:19 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Uploading backup archive to the remote FTP server ... done ")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
2024-11-19 02:54:31 +01:00
|
|
|
// Send notification
|
2024-10-09 22:39:44 +02:00
|
|
|
utils.NotifySuccess(&utils.NotificationData{
|
|
|
|
|
File: finalFileName,
|
|
|
|
|
BackupSize: backupSize,
|
|
|
|
|
Database: db.dbName,
|
|
|
|
|
Storage: config.storage,
|
|
|
|
|
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
|
|
|
|
StartTime: startTime,
|
2024-10-10 04:32:03 +02:00
|
|
|
EndTime: time.Now().Format(utils.TimeFormat()),
|
2024-10-09 22:39:44 +02:00
|
|
|
})
|
2024-11-19 02:54:31 +01:00
|
|
|
// Delete temp
|
2024-09-29 23:53:19 +02:00
|
|
|
deleteTemp()
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Backup completed successfully")
|
2024-09-29 23:53:19 +02:00
|
|
|
}
|
2024-02-18 13:33:05 +01:00
|
|
|
|
2024-10-08 11:04:46 +02:00
|
|
|
func encryptBackup(config *BackupConfig) {
|
2024-10-13 14:20:43 +02:00
|
|
|
backupFile, err := os.ReadFile(filepath.Join(tmpPath, config.backupFileName))
|
|
|
|
|
outputFile := fmt.Sprintf("%s.%s", filepath.Join(tmpPath, config.backupFileName), gpgExtension)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error reading backup file: %s ", err)
|
2024-10-13 14:20:43 +02:00
|
|
|
}
|
2024-10-08 11:04:46 +02:00
|
|
|
if config.usingKey {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Encrypting backup using public key...")
|
2024-10-13 14:20:43 +02:00
|
|
|
pubKey, err := os.ReadFile(config.publicKey)
|
2024-10-08 11:04:46 +02:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error reading public key: %s ", err)
|
2024-10-08 11:04:46 +02:00
|
|
|
}
|
2024-10-13 14:20:43 +02:00
|
|
|
err = encryptor.EncryptWithPublicKey(backupFile, fmt.Sprintf("%s.%s", filepath.Join(tmpPath, config.backupFileName), gpgExtension), pubKey)
|
|
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("Error encrypting backup file: %v ", err)
|
2024-10-13 14:20:43 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Encrypting backup using public key...done")
|
2024-10-13 14:20:43 +02:00
|
|
|
|
2024-10-08 11:04:46 +02:00
|
|
|
} else if config.passphrase != "" {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Encrypting backup using passphrase...")
|
2024-10-13 14:20:43 +02:00
|
|
|
err := encryptor.Encrypt(backupFile, outputFile, config.passphrase)
|
2024-10-08 11:04:46 +02:00
|
|
|
if err != nil {
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Fatal("error during encrypting backup %v", err)
|
2024-10-08 11:04:46 +02:00
|
|
|
}
|
2024-11-19 02:54:31 +01:00
|
|
|
logger.Info("Encrypting backup using passphrase...done")
|
2024-10-08 11:04:46 +02:00
|
|
|
|
2024-02-17 18:20:35 +01:00
|
|
|
}
|
2024-07-29 23:03:28 +02:00
|
|
|
|
2024-01-20 14:03:06 +01:00
|
|
|
}
|