Skip to content

Commit 0df14f3

Browse files
authored
Merge pull request #159 from jkaninda/refactor
chore: add convert bytes to a human-readable string with the appropri…
2 parents d880f40 + 1b60ca6 commit 0df14f3

File tree

11 files changed

+37
-18
lines changed

11 files changed

+37
-18
lines changed

pkg/azure.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
8383

8484
}
8585

86+
utils.Info("Backup name is %s", finalFileName)
87+
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
8688
utils.Info("Uploading backup archive to Azure Blob storage ... done ")
8789

8890
// Send notification
8991
utils.NotifySuccess(&utils.NotificationData{
9092
File: finalFileName,
91-
BackupSize: backupSize,
93+
BackupSize: utils.ConvertBytes(uint64(backupSize)),
9294
Database: db.dbName,
9395
Storage: config.storage,
9496
BackupLocation: filepath.Join(config.remotePath, finalFileName),

pkg/backup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ func localBackup(db *dbConfig, config *BackupConfig) {
268268
utils.Error("Error: %s", err)
269269
}
270270
backupSize = fileInfo.Size()
271-
utils.Info("Backup name is %s", finalFileName)
272271
localStorage := local.NewStorage(local.Config{
273272
LocalPath: tmpPath,
274273
RemotePath: storagePath,
@@ -277,11 +276,13 @@ func localBackup(db *dbConfig, config *BackupConfig) {
277276
if err != nil {
278277
utils.Fatal("Error copying backup file: %s", err)
279278
}
279+
utils.Info("Backup name is %s", finalFileName)
280+
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
280281
utils.Info("Backup saved in %s", filepath.Join(storagePath, finalFileName))
281282
// Send notification
282283
utils.NotifySuccess(&utils.NotificationData{
283284
File: finalFileName,
284-
BackupSize: backupSize,
285+
BackupSize: utils.ConvertBytes(uint64(backupSize)),
285286
Database: db.dbName,
286287
Storage: config.storage,
287288
BackupLocation: filepath.Join(storagePath, finalFileName),

pkg/remote.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
4646
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
4747
}
4848
utils.Info("Uploading backup archive to remote storage ... ")
49-
utils.Info("Backup name is %s", finalFileName)
5049
sshConfig, err := loadSSHConfig()
5150
if err != nil {
5251
utils.Fatal("Error loading ssh config: %s", err)
@@ -74,6 +73,8 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
7473
utils.Error("Error: %s", err)
7574
}
7675
backupSize = fileInfo.Size()
76+
utils.Info("Backup name is %s", finalFileName)
77+
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
7778
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
7879

7980
// Delete backup file from tmp folder
@@ -93,7 +94,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
9394
// Send notification
9495
utils.NotifySuccess(&utils.NotificationData{
9596
File: finalFileName,
96-
BackupSize: backupSize,
97+
BackupSize: utils.ConvertBytes(uint64(backupSize)),
9798
Database: db.dbName,
9899
Storage: config.storage,
99100
BackupLocation: filepath.Join(config.remotePath, finalFileName),
@@ -199,13 +200,14 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
199200
}
200201

201202
}
202-
203+
utils.Info("Backup name is %s", finalFileName)
204+
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
203205
utils.Info("Uploading backup archive to the remote FTP server ... done ")
204206

205207
// Send notification
206208
utils.NotifySuccess(&utils.NotificationData{
207209
File: finalFileName,
208-
BackupSize: backupSize,
210+
BackupSize: utils.ConvertBytes(uint64(backupSize)),
209211
Database: db.dbName,
210212
Storage: config.storage,
211213
BackupLocation: filepath.Join(config.remotePath, finalFileName),

pkg/s3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
9494
// Send notification
9595
utils.NotifySuccess(&utils.NotificationData{
9696
File: finalFileName,
97-
BackupSize: backupSize,
97+
BackupSize: utils.ConvertBytes(uint64(backupSize)),
9898
Database: db.dbName,
9999
Storage: config.storage,
100100
BackupLocation: filepath.Join(config.remotePath, finalFileName),

pkg/var.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ var (
4545
// dbHVars Required environment variables for database
4646
var dbHVars = []string{
4747
"DB_HOST",
48-
"DB_PORT",
4948
"DB_PASSWORD",
5049
"DB_USERNAME",
5150
"DB_NAME",
5251
}
5352
var tdbRVars = []string{
5453
"TARGET_DB_HOST",
55-
"TARGET_DB_PORT",
5654
"TARGET_DB_NAME",
5755
"TARGET_DB_USERNAME",
5856
"TARGET_DB_PASSWORD",

templates/email-error.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@
4545
</head>
4646
<body>
4747
<h2>🔴 Urgent: Database Backup Failure Notification</h2>
48-
<p>Dear Team,</p>
48+
<p>Hi,</p>
4949
<p>An error occurred during the database backup process. Please review the details below and take the necessary actions:</p>
5050

5151
<div class="details">
5252
<h3>Failure Details:</h3>
5353
<ul>
5454
<li><strong>Database Name:</strong> {{.DatabaseName}}</li>
55-
<li><strong>Error Message:</strong> {{.Error}}</li>
5655
<li><strong>Date:</strong> {{.EndTime}}</li>
5756
<li><strong>Backup Reference:</strong> {{.BackupReference}}</li>
57+
<li><strong>Error Message:</strong> {{.Error}}</li>
5858
</ul>
5959
</div>
6060

templates/email.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</head>
4646
<body>
4747
<h2>✅ Database Backup Successful</h2>
48-
<p>Dear Team,</p>
48+
<p>Hi,</p>
4949
<p>The backup process for the <strong>{{.Database}}</strong> database was successfully completed. Please find the details below:</p>
5050

5151
<div class="details">
@@ -56,7 +56,7 @@
5656
<li><strong>Backup End Time:</strong> {{.EndTime}}</li>
5757
<li><strong>Backup Storage:</strong> {{.Storage}}</li>
5858
<li><strong>Backup Location:</strong> {{.BackupLocation}}</li>
59-
<li><strong>Backup Size:</strong> {{.BackupSize}} bytes</li>
59+
<li><strong>Backup Size:</strong> {{.BackupSize}}</li>
6060
<li><strong>Backup Reference:</strong> {{.BackupReference}}</li>
6161
</ul>
6262
</div>

templates/telegram-error.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
🔴 Urgent: Database Backup Failure Notification
22

3-
Dear Team,
3+
Hi,
44
An error occurred during the database backup process.
55
Please review the details below and take the necessary actions:
66
Failure Details:

templates/telegram.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
✅ Database Backup Successful
22

3-
Dear Team,
3+
Hi,
44
The backup process for the {{.Database}} database was successfully completed.
55
Please find the details below:
66

@@ -10,7 +10,7 @@ Backup Details:
1010
- Backup EndTime: {{.EndTime}}
1111
- Backup Storage: {{.Storage}}
1212
- Backup Location: {{.BackupLocation}}
13-
- Backup Size: {{.BackupSize}} bytes
13+
- Backup Size: {{.BackupSize}}
1414
- Backup Reference: {{.BackupReference}}
1515

1616
You can access the backup at the specified location if needed.

utils/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type MailConfig struct {
3737
}
3838
type NotificationData struct {
3939
File string
40-
BackupSize int64
40+
BackupSize string
4141
Database string
4242
StartTime string
4343
EndTime string

utils/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,19 @@ func CronNextTime(cronExpr string) time.Time {
254254
next := schedule.Next(now)
255255
return next
256256
}
257+
258+
// ConvertBytes converts bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB).
259+
func ConvertBytes(bytes uint64) string {
260+
const (
261+
MiB = 1024 * 1024
262+
GiB = MiB * 1024
263+
)
264+
switch {
265+
case bytes >= GiB:
266+
return fmt.Sprintf("%.2f GiB", float64(bytes)/float64(GiB))
267+
case bytes >= MiB:
268+
return fmt.Sprintf("%.2f MiB", float64(bytes)/float64(MiB))
269+
default:
270+
return fmt.Sprintf("%d bytes", bytes)
271+
}
272+
}

0 commit comments

Comments
 (0)