Skip to content

Commit

Permalink
add windows task scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jul 1, 2020
1 parent 2a03874 commit 05d0308
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 10 deletions.
17 changes: 16 additions & 1 deletion commands.go
Expand Up @@ -63,6 +63,13 @@ var (
needConfiguration: true,
hide: false,
},
{
name: "unschedule",
description: "remove a scheduled backup",
action: removeSchedule,
needConfiguration: true,
hide: false,
},
}
)

Expand Down Expand Up @@ -199,7 +206,15 @@ func createSchedule(c *config.Config, flags commandLineFlags, args []string) err

err = schedule.CreateJob(flags.config, profile)
if err == nil {
clog.Info("job created!")
clog.Info("scheduled job created")
}
return err
}

func removeSchedule(c *config.Config, flags commandLineFlags, args []string) error {
err := schedule.RemoveJob(flags.name)
if err == nil {
clog.Info("scheduled job removed")
}
return err
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/adrg/xdg v0.2.1
github.com/blang/semver v3.5.1+incompatible
github.com/capnspacehook/taskmaster v0.0.0-20190802050140-eebf732b5748
github.com/fatih/color v1.9.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.4.2 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Expand Up @@ -28,6 +28,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/capnspacehook/taskmaster v0.0.0-20190802050140-eebf732b5748 h1:ZG6TuKng0DDQ4J0bhDkhZ8hZg2k2q/HhKYuY+kSFOsg=
github.com/capnspacehook/taskmaster v0.0.0-20190802050140-eebf732b5748/go.mod h1:8b7ONZnkt8S45tG/OTFYeG9Xm0orQfEM9iQXq5I9v+M=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -57,6 +59,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -208,6 +212,10 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rhysd/go-github-selfupdate v1.2.2 h1:G+mNzkc1wEtpmM6sFS/Ghkeq+ad4Yp6EZEHyp//wGEo=
github.com/rhysd/go-github-selfupdate v1.2.2/go.mod h1:khesvSyKcXDUxeySCedFh621iawCks0dS/QnHPcpCws=
github.com/rickb777/date v1.12.3 h1:wWZ/ZAQNgTA+TIzSZt72/4lNdIaJ4C+92s9UwncwR4c=
github.com/rickb777/date v1.12.3/go.mod h1:xP0eo/I5qmUt97yRGClHZfyLZ3ikMw6v6SU5MOGZTE0=
github.com/rickb777/plural v1.2.0 h1:5tvEc7UBCZ7l8h/2UeybSkt/uu1DQsZFOFdNevmUhlE=
github.com/rickb777/plural v1.2.0/go.mod h1:UdpyWFCGbo3mvK3f/PfZOAOrkjzJlYN/sD46XNWJ+Es=
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/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down
15 changes: 15 additions & 0 deletions schedule/schedule.go
@@ -0,0 +1,15 @@
package schedule

import (
"path"
"path/filepath"
)

// absolutePathToBinary returns an absolute path to the resticprofile binary
func absolutePathToBinary(currentDir, binaryPath string) string {
binary := binaryPath
if !filepath.IsAbs(binary) {
binary = path.Join(currentDir, binary)
}
return binary
}
12 changes: 6 additions & 6 deletions schedule/schedule_darwin.go
Expand Up @@ -5,7 +5,6 @@ package schedule
import (
"os"
"path"
"path/filepath"
"strings"

"github.com/creativeprojects/resticprofile/config"
Expand Down Expand Up @@ -39,7 +38,7 @@ type CalendarInterval struct {
Minute int `plist:"Minute,omitempty"` // Minute of hour (0..59)
}

func CreateJob(config string, profile *config.Profile) error {
func CreateJob(configFile string, profile *config.Profile) error {
wd, err := os.Getwd()
if err != nil {
return err
Expand All @@ -49,10 +48,7 @@ func CreateJob(config string, profile *config.Profile) error {
return err
}

binary := os.Args[0]
if !filepath.IsAbs(binary) {
binary = path.Join(wd, binary)
}
binary := absolutePathToBinary(wd, os.Args[0])

name := "local.resticprofile." + strings.ToLower(profile.Name)
job := &LaunchJob{
Expand Down Expand Up @@ -89,3 +85,7 @@ func CreateJob(config string, profile *config.Profile) error {

return nil
}

func RemoveJob(profileName string) error {
return nil
}
6 changes: 5 additions & 1 deletion schedule/schedule_systemd.go
Expand Up @@ -4,6 +4,10 @@ package schedule

import "github.com/creativeprojects/resticprofile/config"

func CreateJob(config string, profile *config.Profile) error {
func CreateJob(configFile string, profile *config.Profile) error {
return nil
}

func RemoveJob(profileName string) error {
return nil
}
53 changes: 51 additions & 2 deletions schedule/schedule_windows.go
Expand Up @@ -2,8 +2,57 @@

package schedule

import "github.com/creativeprojects/resticprofile/config"
import (
"fmt"
"os"

func CreateJob(config string, profile *config.Profile) error {
"github.com/capnspacehook/taskmaster"
"github.com/creativeprojects/resticprofile/config"
)

const (
tasksPath = `\resticprofile backup\`
)

func CreateJob(configFile string, profile *config.Profile) error {
wd, err := os.Getwd()
if err != nil {
return err
}

taskService, err := taskmaster.Connect("", "", "", "")
if err != nil {
return err
}
defer taskService.Disconnect()

// office, err := taskService.GetRegisteredTask("\\Microsoft\\Office\\Office Automatic Updates 2.0")
// if err != nil {
// return err
// }
// fmt.Printf("%+v", office)

binary := absolutePathToBinary(wd, os.Args[0])

task := taskService.NewTaskDefinition()
task.AddExecAction(binary, fmt.Sprintf("--no-ansi --config %s --name %s backup", configFile, profile.Name), wd, "")
task.Principal.LogonType = taskmaster.TASK_LOGON_SERVICE_ACCOUNT
task.Principal.RunLevel = taskmaster.TASK_RUNLEVEL_HIGHEST
task.Principal.UserID = "SYSTEM"
task.RegistrationInfo.Description = fmt.Sprintf("restic backup using profile '%s' from '%s'", profile.Name, configFile)
_, _, err = taskService.CreateTask(tasksPath+profile.Name, task, true)
if err != nil {
return err
}
return nil
}

func RemoveJob(profileName string) error {
taskService, err := taskmaster.Connect("", "", "", "")
if err != nil {
return err
}
defer taskService.Disconnect()

return taskService.DeleteTask(tasksPath + profileName)
}

0 comments on commit 05d0308

Please sign in to comment.