Add maintain log in scheduled mode

This commit is contained in:
2024-07-28 16:59:26 +02:00
parent 117fdbde82
commit 0aa38eec20
7 changed files with 54 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ package pkg
import (
"fmt"
"github.com/hpcloud/tail"
"github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra"
"log"
@@ -58,22 +59,41 @@ func scheduledMode() {
fmt.Println(" Starting PostgreSQL Bkup... ")
fmt.Println("***********************************")
utils.Info("Running in Scheduled mode")
utils.Info("Log file in /var/log/pg-bkup.log")
utils.Info("Execution period ", os.Getenv("SCHEDULE_PERIOD"))
//Test database connexion
utils.TestDatabaseConnection()
//Test database connexion
utils.TestDatabaseConnection()
utils.Info("Creating backup job...")
CreateCrontabScript(disableCompression, storage)
//Start Supervisor
supervisordCmd := exec.Command("supervisord", "-c", "/etc/supervisor/supervisord.conf")
if err := supervisordCmd.Run(); err != nil {
utils.Fatalf("Error starting supervisord: %v\n", err)
supervisorConfig := "/etc/supervisor/supervisord.conf"
// Start Supervisor
cmd := exec.Command("supervisord", "-c", supervisorConfig)
err := cmd.Start()
if err != nil {
utils.Fatal("Failed to start supervisord: %v", err)
}
utils.Info("Starting backup job...")
defer func() {
if err := cmd.Process.Kill(); err != nil {
utils.Info("Failed to kill supervisord process: %v", err)
} else {
utils.Info("Supervisor stopped.")
}
}()
if _, err := os.Stat(cronLogFile); os.IsNotExist(err) {
utils.Fatal("Log file %s does not exist.", cronLogFile)
}
t, err := tail.TailFile(cronLogFile, tail.Config{Follow: true})
if err != nil {
utils.Fatalf("Failed to tail file: %v", err)
}
// Read and print new lines from the log file
for line := range t.Lines {
fmt.Println(line.Text)
}
}
@@ -86,6 +106,7 @@ func BackupDatabase(disableCompression bool, prune bool, keepLast int) {
dbPort = os.Getenv("DB_PORT")
storagePath = os.Getenv("STORAGE_PATH")
utils.Info("Starting database backup...")
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" {
utils.Fatal("Please make sure all required environment variables for database are set")
} else {

View File

@@ -11,9 +11,6 @@ import (
"os/exec"
)
const cronLogFile = "/var/log/pg-bkup.log"
const backupCronFile = "/usr/local/bin/backup_cron.sh"
func CreateCrontabScript(disableCompression bool, storage string) {
//task := "/usr/local/bin/backup_cron.sh"
touchCmd := exec.Command("touch", backupCronFile)
@@ -54,6 +51,11 @@ bkup backup --dbname %s --port %s %v
}
touchLogCmd := exec.Command("touch", cronLogFile)
if err := touchLogCmd.Run(); err != nil {
utils.Fatalf("Error creating file %s: %v\n", cronLogFile, err)
}
cronJob := "/etc/cron.d/backup_cron"
touchCronCmd := exec.Command("touch", cronJob)
if err := touchCronCmd.Run(); err != nil {

View File

@@ -2,6 +2,8 @@ package pkg
const s3MountPath string = "/s3mnt"
const s3fsPasswdFile string = "/etc/passwd-s3fs"
const cronLogFile = "/var/log/pg-bkup.log"
const backupCronFile = "/usr/local/bin/backup_cron.sh"
var (
storage = "local"