Make more functional and move more stuff to .env

This commit is contained in:
rasmus 2025-11-11 22:15:58 +01:00
parent 8312e5be27
commit f1dff7e060

View file

@ -2,30 +2,31 @@
# #
#This script will automatically make an encrypted backup to a restic repository as specified with --source #This script will automatically make an encrypted backup to a restic repository as specified with --source
# #
#
#
helpFunction() helpFunction()
{ {
echo "" echo ""
echo "Usage: $0 -i INPUT -c CONFIG_DIR -l LOG_DIR" 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-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 "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)" echo -e "If you pass -i ~/MyServices/SeRvIce1, then the directory specified in your CONFIG_DIR must contain files service1_repository, service1_password and service1_uuid (note lower case)"
exit 1 # Exit script after printing help exit 1 # Exit script after printing help
} }
while getopts "i:c:l:" opt while getopts "i:" opt
do do
case "$opt" in case "$opt" in
i ) INPUT="$OPTARG" ;; i ) INPUT="$OPTARG" ;;
c ) CONFIG_DIR="$OPTARG" ;;
l ) LOG_DIR="$OPTARG" ;;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac esac
done done
# Print helpFunction in case parameters are empty # Print helpFunction in case parameters are empty
if [ -z "$INPUT" ] || [ -z "$CONFIG_DIR" ] || [ -z "$LOG_DIR" ] if [ -z "$INPUT" ] || [ -z "$CONFIG_DIR" ] || [ -z "$LOG_DIR" ] || [ -z "$EXTERNAL_STORAGE_USER" ] || [ -z "$EXTERNAL_STORAGE_URL" ] || [ -z "$EXTERNAL_STORAGE_PORT" ] || [ -z "$EXTERNAL_STORAGE_ROOT_DIR" ] || [ -z "$INTERNAL_STORAGE_ROOT_DIR" ]
then then
echo "Some or all of the parameters are empty"; echo "Some or all of the parameters are empty";
helpFunction helpFunction
@ -33,8 +34,6 @@ fi
# Begin script in case all parameters are correct # Begin script in case all parameters are correct
# Set environment variables
set -a && source .env && set +a
SERVICE=${INPUT##*/} SERVICE=${INPUT##*/}
SERVICE=${SERVICE,,} SERVICE=${SERVICE,,}
@ -49,12 +48,23 @@ UUID_FILE="${CONFIG_DIR}/${SERVICE}_uuid"
DOCKER_CONTAINER_FILE="${CONFIG_DIR}/${SERVICE}_container_name" DOCKER_CONTAINER_FILE="${CONFIG_DIR}/${SERVICE}_container_name"
echo -e "--- START OF BACKUP FOR ${SERVICE^^} - ${DATE} ---\n\n" > $LOG_FILE echo -e "---== START OF BACKUP FOR ${SERVICE^^} - ${DATE} ==---\n\n" > $LOG_FILE
# Stop Docker container # Stop Docker container
echo -e "\n\n Shutting down container with container name $(<$DOCKER_CONTAINER_FILE)" >> $LOG_FILE echo -e "### Shutting down container with container name $(<$DOCKER_CONTAINER_FILE)" >> $LOG_FILE
docker container stop $(<$DOCKER_CONTAINER_FILE) >> $LOG_FILE docker container stop $(<$DOCKER_CONTAINER_FILE) >> $LOG_FILE
TRIES=0
while docker ps --filter "name=$(<$DOCKER_CONTAINER_FILE)" --format '{{.ID}}' | grep -q .; do
echo "Waiting for container to stop"
sleep 0.5
((TRIES++))
if [ "$TRIES" -gt 10 ]; then
echo -e "Encountered an error while trying to stop the container. It never stopped..." >> $LOG_FILE
exit 1
fi
done
restic backup $INPUT --verbose --repository-file $REPOSITORY_FILE --password-file $PASSWORD_FILE >> $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 echo -e "\n\n Current snapshots:\n\n" >> $LOG_FILE
@ -64,7 +74,7 @@ echo -e "\n\n Health check:\n\n" >> $LOG_FILE
restic check --verbose --repository-file $REPOSITORY_FILE --password-file $PASSWORD_FILE >> $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 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 rsync -rahv --partial --delete-after -e "ssh -p ${EXTERNAL_STORAGE_PORT}" "$(<$REPOSITORY_FILE)/" $EXTERNAL_STORAGE_USER@$EXTERNAL_STORAGE_URL:$EXTERNAL_STORAGE_ROOT_DIR/"$(<$UUID_FILE)" >> $LOG_FILE
# Start Docker container again # Start Docker container again