feat: add BACKUP_NO_CRON config to allow running the backup without cron schedule

This commit is contained in:
kolaente 2024-02-13 16:41:27 +01:00
parent 5982f1ef07
commit 49510a6179
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 41 additions and 25 deletions

View File

@ -60,6 +60,12 @@ Check out [crontab.dev](https://crontab.dev/) for a nice explanation of the sche
Default: `0 */6 * * *` (every 6 hours) Default: `0 */6 * * *` (every 6 hours)
### `BACKUP_NO_CRON`
If provided, runs the backup only once without a cron schedule. This is useful for one-off backups of testing if the configuration works.
Default: `false`
### `BACKUP_MAX` ### `BACKUP_MAX`
How many backups to keep. If more backups are stored in the backup folder, the oldest one will be removed until there How many backups to keep. If more backups are stored in the backup folder, the oldest one will be removed until there

60
main.go
View File

@ -3,39 +3,49 @@ package main
import ( import (
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"log" "log"
"os"
) )
func main() { func runBackup() {
c, err := getClient() c, err := getClient()
if err != nil { if err != nil {
log.Fatalf("Could not create client: %s", err) log.Fatalf("Could not create client: %s", err)
} }
updateFullBackupPath()
containers, err := getContainers(c)
if err != nil {
log.Fatalf("Could not get containers: %s", err)
}
storeContainers(c, containers)
err = cleanupOldBackups()
if err != nil {
log.Fatalf("Could not clean old backups: %s", err)
}
dumpAllDatabases(c)
err = callWebhook()
if err != nil {
log.Fatalf("Could not call completion webhook: %s", err)
}
log.Println("Done.")
}
func main() {
noCron, has := os.LookupEnv("BACKUP_NO_CRON")
if has && (noCron == "true" || noCron == "1") {
log.Println("BACKUP_NO_CRON set, running backup once, then exiting")
runBackup()
return
}
cr := cron.New() cr := cron.New()
_, err = cr.AddFunc(config.Schedule, func() { _, err := cr.AddFunc(config.Schedule, runBackup)
updateFullBackupPath()
containers, err := getContainers(c)
if err != nil {
log.Fatalf("Could not get containers: %s", err)
}
storeContainers(c, containers)
err = cleanupOldBackups()
if err != nil {
log.Fatalf("Could not clean old backups: %s", err)
}
dumpAllDatabases(c)
err = callWebhook()
if err != nil {
log.Fatalf("Could not call completion webhook: %s", err)
}
log.Println("Done.")
})
if err != nil { if err != nil {
log.Fatalf("Could not create cron job: %s\n", err) log.Fatalf("Could not create cron job: %s\n", err)
} }