refactor: refactoring of code, improvement of deleteOldBackup function

This commit is contained in:
2024-02-18 13:33:05 +01:00
parent 2f741baeec
commit 641ac941a0
4 changed files with 45 additions and 34 deletions

View File

@@ -31,7 +31,7 @@ PostgreSQL Backup and Restoration tool. Backup database to AWS S3 storage or any
### Usage ### Usage
| Options | Shorts | Usage | | Options | Shorts | Usage |
|-----------------------|--------|-----------------------------------------------------------------------| |-----------------------|--------|----------------------------------------------------------------------|
| pg-bkup | bkup | CLI utility | | pg-bkup | bkup | CLI utility |
| backup | | Backup database operation | | backup | | Backup database operation |
| restore | | Restore database operation | | restore | | Restore database operation |
@@ -43,8 +43,8 @@ PostgreSQL Backup and Restoration tool. Backup database to AWS S3 storage or any
| --port | -p | Set database port (default: 5432) | | --port | -p | Set database port (default: 5432) |
| --mode | -m | Set execution mode. default or scheduled (default: default) | | --mode | -m | Set execution mode. default or scheduled (default: default) |
| --disable-compression | | Disable database backup compression | | --disable-compression | | Disable database backup compression |
| --prune | | Delete old backup | | --prune | | Delete old backup, default disabled |
| --keep-last | | keep all backup and delete within this time interval, default 7 days | | --keep-last | | Delete files created more than specified days ago, default 7 days |
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") | | --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
| --timeout | -t | Set timeout (default: 60s) | | --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit | | --help | -h | Print this help message and exit |

View File

@@ -23,8 +23,8 @@ func init() {
//Backup //Backup
BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled") BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time") BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
BackupCmd.PersistentFlags().BoolP("prune", "", false, "Prune old backup") BackupCmd.PersistentFlags().BoolP("prune", "", false, "Delete old backup, default disabled")
BackupCmd.PersistentFlags().IntP("keep-last", "", 7, "keep all backup and delete within this time interval, default 7 days") BackupCmd.PersistentFlags().IntP("keep-last", "", 7, "Delete files created more than specified days ago, default 7 days")
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression") BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
} }

View File

@@ -21,7 +21,7 @@ ENV ACCESS_KEY=""
ENV SECRET_KEY="" ENV SECRET_KEY=""
ENV S3_ENDPOINT=https://s3.amazonaws.com ENV S3_ENDPOINT=https://s3.amazonaws.com
ARG DEBIAN_FRONTEND=noninteractive ARG DEBIAN_FRONTEND=noninteractive
ENV VERSION="v0.5" ENV VERSION="v0.6"
LABEL authors="Jonas Kaninda" LABEL authors="Jonas Kaninda"
RUN apt-get update -qq RUN apt-get update -qq

View File

@@ -185,26 +185,37 @@ func deleteOldBackup(keepLast int) {
backupDir := storagePath + "/" backupDir := storagePath + "/"
// Get current time // Get current time
currentTime := time.Now() currentTime := time.Now()
// Walk through files in the directory // Delete file
err := filepath.Walk(backupDir, func(path string, info os.FileInfo, err error) error { deleteFile := func(filePath string) error {
err := os.Remove(filePath)
if err != nil {
utils.Fatal("Error:", err)
} else {
utils.Done("File", filePath, "deleted successfully")
}
return err
}
// Walk through the directory and delete files modified more than specified days ago
err := filepath.Walk(backupDir, func(filePath string, fileInfo os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
// Check if the file is older than defined day days // Check if it's a regular file and if it was modified more than specified days ago
if info.Mode().IsRegular() && info.ModTime().Before(currentTime.AddDate(0, 0, -keepLast)) { if fileInfo.Mode().IsRegular() {
// Remove the file timeDiff := currentTime.Sub(fileInfo.ModTime())
err := os.Remove(path) if timeDiff.Hours() > 24*float64(keepLast) {
err := deleteFile(filePath)
if err != nil { if err != nil {
utils.Fatal("Error removing file ", path, err) return err
} else { }
utils.Done("Removed file: ", path)
} }
} }
return nil return nil
}) })
if err != nil { if err != nil {
utils.Fatal("Error walking through directory: ", err) utils.Fatal("Error:", err)
return
} }
} }