From 9ff51d691ab910adea819fb75a72fb1bf820a760 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Sat, 12 Feb 2022 19:11:47 +0100 Subject: [PATCH] WIP make target for scheduledtask --- Makefile | 9 +++++++++ README.md | 17 +++++++++++++++++ install_restic_scheduledtask.ps1 | 17 +++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 install_restic_scheduledtask.ps1 diff --git a/Makefile b/Makefile index b52eba0..8b7f2fe 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ install-systemd install-cron \ install-targets-script install-targets-conf install-targets-systemd \ install-targets-cron install-targets-launchagent \ + install-targets-schedtask \ activate-launchagent deactivate-launchagent #### Macros ################################################################### @@ -69,6 +70,7 @@ SRCS_CONF = $(wildcard $(DIR_CONF)/*) SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*) SRCS_CRON = $(wildcard $(DIR_CRON)/*) SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) +SRCS_SCHEDTASK = install_restic_scheduledtask.ps1 # Local build directory. Sources will be copied here, # modified and then installed from this directory. @@ -85,6 +87,7 @@ BUILD_SRCS_CONF = $(addprefix $(BUILD_DIR)/, $(SRCS_CONF)) BUILD_SRCS_SYSTEMD = $(addprefix $(BUILD_DIR)/, $(SRCS_SYSTEMD)) BUILD_SRCS_CRON = $(addprefix $(BUILD_DIR)/, $(SRCS_CRON)) BUILD_SRCS_LAUNCHAGENT = $(addprefix $(BUILD_DIR)/, $(SRCS_LAUNCHAGENT)) +BUILD_SRCS_SCHEDTASK = $(addprefix $(BUILD_DIR)/, $(SRCS_SCHEDTASK)) # Destination directories DEST_DIR_SCRIPT = $(PREFIX)/$(DIR_SCRIPT) @@ -137,6 +140,10 @@ install-cron: install-targets-script install-targets-conf install-targets-cron install-launchagent: install-targets-script install-targets-conf \ install-targets-launchagent +# target: install-schedtask - Install Windows ScheduledTask +install-schedtask: install-targets-script install-targets-conf \ + install-targets-schedtask + # Install targets. Prereq build sources as well, # so that build dir is re-created if deleted. install-targets-script: $(DEST_TARGS_SCRIPT) $(BUILD_SRCS_SCRIPT) @@ -145,6 +152,8 @@ install-targets-systemd: $(DEST_TARGS_SYSTEMD) $(BUILD_SRCS_SYSTEMD) install-targets-cron: $(DEST_TARGS_CRON) $(BUILD_SRCS_CRON) install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) \ $(BUILD_SRCS_LAUNCHAGENT) $(DEST_DIR_MAC_LOG) +install-targets-schedtask: $(BUILD_SRCS_SCHEDTASK) + ./$< # Copies sources to build directory & replace "$INSTALL_PREFIX". $(BUILD_DIR)/% : % diff --git a/README.md b/README.md index 2cead3b..8b01d7c 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Tip: use the Section icon in the top left of this document to navigate the secti Depending on your system, the setup will look different. Choose one of * [Linux + Systemd](#setup-linux-systemd) * [macOS + LaunchAgent](#setup-macos-launchagent) +* [Windows + ScheduledTask](#setup-windows-scheduledtask) * [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. ## Setup Linux Systemd @@ -307,6 +308,22 @@ $ launchctl bootout gui/$UID/com.github.erikw.restic-automatic-backup If you updated the `.plist` file, you need to issue the `bootout` followed by `bootrstrap` and `enable` sub-commands of `launchctl`. This will guarantee that the file is properly reloaded. + +## Setup Windows ScheduledTask +This is one of may ways you can get restic and this backup script working on Windows: +1. Install [scoop](https://scoop.sh/) +1. Install dependencies from a PowerShell with administrator privileges: + ```console + powershell> scoop install restic make git + ``` +1. In a non-privileged PowerShell, start git-bash and clone this repo + ```console + powershell> git-bash + git-bash$ mkdir ~/src && cd ~/src/ + git-bash$ git clone https://github.com/erikw/restic-systemd-automatic-backup.git && cd $(basename "$_" .git) + ``` + + ## Setup Cron If you want to run an all-classic cron job instead, do like this: diff --git a/install_restic_scheduledtask.ps1 b/install_restic_scheduledtask.ps1 new file mode 100644 index 0000000..dcc7d07 --- /dev/null +++ b/install_restic_scheduledtask.ps1 @@ -0,0 +1,17 @@ +# Install restic scheduled tasks. +# Test run the installed actions by opening the app "Task Scheduler" and go to "Task Scheduler Library" and right clicking on the new tasks > run. +# Reference: https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/ +# Reference: https://www.davidjnice.com/cygwin_scheduled_tasks.html + + +# Install restic_backup.sh +$action = New-ScheduledTaskAction -Execute 'C:\Program Files\Git\git-bash.exe' -Argument '-l -c "/c$INSTALL_PREFIX/bin/restic_backup.sh"' +$trigger = New-ScheduledTaskTrigger -Daily -At 8pm +Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "restic_backup" -Description "Daily backup to B2 with restic." + + + +# Install restic_check.sh +$action = New-ScheduledTaskAction -Execute 'C:\Program Files\Git\git-bash.exe' -Argument '-l -c "/c$INSTALL_PREFIX/bin/restic_check.sh"' +$trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 4 -DaysOfWeek Sunday -At 7pm -RandomDelay 128 +Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "restic_check" -Description "Check B2 backups with restic."