mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
refactor: refactoring of code, improvement of deleteOldBackup function
This commit is contained in:
@@ -39,7 +39,7 @@ MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 A
|
||||
## Usage
|
||||
|
||||
| Options | Shorts | Usage |
|
||||
|-----------------------|--------|-----------------------------------------------------------------------|
|
||||
|-----------------------|--------|--------------------------------------------------------------------|
|
||||
| mysql-bkup | bkup | CLI utility |
|
||||
| backup | | Backup database operation |
|
||||
| restore | | Restore database operation |
|
||||
@@ -51,8 +51,8 @@ MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 A
|
||||
| --port | -p | Set database port (default: 3306) |
|
||||
| --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 |
|
||||
| --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 |
|
||||
|
||||
@@ -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")
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ ENV ACCESS_KEY=""
|
||||
ENV SECRET_KEY=""
|
||||
ENV S3_ENDPOINT=https://s3.amazonaws.com
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
ENV VERSION="v0.6"
|
||||
ENV VERSION="v0.7"
|
||||
LABEL authors="Jonas Kaninda"
|
||||
|
||||
RUN apt-get update -qq
|
||||
|
||||
@@ -173,26 +173,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)
|
||||
// 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 {
|
||||
utils.Fatal("Error removing file ", path, err)
|
||||
} else {
|
||||
utils.Done("Removed file: ", path)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
utils.Fatal("Error walking through directory: ", err)
|
||||
utils.Fatal("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user