Skip to content

Commit

Permalink
chore: add convert bytes to a human-readable string with the appropri…
Browse files Browse the repository at this point in the history
…ate unit (bytes, MiB, or GiB)
  • Loading branch information
jkaninda committed Dec 12, 2024
1 parent d880f40 commit 1b60ca6
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 18 deletions.
4 changes: 3 additions & 1 deletion pkg/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ func azureBackup(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 Azure Blob storage ... done ")

// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down
5 changes: 3 additions & 2 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ func localBackup(db *dbConfig, config *BackupConfig) {
utils.Error("Error: %s", err)
}
backupSize = fileInfo.Size()
utils.Info("Backup name is %s", finalFileName)
localStorage := local.NewStorage(local.Config{
LocalPath: tmpPath,
RemotePath: storagePath,
Expand All @@ -277,11 +276,13 @@ func localBackup(db *dbConfig, config *BackupConfig) {
if err != nil {
utils.Fatal("Error copying backup file: %s", err)
}
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))
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(storagePath, finalFileName),
Expand Down
10 changes: 6 additions & 4 deletions pkg/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
}
utils.Info("Uploading backup archive to remote storage ... ")
utils.Info("Backup name is %s", finalFileName)
sshConfig, err := loadSSHConfig()
if err != nil {
utils.Fatal("Error loading ssh config: %s", err)
Expand Down Expand Up @@ -74,6 +73,8 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
utils.Error("Error: %s", err)
}
backupSize = fileInfo.Size()
utils.Info("Backup name is %s", finalFileName)
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))

// Delete backup file from tmp folder
Expand All @@ -93,7 +94,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down Expand Up @@ -199,13 +200,14 @@ 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 ")

// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down
2 changes: 1 addition & 1 deletion pkg/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down
2 changes: 0 additions & 2 deletions pkg/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ var (
// dbHVars Required environment variables for database
var dbHVars = []string{
"DB_HOST",
"DB_PORT",
"DB_PASSWORD",
"DB_USERNAME",
"DB_NAME",
}
var tdbRVars = []string{
"TARGET_DB_HOST",
"TARGET_DB_PORT",
"TARGET_DB_NAME",
"TARGET_DB_USERNAME",
"TARGET_DB_PASSWORD",
Expand Down
4 changes: 2 additions & 2 deletions templates/email-error.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@
</head>
<body>
<h2>🔴 Urgent: Database Backup Failure Notification</h2>
<p>Dear Team,</p>
<p>Hi,</p>
<p>An error occurred during the database backup process. Please review the details below and take the necessary actions:</p>

<div class="details">
<h3>Failure Details:</h3>
<ul>
<li><strong>Database Name:</strong> {{.DatabaseName}}</li>
<li><strong>Error Message:</strong> {{.Error}}</li>
<li><strong>Date:</strong> {{.EndTime}}</li>
<li><strong>Backup Reference:</strong> {{.BackupReference}}</li>
<li><strong>Error Message:</strong> {{.Error}}</li>
</ul>
</div>

Expand Down
4 changes: 2 additions & 2 deletions templates/email.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</head>
<body>
<h2>✅ Database Backup Successful</h2>
<p>Dear Team,</p>
<p>Hi,</p>
<p>The backup process for the <strong>{{.Database}}</strong> database was successfully completed. Please find the details below:</p>

<div class="details">
Expand All @@ -56,7 +56,7 @@
<li><strong>Backup End Time:</strong> {{.EndTime}}</li>
<li><strong>Backup Storage:</strong> {{.Storage}}</li>
<li><strong>Backup Location:</strong> {{.BackupLocation}}</li>
<li><strong>Backup Size:</strong> {{.BackupSize}} bytes</li>
<li><strong>Backup Size:</strong> {{.BackupSize}}</li>
<li><strong>Backup Reference:</strong> {{.BackupReference}}</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/telegram-error.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
🔴 Urgent: Database Backup Failure Notification

Dear Team,
Hi,
An error occurred during the database backup process.
Please review the details below and take the necessary actions:
Failure Details:
Expand Down
4 changes: 2 additions & 2 deletions templates/telegram.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
✅ Database Backup Successful

Dear Team,
Hi,
The backup process for the {{.Database}} database was successfully completed.
Please find the details below:

Expand All @@ -10,7 +10,7 @@ Backup Details:
- Backup EndTime: {{.EndTime}}
- Backup Storage: {{.Storage}}
- Backup Location: {{.BackupLocation}}
- Backup Size: {{.BackupSize}} bytes
- Backup Size: {{.BackupSize}}
- Backup Reference: {{.BackupReference}}

You can access the backup at the specified location if needed.
2 changes: 1 addition & 1 deletion utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type MailConfig struct {
}
type NotificationData struct {
File string
BackupSize int64
BackupSize string
Database string
StartTime string
EndTime string
Expand Down
16 changes: 16 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,19 @@ func CronNextTime(cronExpr string) time.Time {
next := schedule.Next(now)
return next
}

// ConvertBytes converts bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB).
func ConvertBytes(bytes uint64) string {
const (
MiB = 1024 * 1024
GiB = MiB * 1024
)
switch {
case bytes >= GiB:
return fmt.Sprintf("%.2f GiB", float64(bytes)/float64(GiB))
case bytes >= MiB:
return fmt.Sprintf("%.2f MiB", float64(bytes)/float64(MiB))
default:
return fmt.Sprintf("%d bytes", bytes)
}
}

0 comments on commit 1b60ca6

Please sign in to comment.