Added cli parameters for automation

This commit is contained in:
Enrico Ludwig 2024-05-12 17:12:33 +02:00
parent 95cbee6fca
commit 0dbe3e8d48

View File

@ -304,6 +304,189 @@ function check_crontab {
# main script # main script
# Available arguments:
# -a, --auto: Enable automatic, non-interactive mode
# -d, --debug: Enable debug mode
# -v, --version: Show script version
# -h, --help: Show help message
# -n, --no-upgrade: Skip package upgrades
# -f, --first-backup: Run the first backup after setup
#
# -R, --repo: Set the backup repository path
# -E, --encryption: Set the encryption method
# -P, --passphrase: Set the backup passphrase
# -D, --dirs: Set the backup directories (separated by space as a string, for example: --dirs "/etc /var/log /home")
# -L, --label: Set the backup label prefix
# -S, --schedule: Set the cron schedule
#
# -H, --host: Set the backup server hostname or IP address
# -P, --port: Set the backup server SSH port
# -U, --user: Set the backup server SSH user
# -W, --password: Set the backup server SSH password
# -K, --key-name: Set the SSH key name
# -T, --key-type: Set the SSH key type
# -F, --key-file: Set the SSH key file path
#
# Example usage:
# ./borgmatic_setup.sh -R "Backups/MyServer" \
# -E "none" -P "example" \
# -D "/etc /var/log /home" \
# -L "BACKUP-" \
# -S "0 3 * * *" \
# -H "192.168.1.200" \
# -P "22" \
# -U "root" \
# -W "12345678" \
# -K "borgmatic" \
# -T "ed25519" \
# -F "/root/.ssh/borgmatic_ed25519"
#
# or with long options:
# ./borgmatic_setup.sh --repo "Backups/MyServer" \
# --encryption "none" --passphrase "example" \
# --dirs "/etc /var/log /home" \
# --label "BACKUP-" \
# --schedule "0 3 * * *" \
# --host "192.168.1.200" \
# --port "22" \
# --user "root" \
# --password "12345678"
# --key-name "borgmatic" \
# --key-type "ed25519" \
# --key-file "/root/.ssh/borgmatic_ed25519"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-a|--auto)
AUTO=1
shift
;;
-d|--debug)
DEBUG=1
shift
;;
-v|--version)
echo "$SCRIPT_NAME v$VERSION"
exit 0
;;
-n|--no-upgrade)
NO_UPGRADE=1
shift
;;
-f|--first-backup)
FIRST_BACKUP=1
shift
;;
-h|--help)
inf "Usage: borgmatic_setup.sh [options]"
inf ""
inf "Options will override the default settings set in the script."
inf "If no options are provided, the script will run in interactive mode."
inf "All options are optional."
inf ""
inf "\e[1m\e[31mImportant: The script must be run as root to work properly.\e[0m"
inf ""
inf "Options:"
inf " -a, --auto: Enable automatic, non-interactive mode"
inf " -d, --debug: Enable debug mode"
inf " -v, --version: Show script version"
inf " -h, --help: Show help message"
inf ""
inf " -R, --repo: Set the backup repository path"
inf " -E, --encryption: Set the encryption method"
inf " -P, --passphrase: Set the backup passphrase"
inf " -D, --dirs: Set the backup directories (separated by space as a string, for example: --dirs \"/etc /var/log /home\")"
inf " -L, --label: Set the backup label prefix"
inf " -S, --schedule: Set the cron schedule"
inf ""
inf " -H, --host: Set the backup server hostname or IP address"
inf " -P, --port: Set the backup server SSH port"
inf " -U, --user: Set the backup server SSH user"
inf " -W, --password: Set the backup server SSH password"
inf " -K, --key-name: Set the SSH key name"
inf " -T, --key-type: Set the SSH key type"
inf " -F, --key-file: Set the SSH key file path"
inf ""
inf "Example usage:"
inf "./borgmatic_setup.sh --dirs \"/etc /var/log /home\" --host \"192.168.1.200\" --encryption \"none\""
inf ""
inf "or for an encrypted backup:"
inf "./borgmatic_setup.sh --dirs \"/etc /var/log /home\" --host \"192.168.1.200\" --encryption \"repokey\" --passphrase \"example\""
exit 0
;;
-R|--repo)
BACKUP_REPO="$2"
shift
shift
;;
-E|--encryption)
BACKUP_ENCRYPTION="$2"
shift
shift
;;
-P|--passphrase)
BACKUP_PASSPHRASE="$2"
shift
shift
;;
-D|--dirs)
BACKUP_DIRS="$2"
shift
shift
;;
-L|--label)
BACKUP_LABEL_PREFIX="$2"
shift
shift
;;
-S|--schedule)
BACKUP_CRON_SCHEDULE="$2"
shift
shift
;;
-H|--host)
SSH_HOST="$2"
shift
shift
;;
-P|--port)
SSH_PORT="$2"
shift
shift
;;
-U|--user)
SSH_USER="$2"
shift
shift
;;
-W|--password)
SSH_PASSWORD="$2"
shift
shift
;;
-K|--key-name)
SSH_KEY_NAME="$2"
shift
shift
;;
-T|--key-type)
SSH_KEY_TYPE="$2"
shift
shift
;;
-F|--key-file)
SSH_KEY_FILE="$2"
shift
shift
;;
*)
shift
;;
esac
done
echo -e "\e[97m╭─────────────────────────────────────────────────────────────────────────────╮" echo -e "\e[97m╭─────────────────────────────────────────────────────────────────────────────╮"
echo -e "│ │" echo -e "│ │"
echo -e "│ \e[1m$SCRIPT_NAME\e[0m │" echo -e "│ \e[1m$SCRIPT_NAME\e[0m │"
@ -333,7 +516,8 @@ wrn "This script will install \e[1mborgbackup\e[0m, \e[1mpipx\e[0m"
wrn "and \e[1msshpass\e[0m from the official repositories as well" wrn "and \e[1msshpass\e[0m from the official repositories as well"
wrn "as \e[1mborgmatic\e[0m from the official PyPI repository." wrn "as \e[1mborgmatic\e[0m from the official PyPI repository."
if ! wrnp "Do you want to continue? [y/N]"; then # only ask for confirmation in interactive mode
if [ -z $AUTO ] && ! wrnp "Do you want to continue? [y/N]"; then
inf "Aborted." inf "Aborted."
exit 1 exit 1
fi fi
@ -364,9 +548,11 @@ else
fi fi
fi fi
if infp "Do you also want to upgrade all installed packages? [y/N]"; then if [ -z $NO_UPGRADE ]; then
if ! inf_follow "Upgrading packages..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" apt_upgrade; then if [ -z $AUTO ] && infp "Do you also want to upgrade all installed packages? [y/N]"; then
exit 1 if ! inf_follow "Upgrading packages..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" apt_upgrade; then
exit 1
fi
fi fi
fi fi
@ -535,18 +721,20 @@ if ! inf_follow "Checking for cronjob..." "\e[1;32mYes\e[0m" "\e[1;31mNo\e[0m" c
fi fi
fi fi
if ! infp "Do you want to run the first backup now? [y/N]"; then if [ -z $AUTO ]; then
inf "Setup completed." if [ -z $FIRST_BACKUP ] && infp "Do you want to run the first backup now? [y/N]"; then
exit 0 if ! inf_follow "Running first backup..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" run_command /root/.local/pipx/venvs/borgmatic/bin/borgmatic create --verbosity 1 --list --stats; then
fi err "Failed to run first backup."
err "If the error persists, please contact the support at $SCRIPT_SUPPORT."
if ! inf_follow "Running first backup..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" run_command /root/.local/pipx/venvs/borgmatic/bin/borgmatic create --verbosity 1 --list --stats; then exit 1
err "Failed to run first backup." else
err "If the error persists, please contact the support at $SCRIPT_SUPPORT." inf "Your first backup has been created successfully!"
exit 1 fi
else
else inf "Skipping first backup."
inf "Your first backup has been created successfully!" inf "Setup completed."
exit 0
fi
fi fi
inf "Setup completed." inf "Setup completed."