#!/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