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 ad2f3a1 commit abf1aeb
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
5 changes: 3 additions & 2 deletions pkg/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,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: utils.BytesToMb(uint64(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 @@ -277,7 +277,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 @@ -286,11 +285,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: utils.BytesToMb(uint64(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 @@ -88,11 +88,13 @@ func sshBackup(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 remote storage ... done ")
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.BytesToMb(uint64(backupSize)),
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down Expand Up @@ -161,7 +163,6 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
}
utils.Info("Uploading backup archive to the remote FTP server ... ")
utils.Info("Backup name is %s", finalFileName)
ftpConfig := loadFtpConfig()
ftpStorage, err := ftp.NewStorage(ftp.Config{
Host: ftpConfig.host,
Expand Down Expand Up @@ -198,13 +199,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: utils.BytesToMb(uint64(backupSize)),
BackupSize: utils.ConvertBytes(uint64(backupSize)),
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
Expand Down
3 changes: 2 additions & 1 deletion pkg/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
}
}
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
utils.Info("Uploading backup archive to remote storage S3 ... done ")
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: utils.BytesToMb(uint64(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 templates/email.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}} MiB</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.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Backup Details:
- Backup EndTime: {{.EndTime}}
- Backup Storage: {{.Storage}}
- Backup Location: {{.BackupLocation}}
- Backup Size: {{.BackupSize}} MiB
- 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 uint64
BackupSize string
Database string
StartTime string
EndTime string
Expand Down
16 changes: 14 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ func CronNextTime(cronExpr string) time.Time {
return next
}

func BytesToMb(b uint64) uint64 {
return b / 1024 / 1024
// 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 abf1aeb

Please sign in to comment.