mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
refactor: clean up code
This commit is contained in:
@@ -195,7 +195,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Done("Database has been backed up")
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
} else {
|
||||
// Execute mysqldump
|
||||
@@ -217,7 +217,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Done("Database has been backed up")
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Fatal("Error deleting old backup from S3: %s ", err)
|
||||
}
|
||||
}
|
||||
utils.Done("Uploading backup archive to remote storage S3 ... done ")
|
||||
utils.Info("Uploading backup archive to remote storage S3 ... done ")
|
||||
//Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -353,7 +353,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
}
|
||||
|
||||
utils.Done("Uploading backup archive to remote storage ... done ")
|
||||
utils.Info("Uploading backup archive to remote storage ... done ")
|
||||
//Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -405,7 +405,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
}
|
||||
|
||||
utils.Done("Uploading backup archive to the remote FTP server ... done ")
|
||||
utils.Info("Uploading backup archive to the remote FTP server ... done ")
|
||||
//Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
|
||||
@@ -39,7 +39,7 @@ func moveToBackup(backupFileName string, destinationPath string) {
|
||||
fmt.Println("Error deleting file:", err)
|
||||
|
||||
}
|
||||
utils.Done("Database has been backed up and copied to %s", filepath.Join(destinationPath, backupFileName))
|
||||
utils.Info("Database has been backed up and copied to %s", filepath.Join(destinationPath, backupFileName))
|
||||
}
|
||||
func deleteOldBackup(retentionDays int) {
|
||||
utils.Info("Deleting old backups...")
|
||||
@@ -54,7 +54,7 @@ func deleteOldBackup(retentionDays int) {
|
||||
if err != nil {
|
||||
utils.Fatal(fmt.Sprintf("Error: %s", err))
|
||||
} else {
|
||||
utils.Done("File %s has been deleted successfully", filePath)
|
||||
utils.Info("File %s has been deleted successfully", filePath)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func deleteOldBackup(retentionDays int) {
|
||||
utils.Fatal(fmt.Sprintf("Error: %s", err))
|
||||
return
|
||||
}
|
||||
utils.Done("Deleting old backups...done")
|
||||
utils.Info("Deleting old backups...done")
|
||||
|
||||
}
|
||||
func deleteTemp() {
|
||||
|
||||
@@ -125,7 +125,7 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||
utils.Fatal("Error, in restoring the database %v", err)
|
||||
}
|
||||
utils.Info("Restoring database... done")
|
||||
utils.Done("Database has been restored")
|
||||
utils.Info("Database has been restored")
|
||||
//Delete temp
|
||||
deleteTemp()
|
||||
|
||||
@@ -137,7 +137,7 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||
utils.Fatal("Error in restoring the database %v", err)
|
||||
}
|
||||
utils.Info("Restoring database... done")
|
||||
utils.Done("Database has been restored")
|
||||
utils.Info("Database has been restored")
|
||||
//Delete temp
|
||||
deleteTemp()
|
||||
} else {
|
||||
|
||||
@@ -106,6 +106,8 @@ func DownloadFile(destinationPath, key, bucket, prefix string) error {
|
||||
return nil
|
||||
}
|
||||
func DeleteOldBackup(bucket, prefix string, retention int) error {
|
||||
utils.Info("Deleting old backups...")
|
||||
utils.Info("Bucket %s Prefix: %s Retention: %d", bucket, prefix, retention)
|
||||
sess, err := CreateSession()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -113,7 +115,7 @@ func DeleteOldBackup(bucket, prefix string, retention int) error {
|
||||
|
||||
svc := s3.New(sess)
|
||||
|
||||
// Get the current time and the time threshold for 7 days ago
|
||||
// Get the current time
|
||||
now := time.Now()
|
||||
backupRetentionDays := now.AddDate(0, 0, -retention)
|
||||
|
||||
@@ -125,6 +127,7 @@ func DeleteOldBackup(bucket, prefix string, retention int) error {
|
||||
err = svc.ListObjectsV2Pages(listObjectsInput, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
|
||||
for _, object := range page.Contents {
|
||||
if object.LastModified.Before(backupRetentionDays) {
|
||||
utils.Info("Deleting old backup: %s", *object.Key)
|
||||
// Object is older than retention days, delete it
|
||||
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
@@ -133,7 +136,7 @@ func DeleteOldBackup(bucket, prefix string, retention int) error {
|
||||
if err != nil {
|
||||
utils.Info("Failed to delete object %s: %v", *object.Key, err)
|
||||
} else {
|
||||
utils.Info("Deleted object %s\n", *object.Key)
|
||||
utils.Info("Deleted object %s", *object.Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,6 +146,6 @@ func DeleteOldBackup(bucket, prefix string, retention int) error {
|
||||
utils.Error("Failed to list objects: %v", err)
|
||||
}
|
||||
|
||||
utils.Info("Finished deleting old files.")
|
||||
utils.Info("Deleting old backups...done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,15 +41,6 @@ func Error(msg string, args ...any) {
|
||||
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
func Done(msg string, args ...any) {
|
||||
var currentTime = time.Now().Format("2006/01/02 15:04:05")
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// Fatal logs an error message and exits the program
|
||||
func Fatal(msg string, args ...any) {
|
||||
|
||||
Reference in New Issue
Block a user