Created
April 2, 2024 22:48
-
-
Save marcelschmidtdev/fe859680d91beed5beee1f263301e7bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script opens a Wireguard VPN and is closing it after a Hyperbackup task has finished. | |
# It was tested with only one active backup task on a single remote target and Hyperbackup 4.1.1-3758. | |
# | |
# How it works: | |
# * Setup a new scheduled task in the control panel executing this script with correct parameters | |
# * Make sure it runs BEFORE your Hyperbackup tasks are started | |
# * Note: Verify if it's working. Unfortunately Hyperbackup officially doesn't support any hooks, | |
# therefore this is relying on parsing the log file with regex. It can break any time with a | |
# future update of Hyperbackup. | |
LOG_FILE=/var/packages/HyperBackup/var/log/hyperbackup.log | |
print_help () { | |
echo "Usage: $0 [wg_config] (-backup | -integrity)" | |
} | |
if [ -z "$1" ]; then | |
print_help | |
exit 1 | |
fi | |
case $2 in | |
-backup) | |
task='backup' | |
regex='\[BackupTaskFinished\]' | |
;; | |
-integrity) | |
task='integrity check' | |
regex='from state \[.+\] to state \[Backupable\] with action \[Finish\]' | |
;; | |
*) | |
print_help | |
exit 1 | |
;; | |
esac | |
echo Connecting VPN... | |
wg-quick up $1 | |
echo "Waiting for $task to finish..." | |
start_ts=$(date +%s) | |
while : | |
do | |
# Reverse cat and return the first grep match | |
line=$(tac $LOG_FILE | grep -E "$regex" | head -1) | |
# Check if string is not empty | |
if [ -n "$line" ]; then | |
array=($line) | |
ts=$(date -d ${array[0]} +%s) | |
# Check if found timestamp is greater than timestamp of starting this script | |
if [ $ts -gt $start_ts ]; then | |
echo "${task^} finished. Disconnect VPN..." | |
wg-quick down $1 | |
break | |
fi | |
fi | |
sleep 60 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment