2017-07-06 13:03:44 +02:00
|
|
|
package cmd
|
2017-07-01 20:28:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-06 13:03:44 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zrepl/zrepl/jobrun"
|
2017-07-01 20:28:46 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2017-07-06 15:36:53 +02:00
|
|
|
"os"
|
2017-07-06 13:03:44 +02:00
|
|
|
"sync"
|
2017-07-01 20:28:46 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-07-06 13:03:44 +02:00
|
|
|
var AutosnapCmd = &cobra.Command{
|
|
|
|
Use: "autosnap",
|
|
|
|
Short: "perform automatic snapshotting",
|
|
|
|
Run: cmdAutosnap,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(AutosnapCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAutosnap(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
runner.Start()
|
|
|
|
}()
|
|
|
|
|
2017-07-06 15:36:53 +02:00
|
|
|
if len(args) < 1 {
|
|
|
|
log.Printf("must specify exactly one job as positional argument")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-07-06 13:03:44 +02:00
|
|
|
|
2017-07-06 15:36:53 +02:00
|
|
|
snap, ok := conf.Autosnaps[args[0]]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("could not find autosnap job: %s", args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-07-06 13:03:44 +02:00
|
|
|
|
2017-07-06 15:36:53 +02:00
|
|
|
job := jobrun.Job{
|
|
|
|
Name: snap.JobName,
|
|
|
|
RepeatStrategy: snap.Interval,
|
|
|
|
RunFunc: func(log jobrun.Logger) error {
|
|
|
|
log.Printf("doing autosnap: %v", snap)
|
|
|
|
ctx := AutosnapContext{snap}
|
|
|
|
return doAutosnap(ctx, log)
|
|
|
|
},
|
2017-07-06 13:03:44 +02:00
|
|
|
}
|
2017-07-06 15:36:53 +02:00
|
|
|
runner.AddJob(job)
|
2017-07-06 13:03:44 +02:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:28:46 +02:00
|
|
|
type AutosnapContext struct {
|
2017-07-06 15:36:53 +02:00
|
|
|
Autosnap *Autosnap
|
2017-07-01 20:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func doAutosnap(ctx AutosnapContext, log Logger) (err error) {
|
|
|
|
|
|
|
|
snap := ctx.Autosnap
|
|
|
|
|
|
|
|
filesystems, err := zfs.ZFSListMapping(snap.DatasetFilter)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot filter datasets: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
suffix := time.Now().In(time.UTC).Format("20060102_150405_000")
|
|
|
|
snapname := fmt.Sprintf("%s%s", snap.Prefix, suffix)
|
|
|
|
|
|
|
|
hadError := false
|
|
|
|
|
|
|
|
for _, fs := range filesystems { // optimization: use recursive snapshots / channel programs here
|
|
|
|
log.Printf("snapshotting filesystem %s@%s", fs, snapname)
|
|
|
|
err := zfs.ZFSSnapshot(fs, snapname, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error snapshotting %s: %s", fs, err)
|
|
|
|
hadError = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hadError {
|
|
|
|
err = fmt.Errorf("errors occurred during autosnap, check logs for details")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|