Merge pull request #78 from erikw/windows

Add Windows support with TaskScheduler
This commit is contained in:
Erik Westrup
2022-02-12 20:46:39 +01:00
committed by GitHub
6 changed files with 79 additions and 2 deletions
+2
View File
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added
- Windows support with native ScheduledTask! New target `$ make install-schedtask` for Windows users.
## [5.3.1] - 2022-02-12 ## [5.3.1] - 2022-02-12
### Fixed ### Fixed
+10 -1
View File
@@ -18,7 +18,8 @@
.PHONY: help clean uninstall \ .PHONY: help clean uninstall \
install-systemd install-cron \ install-systemd install-cron \
install-targets-script install-targets-conf install-targets-systemd \ install-targets-script install-targets-conf install-targets-systemd \
install-targets-cron \ install-targets-cron install-targets-launchagent \
install-targets-schedtask \
activate-launchagent deactivate-launchagent activate-launchagent deactivate-launchagent
#### Macros ################################################################### #### Macros ###################################################################
@@ -69,6 +70,7 @@ SRCS_CONF = $(wildcard $(DIR_CONF)/*)
SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*) SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*)
SRCS_CRON = $(wildcard $(DIR_CRON)/*) SRCS_CRON = $(wildcard $(DIR_CRON)/*)
SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*)
SRCS_SCHEDTASK = install_restic_scheduledtask.ps1
# Local build directory. Sources will be copied here, # Local build directory. Sources will be copied here,
# modified and then installed from this directory. # 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_SYSTEMD = $(addprefix $(BUILD_DIR)/, $(SRCS_SYSTEMD))
BUILD_SRCS_CRON = $(addprefix $(BUILD_DIR)/, $(SRCS_CRON)) BUILD_SRCS_CRON = $(addprefix $(BUILD_DIR)/, $(SRCS_CRON))
BUILD_SRCS_LAUNCHAGENT = $(addprefix $(BUILD_DIR)/, $(SRCS_LAUNCHAGENT)) BUILD_SRCS_LAUNCHAGENT = $(addprefix $(BUILD_DIR)/, $(SRCS_LAUNCHAGENT))
BUILD_SRCS_SCHEDTASK = $(addprefix $(BUILD_DIR)/, $(SRCS_SCHEDTASK))
# Destination directories # Destination directories
DEST_DIR_SCRIPT = $(PREFIX)/$(DIR_SCRIPT) 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-launchagent: install-targets-script install-targets-conf \
install-targets-launchagent 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, # Install targets. Prereq build sources as well,
# so that build dir is re-created if deleted. # so that build dir is re-created if deleted.
install-targets-script: $(DEST_TARGS_SCRIPT) $(BUILD_SRCS_SCRIPT) 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-cron: $(DEST_TARGS_CRON) $(BUILD_SRCS_CRON)
install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) \ install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) \
$(BUILD_SRCS_LAUNCHAGENT) $(DEST_DIR_MAC_LOG) $(BUILD_SRCS_LAUNCHAGENT) $(DEST_DIR_MAC_LOG)
install-targets-schedtask: $(BUILD_SRCS_SCHEDTASK)
./$<
# Copies sources to build directory & replace "$INSTALL_PREFIX". # Copies sources to build directory & replace "$INSTALL_PREFIX".
$(BUILD_DIR)/% : % $(BUILD_DIR)/% : %
+43
View File
@@ -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 Depending on your system, the setup will look different. Choose one of
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/linux.svg" /> [Linux + Systemd](#setup-linux-systemd) * <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/linux.svg" /> [Linux + Systemd](#setup-linux-systemd)
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/apple.svg" /> [macOS + LaunchAgent](#setup-macos-launchagent) * <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/apple.svg" /> [macOS + LaunchAgent](#setup-macos-launchagent)
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/windows.svg" /> [Windows + ScheduledTask](#setup-windows-scheduledtask)
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/clockify.svg" /> [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. * <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/clockify.svg" /> [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS.
## Setup Linux Systemd ## Setup Linux Systemd
@@ -307,6 +308,48 @@ $ 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. 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)
```
1. Install scripts, conf and the ScheduledTask
```console
git-bash$ make install-schedtask
```
1. Edit configs and initialize repo according to *TL;DR* section above
```console
git-bash$ vim /etc/restic/*
git-bash$ source /etc/restic/default.env.sh
git-bash$ restic init
git-bash$ restic_backup.sh
```
Note that you should use cygwin/git-bash paths e.g. in `default.env.sh` you can have
```bash
export RESTIC_BACKUP_PATHS='/c/Users/<username>/My Documents'
```
1. Inspect the installed tasks and make a test run
1. Open the app "Task Scheduler" (`taskschd.msc`)
1. Go to the local "Task Scheduler Library"
1. Right click on one of the newly installed tasks e.g. `restic_backup` and click "run".
- If the tasks are not there, maybe you opended it up before `make install-schedtask`: just close and start it again to refresh.
1. Now a git-bash window should open running `restic_backup.sh`, and the next time the configured schedule hits!
1. With `taskschd.msc` you can easily start, stop, delete and configure the scheduled tasks to your liking.
After installing, you can control your backups through `tasksched.msc`:
<a href="img/tasksched.png" title="Windows Task Scheduler"><img src="img/tasksched.png" width="512" alt="Windows Task Schedulder"></a>
## Setup Cron ## Setup Cron
If you want to run an all-classic cron job instead, do like this: If you want to run an all-classic cron job instead, do like this:
+5 -1
View File
@@ -57,6 +57,10 @@ for backup_path in "${backup_paths[@]}"; do
fi fi
done done
# --one-file-system is not supportd on Windows (=msys).
FS_ARG=
test "$OSTYPE" = msys || FS_ARG=--one-file-system
# NOTE start all commands in background and wait for them to finish. # NOTE start all commands in background and wait for them to finish.
# Reason: bash ignores any signals while child process is executing and thus the trap exit hook is not triggered. # Reason: bash ignores any signals while child process is executing and thus the trap exit hook is not triggered.
# However if put in subprocesses, wait(1) waits until the process finishes OR signal is received. # However if put in subprocesses, wait(1) waits until the process finishes OR signal is received.
@@ -72,7 +76,7 @@ wait $!
# --tag lets us reference these backups later when doing restic-forget. # --tag lets us reference these backups later when doing restic-forget.
restic backup \ restic backup \
--verbose="$RESTIC_VERBOSITY_LEVEL" \ --verbose="$RESTIC_VERBOSITY_LEVEL" \
--one-file-system \ $FS_ARG \
--tag "$RESTIC_BACKUP_TAG" \ --tag "$RESTIC_BACKUP_TAG" \
--option b2.connections="$B2_CONNECTIONS" \ --option b2.connections="$B2_CONNECTIONS" \
"${exclusion_args[@]}" \ "${exclusion_args[@]}" \
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env pwsh
# Install restic scheduled tasks.
# Test run the installed actions by
# 1. open the app "Task Scheduler" (taskschd.msc)
# 2. go to the local "Task Scheduler Library"
# 3. right click on the new tasks and click "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 "source $INSTALL_PREFIX/etc/restic/default.env.sh && $INSTALL_PREFIX/bin/restic_backup.sh"'
$trigger = New-ScheduledTaskTrigger -Daily -At 7pm
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 "source $INSTALL_PREFIX/etc/restic/default.env.sh && /INSTALL_PREFIX/bin/restic_check.sh"'
$trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 4 -DaysOfWeek Sunday -At 8pm -RandomDelay 128
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "restic_check" -Description "Check B2 backups with restic."