diff --git a/go.mod b/go.mod index 0d4b840..1647983 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0206d72..c4d407b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/azure.go b/pkg/azure.go index 7a1132e..64f7048 100644 --- a/pkg/azure.go +++ b/pkg/azure.go @@ -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" @@ -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) @@ -87,6 +87,8 @@ 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, @@ -94,12 +96,11 @@ func azureBackup(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) } func azureRestore(db *dbConfig, conf *RestoreConfig) { utils.Info("Restore database from Azure Blob storage") diff --git a/pkg/backup.go b/pkg/backup.go index 3ff20a3..0836021 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -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" @@ -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 { @@ -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) @@ -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 { @@ -279,6 +280,8 @@ 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, @@ -286,8 +289,7 @@ func localBackup(db *dbConfig, config *BackupConfig) { 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 { @@ -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) { diff --git a/pkg/remote.go b/pkg/remote.go index 2003301..0e126a5 100644 --- a/pkg/remote.go +++ b/pkg/remote.go @@ -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" @@ -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 @@ -91,6 +91,8 @@ 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, @@ -98,12 +100,11 @@ func sshBackup(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) } func remoteRestore(db *dbConfig, conf *RestoreConfig) { @@ -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) @@ -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{ @@ -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) } diff --git a/pkg/s3.go b/pkg/s3.go index 1413254..4f903a5 100644 --- a/pkg/s3.go +++ b/pkg/s3.go @@ -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" @@ -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 @@ -91,6 +91,7 @@ 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, @@ -98,12 +99,11 @@ func s3Backup(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) } func s3Restore(db *dbConfig, conf *RestoreConfig) { diff --git a/pkg/var.go b/pkg/var.go index 262c631..4b8327d 100644 --- a/pkg/var.go +++ b/pkg/var.go @@ -24,6 +24,8 @@ SOFTWARE. package pkg +import "time" + const tmpPath = "/tmp/backup" const gpgHome = "/config/gnupg" const gpgExtension = "gpg" @@ -39,7 +41,7 @@ var ( encryption = false usingKey = false backupSize int64 = 0 - startTime string + startTime = time.Now() ) // dbHVars Required environment variables for database diff --git a/templates/email.tmpl b/templates/email.tmpl index 98d64a0..d27aa43 100644 --- a/templates/email.tmpl +++ b/templates/email.tmpl @@ -52,8 +52,7 @@

Backup Details: