mirror of
https://kolaente.dev/konrad/docker-db-backup.git
synced 2026-01-10 03:00:08 +01:00
feat: add BACKUP_NO_CRON config to allow running the backup without cron schedule
This commit is contained in:
60
main.go
60
main.go
@@ -3,39 +3,49 @@ package main
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func runBackup() {
|
||||
c, err := getClient()
|
||||
if err != nil {
|
||||
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()
|
||||
_, err = cr.AddFunc(config.Schedule, func() {
|
||||
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.")
|
||||
})
|
||||
_, err := cr.AddFunc(config.Schedule, runBackup)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not create cron job: %s\n", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user