Skip to content

Commit

Permalink
feat: add backup duration
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaninda committed Jan 22, 2025
1 parent 3a1bfc5 commit 3e8bfab
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a // indirect
github.com/jlaffaye/ftp v0.2.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e h1:jtFKZHt/PLGQ
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e/go.mod h1:Y1EXpPWQ9PNd7y7E6ez3xgnzZc8fuDWXwX/1/dXNCE4=
github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU=
github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps=
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150 h1:AgcKk58P/Z+u4DBE2MTyZ6kCweA89YUpX7TuttHS3oQ=
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a h1:ZTpKujQGhEF266RWkD2cXnCsafk3R2+sGtfbzCQSs1s=
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand Down
9 changes: 5 additions & 4 deletions pkg/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package pkg
import (
"fmt"
"github.com/jkaninda/go-storage/pkg/azure"
goutils "github.com/jkaninda/go-utils"
"github.com/jkaninda/mysql-bkup/utils"

"os"
Expand All @@ -36,7 +37,6 @@ import (

func azureBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to Azure Blob Storage")
startTime = time.Now().Format(utils.TimeFormat())

// Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
Expand Down Expand Up @@ -87,19 +87,20 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
utils.Info("Uploading backup archive to Azure Blob storage ... done ")

duration := goutils.FormatDuration(time.Since(startTime), 0)

// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
Duration: duration,
})
// Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
utils.Info("Backup successfully completed in %s", duration)
}
func azureRestore(db *dbConfig, conf *RestoreConfig) {
utils.Info("Restore database from Azure Blob storage")
Expand Down
12 changes: 7 additions & 5 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"
"github.com/jkaninda/encryptor"
"github.com/jkaninda/go-storage/pkg/local"
goutils "github.com/jkaninda/go-utils"
"github.com/jkaninda/mysql-bkup/utils"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -107,6 +108,7 @@ func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
// BackupTask backups database
func BackupTask(db *dbConfig, config *BackupConfig) {
utils.Info("Starting backup task...")
startTime = time.Now()
// Generate file name
backupFileName := fmt.Sprintf("%s_%s.sql.gz", db.dbName, time.Now().Format("20060102_150405"))
if config.disableCompression {
Expand All @@ -118,7 +120,7 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
localBackup(db, config)
case "s3", "S3":
s3Backup(db, config)
case "ssh", "SSH", "remote":
case "ssh", "SSH", "remote", "sftp":
sshBackup(db, config)
case "ftp", "FTP":
ftpBackup(db, config)
Expand Down Expand Up @@ -256,7 +258,6 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
}
func localBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to local storage")
startTime = time.Now().Format(utils.TimeFormat())
BackupDatabase(db, config.backupFileName, disableCompression)
finalFileName := config.backupFileName
if config.encryption {
Expand All @@ -279,15 +280,16 @@ func localBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup name is %s", finalFileName)
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
utils.Info("Backup saved in %s", filepath.Join(storagePath, finalFileName))
duration := goutils.FormatDuration(time.Since(startTime), 0)

// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(storagePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
Duration: duration,
})
// Delete old backup
if config.prune {
Expand All @@ -299,7 +301,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
}
// Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
utils.Info("Backup successfully completed in %s", duration)
}

func encryptBackup(config *BackupConfig) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"fmt"
"github.com/jkaninda/go-storage/pkg/ftp"
"github.com/jkaninda/go-storage/pkg/ssh"
goutils "github.com/jkaninda/go-utils"
"github.com/jkaninda/mysql-bkup/utils"

"os"
Expand All @@ -37,7 +38,6 @@ import (

func sshBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to Remote server")
startTime = time.Now().Format(utils.TimeFormat())
// Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
finalFileName := config.backupFileName
Expand Down Expand Up @@ -91,19 +91,20 @@ func sshBackup(db *dbConfig, config *BackupConfig) {

}
utils.Info("Uploading backup archive to remote storage ... done ")
duration := goutils.FormatDuration(time.Since(startTime), 0)

// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
Duration: duration,
})
// Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
utils.Info("Backup successfully completed in %s", duration)

}
func remoteRestore(db *dbConfig, conf *RestoreConfig) {
Expand Down Expand Up @@ -153,7 +154,6 @@ func ftpRestore(db *dbConfig, conf *RestoreConfig) {
}
func ftpBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to the remote FTP server")
startTime = time.Now().Format(utils.TimeFormat())

// Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
Expand Down Expand Up @@ -203,6 +203,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup name is %s", finalFileName)
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
utils.Info("Uploading backup archive to the remote FTP server ... done ")
duration := goutils.FormatDuration(time.Since(startTime), 0)

// Send notification
utils.NotifySuccess(&utils.NotificationData{
Expand All @@ -211,10 +212,9 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
Duration: duration,
})
// Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
utils.Info("Backup successfully completed in %s", duration)
}
8 changes: 4 additions & 4 deletions pkg/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package pkg
import (
"fmt"
"github.com/jkaninda/go-storage/pkg/s3"
goutils "github.com/jkaninda/go-utils"
"github.com/jkaninda/mysql-bkup/utils"

"os"
Expand All @@ -37,7 +38,6 @@ import (
func s3Backup(db *dbConfig, config *BackupConfig) {

utils.Info("Backup database to s3 storage")
startTime = time.Now().Format(utils.TimeFormat())
// Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
finalFileName := config.backupFileName
Expand Down Expand Up @@ -91,19 +91,19 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
}
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
utils.Info("Uploading backup archive to remote storage S3 ... done ")
duration := goutils.FormatDuration(time.Since(startTime), 0)
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
Duration: duration,
})
// Delete temp
deleteTemp()
utils.Info("Backup completed successfully")
utils.Info("Backup successfully completed in %s", duration)

}
func s3Restore(db *dbConfig, conf *RestoreConfig) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SOFTWARE.

package pkg

import "time"

const tmpPath = "/tmp/backup"
const gpgHome = "/config/gnupg"
const gpgExtension = "gpg"
Expand All @@ -39,7 +41,7 @@ var (
encryption = false
usingKey = false
backupSize int64 = 0
startTime string
startTime = time.Now()
)

// dbHVars Required environment variables for database
Expand Down
3 changes: 1 addition & 2 deletions templates/email.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
<h3>Backup Details:</h3>
<ul>
<li><strong>Database Name:</strong> {{.Database}}</li>
<li><strong>Backup Start Time:</strong> {{.StartTime}}</li>
<li><strong>Backup End Time:</strong> {{.EndTime}}</li>
<li><strong>Backup Duration:</strong> {{.Duration}}</li>
<li><strong>Backup Storage:</strong> {{.Storage}}</li>
<li><strong>Backup Location:</strong> {{.BackupLocation}}</li>
<li><strong>Backup Size:</strong> {{.BackupSize}}</li>
Expand Down
3 changes: 1 addition & 2 deletions templates/telegram.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Please find the details below:

Backup Details:
- Database Name: {{.Database}}
- Backup Start Time: {{.StartTime}}
- Backup EndTime: {{.EndTime}}
- Backup Duration: {{.Duration}}
- Backup Storage: {{.Storage}}
- Backup Location: {{.BackupLocation}}
- Backup Size: {{.BackupSize}}
Expand Down
3 changes: 1 addition & 2 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ type NotificationData struct {
File string
BackupSize string
Database string
StartTime string
EndTime string
Duration string
Storage string
BackupLocation string
BackupReference string
Expand Down

0 comments on commit 3e8bfab

Please sign in to comment.