refactor: refactoring of code, improvement of deleteOldBackup function

This commit is contained in:
2024-02-18 13:27:54 +01:00
parent e0457a4ed8
commit 00f2fca8e4
4 changed files with 45 additions and 34 deletions

View File

@@ -38,25 +38,25 @@ MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 A
## Usage ## Usage
| Options | Shorts | Usage | | Options | Shorts | Usage |
|-----------------------|--------|-----------------------------------------------------------------------| |-----------------------|--------|--------------------------------------------------------------------|
| mysql-bkup | bkup | CLI utility | | mysql-bkup | bkup | CLI utility |
| backup | | Backup database operation | | backup | | Backup database operation |
| restore | | Restore database operation | | restore | | Restore database operation |
| history | | Show the history of backup | | history | | Show the history of backup |
| --storage | -s | Set storage. local or s3 (default: local) | | --storage | -s | Set storage. local or s3 (default: local) |
| --file | -f | Set file name for restoration | | --file | -f | Set file name for restoration |
| --path | | Set s3 path without file name. eg: /custom_path | | --path | | Set s3 path without file name. eg: /custom_path |
| --dbname | -d | Set database name | | --dbname | -d | Set database name |
| --port | -p | Set database port (default: 3306) | | --port | -p | Set database port (default: 3306) |
| --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 |
| --version | -V | Print version information and exit | | --version | -V | Print version information and exit |
## Environment variables ## Environment variables

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.6" ENV VERSION="v0.7"
LABEL authors="Jonas Kaninda" LABEL authors="Jonas Kaninda"
RUN apt-get update -qq RUN apt-get update -qq

View File

@@ -173,26 +173,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) {
if err != nil { err := deleteFile(filePath)
utils.Fatal("Error removing file ", path, err) if err != nil {
} else { return err
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
} }
} }