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

18
main.go
View File

@ -3,16 +3,15 @@ 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)
} }
cr := cron.New()
_, err = cr.AddFunc(config.Schedule, func() {
updateFullBackupPath() updateFullBackupPath()
containers, err := getContainers(c) containers, err := getContainers(c)
@ -35,7 +34,18 @@ func main() {
} }
log.Println("Done.") 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()
_, err := cr.AddFunc(config.Schedule, runBackup)
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)
} }