From 641ac941a0e89edf7ea947505c97ea835fc4be4c Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Sun, 18 Feb 2024 13:33:05 +0100 Subject: [PATCH] refactor: refactoring of code, improvement of deleteOldBackup function --- README.md | 38 +++++++++++++++++++------------------- cmd/backup.go | 4 ++-- docker/Dockerfile | 2 +- pkg/backup.go | 35 +++++++++++++++++++++++------------ 4 files changed, 45 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index d0964ef..9d0b296 100644 --- a/README.md +++ b/README.md @@ -30,25 +30,25 @@ PostgreSQL Backup and Restoration tool. Backup database to AWS S3 storage or any ### Usage -| Options | Shorts | Usage | -|-----------------------|--------|-----------------------------------------------------------------------| -| pg-bkup | bkup | CLI utility | -| backup | | Backup database operation | -| restore | | Restore database operation | -| history | | Show the history of backup | -| --storage | -s | Set storage. local or s3 (default: local) | -| --file | -f | Set file name for restoration | -| --path | | Set s3 path without file name. eg: /custom_path | -| --dbname | -d | Set database name | -| --port | -p | Set database port (default: 5432) | -| --mode | -m | Set execution mode. default or scheduled (default: default) | -| --disable-compression | | Disable database backup compression | -| --prune | | Delete old backup | -| --keep-last | | keep all backup and delete within this time interval, default 7 days | -| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") | -| --timeout | -t | Set timeout (default: 60s) | -| --help | -h | Print this help message and exit | -| --version | -V | Print version information and exit | +| Options | Shorts | Usage | +|-----------------------|--------|----------------------------------------------------------------------| +| pg-bkup | bkup | CLI utility | +| backup | | Backup database operation | +| restore | | Restore database operation | +| history | | Show the history of backup | +| --storage | -s | Set storage. local or s3 (default: local) | +| --file | -f | Set file name for restoration | +| --path | | Set s3 path without file name. eg: /custom_path | +| --dbname | -d | Set database name | +| --port | -p | Set database port (default: 5432) | +| --mode | -m | Set execution mode. default or scheduled (default: default) | +| --disable-compression | | Disable database backup compression | +| --prune | | Delete old backup, default disabled | +| --keep-last | | Delete files created more than specified days ago, default 7 days | +| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") | +| --timeout | -t | Set timeout (default: 60s) | +| --help | -h | Print this help message and exit | +| --version | -V | Print version information and exit | ## Environment variables diff --git a/cmd/backup.go b/cmd/backup.go index 0f6ec8a..3f5aa7c 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -23,8 +23,8 @@ func init() { //Backup BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled") BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time") - BackupCmd.PersistentFlags().BoolP("prune", "", false, "Prune old backup") - BackupCmd.PersistentFlags().IntP("keep-last", "", 7, "keep all backup and delete within this time interval, default 7 days") + BackupCmd.PersistentFlags().BoolP("prune", "", false, "Delete old backup, default disabled") + 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") } diff --git a/docker/Dockerfile b/docker/Dockerfile index 466147b..38e82b8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -21,7 +21,7 @@ ENV ACCESS_KEY="" ENV SECRET_KEY="" ENV S3_ENDPOINT=https://s3.amazonaws.com ARG DEBIAN_FRONTEND=noninteractive -ENV VERSION="v0.5" +ENV VERSION="v0.6" LABEL authors="Jonas Kaninda" RUN apt-get update -qq diff --git a/pkg/backup.go b/pkg/backup.go index ca8c96c..99c1a41 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -185,26 +185,37 @@ func deleteOldBackup(keepLast int) { backupDir := storagePath + "/" // Get current time currentTime := time.Now() - // Walk through files in the directory - err := filepath.Walk(backupDir, func(path string, info os.FileInfo, err error) error { + // Delete file + 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 { return err } - // Check if the file is older than defined day days - if info.Mode().IsRegular() && info.ModTime().Before(currentTime.AddDate(0, 0, -keepLast)) { - // Remove the file - err := os.Remove(path) - if err != nil { - utils.Fatal("Error removing file ", path, err) - } else { - utils.Done("Removed file: ", path) + // Check if it's a regular file and if it was modified more than specified days ago + if fileInfo.Mode().IsRegular() { + timeDiff := currentTime.Sub(fileInfo.ModTime()) + if timeDiff.Hours() > 24*float64(keepLast) { + err := deleteFile(filePath) + if err != nil { + return err + } } } return nil }) if err != nil { - utils.Fatal("Error walking through directory: ", err) + utils.Fatal("Error:", err) + return } - }