From 50f4c2e14ddb207dbb803337a280d77a89d0c561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Vester=20Th=C3=B8gersen?= Date: Thu, 25 Sep 2025 22:44:48 +0200 Subject: [PATCH] Add initial version of backup script --- backup.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..a57a337 --- /dev/null +++ b/backup.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# +#This script will automatically make an encrypted backup to a restic repository as specified with --source +# +helpFunction() +{ + echo "" + echo "Usage: $0 -i INPUT -c CONFIG_DIR -l LOG_DIR" + echo -e "\t-i Input folder that should be backed up." + echo -e "\t-c Location of config directory containing repository, password and uuid files." + echo -e "\t-l Location of where the log should be outputted to." + echo -e "Example:" + echo -e "If you pass -i ~/MyServices/SeRvIce1, then the directory you pass as -c must contain files service1_repository, service1_password and service1_uuid (note lower case)" + exit 1 # Exit script after printing help +} + +while getopts "i:c:l:" opt +do + case "$opt" in + i ) INPUT="$OPTARG" ;; + c ) CONFIG_DIR="$OPTARG" ;; + l ) LOG_DIR="$OPTARG" ;; + ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent + esac +done + +# Print helpFunction in case parameters are empty +if [ -z "$INPUT" ] || [ -z "$CONFIG_DIR" ] || [ -z "$LOG_DIR" ] +then + echo "Some or all of the parameters are empty"; + helpFunction +fi + +# Begin script in case all parameters are correct + +# Set environment variables +set -a && source .env && set +a + +SERVICE=${INPUT##*/} +SERVICE=${SERVICE,,} +DATE=$(date +%Y-%m-%d_%H%M%S) +LOG_FILE="${LOG_DIR}/${SERVICE}_${DATE}.log" + + + +REPOSITORY_FILE="${CONFIG_DIR}/${SERVICE}_repository" +PASSWORD_FILE="${CONFIG_DIR}/${SERVICE}_password" +UUID_FILE="${CONFIG_DIR}/${SERVICE}_uuid" + +echo -e "--- START OF BACKUP FOR ${SERVICE^^} - ${DATE} ---\n\n" > $LOG_FILE +restic backup $INPUT --verbose --repository-file $REPOSITORY_FILE --password-file $PASSWORD_FILE >> $LOG_FILE + +echo -e "\n\n Current snapshots:\n\n" >> $LOG_FILE +restic snapshots --verbose --repository-file $REPOSITORY_FILE --password-file $PASSWORD_FILE >> $LOG_FILE + +echo -e "\n\n Health check:\n\n" >> $LOG_FILE +restic check --verbose --repository-file $REPOSITORY_FILE --password-file $PASSWORD_FILE >> $LOG_FILE + +echo -e "\n\n Move to external storage:\n\n" >> $LOG_FILE +rsync -rahPv --delete-after -e "ssh -p ${EXTERNAL_STORAGE_PORT}" "$(<$REPOSITORY_FILE)" $EXTERNAL_STORAGE_USER@$EXTERNAL_STORAGE_URL:$EXTERNAL_STORAGE_ROOT_DIR/"$(<$UUID_FILE)" >> $LOG_FILE