2024-09-10 23:01:26 +02:00
|
|
|
// Package pkg /
|
|
|
|
|
/*****
|
|
|
|
|
@author Jonas Kaninda
|
|
|
|
|
@license MIT License <https://opensource.org/licenses/MIT>
|
|
|
|
|
@Copyright © 2024 Jonas Kaninda
|
|
|
|
|
**/
|
2024-08-03 16:03:17 +02:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
import (
|
2024-08-30 19:58:12 +02:00
|
|
|
"bytes"
|
2024-08-03 16:03:17 +02:00
|
|
|
"fmt"
|
|
|
|
|
"github.com/jkaninda/mysql-bkup/utils"
|
|
|
|
|
"os"
|
2024-08-30 19:58:12 +02:00
|
|
|
"os/exec"
|
2024-08-03 16:03:17 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func copyToTmp(sourcePath string, backupFileName string) {
|
|
|
|
|
//Copy backup from storage to /tmp
|
|
|
|
|
err := utils.CopyFile(filepath.Join(sourcePath, backupFileName), filepath.Join(tmpPath, backupFileName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fatal(fmt.Sprintf("Error copying file %s %s", backupFileName, err))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func moveToBackup(backupFileName string, destinationPath string) {
|
|
|
|
|
//Copy backup from tmp folder to storage destination
|
|
|
|
|
err := utils.CopyFile(filepath.Join(tmpPath, backupFileName), filepath.Join(destinationPath, backupFileName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fatal(fmt.Sprintf("Error copying file %s %s", backupFileName, err))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//Delete backup file from tmp folder
|
|
|
|
|
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error deleting file:", err)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
utils.Done("Database has been backed up and copied to %s", filepath.Join(destinationPath, backupFileName))
|
|
|
|
|
}
|
|
|
|
|
func deleteOldBackup(retentionDays int) {
|
|
|
|
|
utils.Info("Deleting old backups...")
|
|
|
|
|
storagePath = os.Getenv("STORAGE_PATH")
|
|
|
|
|
// Define the directory path
|
|
|
|
|
backupDir := storagePath + "/"
|
|
|
|
|
// Get current time
|
|
|
|
|
currentTime := time.Now()
|
|
|
|
|
// Delete file
|
|
|
|
|
deleteFile := func(filePath string) error {
|
|
|
|
|
err := os.Remove(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fatal(fmt.Sprintf("Error: %s", err))
|
|
|
|
|
} else {
|
|
|
|
|
utils.Done("File %s has been deleted successfully", filePath)
|
|
|
|
|
}
|
|
|
|
|
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 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(retentionDays) {
|
|
|
|
|
err := deleteFile(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fatal(fmt.Sprintf("Error: %s", err))
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-11 09:38:31 +02:00
|
|
|
utils.Done("Deleting old backups...done")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func deleteTemp() {
|
|
|
|
|
utils.Info("Deleting %s ...", tmpPath)
|
|
|
|
|
err := filepath.Walk(tmpPath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// Check if the current item is a file
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
|
// Delete the file
|
|
|
|
|
err = os.Remove(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Error("Error deleting files: %v", err)
|
|
|
|
|
} else {
|
|
|
|
|
utils.Info("Deleting %s ... done", tmpPath)
|
|
|
|
|
}
|
2024-08-03 16:03:17 +02:00
|
|
|
}
|
2024-08-30 19:58:12 +02:00
|
|
|
|
|
|
|
|
// TestDatabaseConnection tests the database connection
|
|
|
|
|
func testDatabaseConnection(db *dbConfig) {
|
2024-09-28 03:06:09 +02:00
|
|
|
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-30 19:58:12 +02:00
|
|
|
utils.Info("Connecting to %s database ...", db.dbName)
|
2024-09-28 02:25:42 +02:00
|
|
|
cmd := exec.Command("mysql", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit")
|
2024-08-30 19:58:12 +02:00
|
|
|
// Capture the output
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
cmd.Stderr = &out
|
2024-09-28 03:06:09 +02:00
|
|
|
err = cmd.Run()
|
2024-08-30 19:58:12 +02:00
|
|
|
if err != nil {
|
2024-09-11 08:03:16 +02:00
|
|
|
utils.Fatal("Error testing database connection: %v\nOutput: %s", err, out.String())
|
2024-08-30 19:58:12 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
utils.Info("Successfully connected to %s database", db.dbName)
|
|
|
|
|
|
|
|
|
|
}
|
2024-09-28 07:26:33 +02:00
|
|
|
func intro() {
|
|
|
|
|
utils.Info("Starting MySQL Backup...")
|
2024-09-29 07:35:27 +02:00
|
|
|
utils.Info("Copyright (c) 2024 Jonas Kaninda ")
|
2024-09-28 07:26:33 +02:00
|
|
|
}
|