Skip to content

Commit

Permalink
cron
Browse files Browse the repository at this point in the history
  • Loading branch information
cupcakearmy committed Apr 11, 2021
1 parent 5d92b5b commit 8a1fe41
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
36 changes: 36 additions & 0 deletions cmd/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/cupcakearmy/autorestic/internal"
"github.com/spf13/cobra"
)

// cronCmd represents the cron command
var cronCmd = &cobra.Command{
Use: "cron",
Short: "Run cron job for automated backups",
Long: `Intended to be mainly triggered by an automated system like systemd or crontab. For each location checks if a cron backup is due and runs it.`,
Run: func(cmd *cobra.Command, args []string) {
err := internal.RunCron()
cobra.CheckErr(err)
},
}

func init() {
rootCmd.AddCommand(cronCmd)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/buger/goterm v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/robfig/cron v1.2.0
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
12 changes: 12 additions & 0 deletions internal/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package internal

func RunCron() error {
c := GetConfig()
for _, l := range c.Locations {
err := l.RunCron()
if err != nil {
return err
}
}
return nil
}
27 changes: 27 additions & 0 deletions internal/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/robfig/cron"
)

type HookArray = []string
Expand Down Expand Up @@ -163,3 +167,26 @@ func (l Location) Restore(to, from string, force bool) error {
}
return nil
}

func (l Location) RunCron() error {
if l.Cron == "" {
return nil
}

schedule, err := cron.ParseStandard(l.Cron)
if err != nil {
return err
}
last := lock.GetCron("test")
fmt.Println(last)
next := schedule.Next(time.Unix(last, 0))
fmt.Println(next)
now := time.Now()
if now.After(next) {
fmt.Println("Running")
lock.SetCron("test", now.Unix())
} else {
fmt.Println("Not due yet")
}
return nil
}
16 changes: 13 additions & 3 deletions internal/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func getLock() *viper.Viper {
return lock
}

func set(locked bool) error {
func setLock(locked bool) error {
lock := getLock()
if locked {
running := lock.GetBool("running")
Expand All @@ -43,10 +43,20 @@ func set(locked bool) error {
return nil
}

func GetCron(location string) int64 {
lock := getLock()
return lock.GetInt64("cron." + location)
}

func SetCron(location string, value int64) {
lock.Set("cron."+location, value)
lock.WriteConfigAs(file)
}

func Lock() error {
return set(true)
return setLock(true)
}

func Unlock() error {
return set(false)
return setLock(false)
}

0 comments on commit 8a1fe41

Please sign in to comment.