Rename file and add bunch of things

This commit is contained in:
Rasmus Vester Thøgersen 2025-10-01 20:18:28 +02:00
parent e659f919c4
commit 8853bc8129
2 changed files with 114 additions and 38 deletions

114
init-repo.sh Executable file
View file

@ -0,0 +1,114 @@
#!/bin/bash
#
#This script will initialise the required files and directories for automatic backup of Docker data.
#
helpFunction()
{
echo ""
echo "Usage: $0 -i INPUT -c CONFIG_DIR -l LOG_DIR"
echo -e "\t-i Input folder to create a repository for."
echo -e "\t-c Location of config directory where the repository, password and uuid files are stored."
echo -e "\t-n Name of the Docker container. Required to stop the container and restart it before backing up."
exit 1 # Exit script after printing help
}
while getopts "i:c:n:f" opt
do
case "$opt" in
i ) INPUT="$OPTARG" ;;
c ) CONFIG_DIR="$OPTARG" ;;
n ) CONTAINER_NAME="$OPTARG" ;;
f ) FORCE=true ;;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
# Verify that required parameters are present
set -a && source .env && set +a
REQUIRED_PARAMS=(
INPUT
CONFIG_DIR
CONTAINER_NAME
EXTERNAL_STORAGE_URL
EXTERNAL_STORAGE_USER
EXTERNAL_STORAGE_PORT
EXTERNAL_STORAGE_ROOT_DIR
INTERNAL_STORAGE_ROOT_DIR
)
for variable in "${REQUIRED_PARAMS[@]}" ; do
if [ -z "${!variable+x}" ]; then
echo "$variable is not set. Make sure your .env file is complete, and you have passed in all required flags.";
exit;
fi
done
echo $FORCE
# Parse the input to create the SERVICE variable (lower case)
SERVICE=${INPUT##*/}
SERVICE=${SERVICE,,}
# Set the location of the internal repo
INTERNAL_REPO_LOCATION=$INTERNAL_STORAGE_ROOT_DIR/$SERVICE
# Generate random password and UUID
PASSWORD=$(tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 50)
UUID=$(uuidgen)
# Set filenames
REPO_FILE_PATH="$CONFIG_DIR/${SERVICE}_repository"
PWD_FILE_PATH="$CONFIG_DIR/${SERVICE}_password"
UUID_FILE_PATH="$CONFIG_DIR/${SERVICE}_uuid"
CONTAINER_NAME_FILE_PATH="$CONFIG_DIR/${SERVICE}_container_name"
# Make the config directory if it does not already exist
if [ ! -d "$CONFIG_DIR" ]; then
mkdir $CONFIG_DIR
fi
# Run the code only if the config files for the service does not already exist, or if force overwrite is specified
if [[ ! -f "$CONFIG_DIR/${SERVICE}_repository" ]] || [[ $FORCE == true ]]; then
# Prompt user if they are sure whether to force overwrite
if [[ $FORCE == true ]]; then
while true; do
read -p "The config for ${INPUT} already exists, but you specified to force overwrite. This will also delete what is already in the restic repository and recreate an empty repo. Are you sure? [y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Exiting without overwriting..."; exit;;
* ) echo "Please anser yes or no."
esac
done
fi
# Write the config files
echo $INTERNAL_REPO_LOCATION > $REPO_FILE_PATH
echo $PASSWORD > $PWD_FILE_PATH
echo $UUID > $UUID_FILE_PATH
echo $CONTAINER_NAME > $CONTAINER_NAME_FILE_PATH
echo "Created required backup directories for $INPUT"
echo "Password: $PASSWORD"
echo "UUID: $UUID"
echo "Make sure to note this down in your password manager to be able to decrypt the repositories, and to map the repositories on the external storage"
# Make the internal restic repository
if [ -d "$INTERNAL_REPO_LOCATION" ]; then
cp -r $INTERNAL_REPO_LOCATION ${INTERNAL_REPO_LOCATION}_old_$(date +%Y%m%d_%H%M%S)
rm -r $INTERNAL_REPO_LOCATION
fi
restic init --repository-file $REPO_FILE_PATH -p $PWD_FILE_PATH
else
echo "Repository config already exists for the service you specified. You can override this by passing the -f flag."
fi

View file

@ -1,38 +0,0 @@
#!/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 to create a repository for."
echo -e "\t-c Location of config directory where the repository, password and uuid files are stored."
exit 1 # Exit script after printing help
}
while getopts "i:c:l:" opt
do
case "$opt" in
i ) INPUT="$OPTARG" ;;
c ) CONFIG_DIR="$OPTARG" ;;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
SERVICE=${INPUT##*/}
SERVICE=${SERVICE,,}
PASSWORD=$(tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 50)
UUID=$(uuidgen)
echo $PASSWORD > $CONFIG_DIR/${SERVICE}_password
echo $UUID > $CONFIG_DIR/${SERVICE}_uuid
echo "Created required backup directories for $INPUT"
echo "Password: $PASSWORD"
echo "UUID: $UUID"
echo "Make sure to note this down in your password manager to be able to decrypt the repositories, and to map the repositories on the external storage"