feat: add backup prune, to delete old backup

This commit is contained in:
2024-02-17 18:20:35 +01:00
parent 6426be8b8e
commit af8a50f646
5 changed files with 65 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ docker-build:
docker build -f docker/Dockerfile -t jkaninda/pg-bkup:latest . docker build -f docker/Dockerfile -t jkaninda/pg-bkup:latest .
docker-run: docker-build docker-run: docker-build
docker run --rm --network internal --privileged --device /dev/fuse --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" jkaninda/pg-bkup bkup backup docker run --rm --network internal --privileged --device /dev/fuse --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" jkaninda/pg-bkup bkup backup --prune --keep-last 2
docker-run-scheduled: docker-build docker-run-scheduled: docker-build

View File

@@ -30,23 +30,25 @@ 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 |
| 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: 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 |
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") | | --prune | | Delete old backup |
| --timeout | -t | Set timeout (default: 60s) | | --keep-last | | keep all backup and delete within this time interval, default 7 days |
| --help | -h | Print this help message and exit | | --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
| --version | -V | Print version information and exit | | --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit |
## Environment variables ## Environment variables

View File

@@ -23,6 +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().IntP("keep-last", "", 7, "keep all backup and delete within this time interval, default 7 days")
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression") BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
} }

View File

@@ -5,7 +5,6 @@ Copyright © 2024 Jonas Kaninda <jonaskaninda@gmail.com>
package cmd package cmd
import ( import (
"fmt"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os" "os"
@@ -18,16 +17,6 @@ var rootCmd = &cobra.Command{
Long: `PostgreSQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`, Long: `PostgreSQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`,
Example: utils.MainExample, Example: utils.MainExample,
Version: appVersion, Version: appVersion,
//TODO: To remove
//For old user || To remove
Run: func(cmd *cobra.Command, args []string) {
if operation != "" {
if operation == "backup" || operation == "restore" {
fmt.Println(utils.Notice)
utils.Fatal("New config required, please check --help")
}
}
},
} }
var operation = "" var operation = ""
var s3Path = "/pg-bkup" var s3Path = "/pg-bkup"
@@ -49,11 +38,6 @@ func init() {
rootCmd.PersistentFlags().IntP("port", "p", 5432, "Set database port") rootCmd.PersistentFlags().IntP("port", "p", 5432, "Set database port")
rootCmd.PersistentFlags().StringVarP(&operation, "operation", "o", "", "Set operation, for old version only") rootCmd.PersistentFlags().StringVarP(&operation, "operation", "o", "", "Set operation, for old version only")
rootCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
rootCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
rootCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
rootCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
rootCmd.AddCommand(VersionCmd) rootCmd.AddCommand(VersionCmd)
rootCmd.AddCommand(BackupCmd) rootCmd.AddCommand(BackupCmd)
rootCmd.AddCommand(RestoreCmd) rootCmd.AddCommand(RestoreCmd)

View File

@@ -11,6 +11,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"time" "time"
) )
@@ -27,16 +28,18 @@ func StartBackup(cmd *cobra.Command) {
s3Path = utils.GetEnv(cmd, "path", "S3_PATH") s3Path = utils.GetEnv(cmd, "path", "S3_PATH")
storage = utils.GetEnv(cmd, "storage", "STORAGE") storage = utils.GetEnv(cmd, "storage", "STORAGE")
file = utils.GetEnv(cmd, "file", "FILE_NAME") file = utils.GetEnv(cmd, "file", "FILE_NAME")
keepLast, _ := cmd.Flags().GetInt("keep-last")
prune, _ := cmd.Flags().GetBool("prune")
disableCompression, _ = cmd.Flags().GetBool("disable-compression") disableCompression, _ = cmd.Flags().GetBool("disable-compression")
executionMode, _ = cmd.Flags().GetString("mode") executionMode, _ = cmd.Flags().GetString("mode")
if executionMode == "default" { if executionMode == "default" {
if storage == "s3" { if storage == "s3" {
utils.Info("Backup database to s3 storage") utils.Info("Backup database to s3 storage")
s3Backup(disableCompression, s3Path) s3Backup(disableCompression, s3Path, prune, keepLast)
} else { } else {
utils.Info("Backup database to local storage") utils.Info("Backup database to local storage")
BackupDatabase(disableCompression) BackupDatabase(disableCompression, prune, keepLast)
} }
} else if executionMode == "scheduled" { } else if executionMode == "scheduled" {
@@ -75,7 +78,7 @@ func scheduledMode() {
} }
// BackupDatabase backup database // BackupDatabase backup database
func BackupDatabase(disableCompression bool) { func BackupDatabase(disableCompression bool, prune bool, keepLast int) {
dbHost = os.Getenv("DB_HOST") dbHost = os.Getenv("DB_HOST")
dbPassword = os.Getenv("DB_PASSWORD") dbPassword = os.Getenv("DB_PASSWORD")
dbUserName = os.Getenv("DB_USERNAME") dbUserName = os.Getenv("DB_USERNAME")
@@ -151,6 +154,11 @@ func BackupDatabase(disableCompression bool) {
} }
utils.Done("Database has been backed up") utils.Done("Database has been backed up")
//Delete old backup
if prune {
deleteOldBackup(keepLast)
}
} }
historyFile, err := os.OpenFile(fmt.Sprintf("%s/history.txt", storagePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) historyFile, err := os.OpenFile(fmt.Sprintf("%s/history.txt", storagePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -165,8 +173,38 @@ func BackupDatabase(disableCompression bool) {
} }
func s3Backup(disableCompression bool, s3Path string) { func s3Backup(disableCompression bool, s3Path string, prune bool, keepLast int) {
// Backup Database to S3 storage // Backup Database to S3 storage
MountS3Storage(s3Path) MountS3Storage(s3Path)
BackupDatabase(disableCompression) BackupDatabase(disableCompression, prune, keepLast)
}
func deleteOldBackup(keepLast int) {
utils.Info("Deleting old backups...")
storagePath = os.Getenv("STORAGE_PATH")
// Define the directory path
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 {
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)
}
}
return nil
})
if err != nil {
utils.Fatal("Error walking through directory: ", err)
}
} }