diff --git a/borgmatic_setup.sh b/borgmatic_setup.sh index cf36322..d260dd8 100755 --- a/borgmatic_setup.sh +++ b/borgmatic_setup.sh @@ -57,6 +57,7 @@ AUTO=0 NO_UPGRADE=0 FIRST_BACKUP=0 IS_REMOTE_SYNOLGY=0 +OVERRIDE_CRONTAB=0 # constants readonly SCRIPT_NAME="Borgmatic Backup Setup Tool" @@ -324,17 +325,27 @@ function dir_exists { } function add_crontab { - line="$BACKUP_CRON_SCHEDULE root PATH=$PATH:/usr/bin:/usr/local/bin /root/.local/bin/borgmatic --verbosity -1 --syslog-verbosity 1 # borgmatic backup job" + if check_crontab; then + if [ $OVERRIDE_CRONTAB -eq 1 ]; then + wrn "Crontab file already exists. Overriding existing crontab file." + else + err "Crontab file already exists. Please remove the existing crontab file or use the --override-crontab option." + return 1 + fi + fi - (crontab -l; echo "$line") | crontab - + echo "$BACKUP_CRON_SCHEDULE root PATH=\$PATH:/usr/bin:/usr/local/bin /root/.local/bin/borgmatic --verbosity -1 --syslog-verbosity 1" > /etc/cron.d/borgmatic return $? } function check_crontab { - crontab -l | grep -q "# borgmatic backup job" - - return $? + # check for file /etc/cron.d/borgmatic + if [ -f /etc/cron.d/borgmatic ]; then + return 0 + else + return 1 + fi } function is_remote_synology { @@ -351,6 +362,7 @@ function is_remote_synology { # -h, --help: Show help message # -n, --no-upgrade: Skip package upgrades # -f, --first-backup: Run the first backup after setup +# -o, --override-crontab: Override the existing crontab file # # -R, --repo: Set the backup repository path # -E, --encryption: Set the encryption method @@ -419,6 +431,10 @@ while [[ $# -gt 0 ]]; do FIRST_BACKUP=1 shift ;; + -o|--override-crontab) + OVERRIDE_CRONTAB=1 + shift + ;; -h|--help) inf "Usage: borgmatic_setup.sh [options]" inf "" @@ -433,6 +449,7 @@ while [[ $# -gt 0 ]]; do inf " -n, --no-upgrade: Skip package upgrades" inf " -d, --debug: Enable debug mode" inf " -f, --first-backup: Run the first backup after setup" + inf " -o, --override-crontab: Override the existing crontab file" inf " -v, --version: Show script version" inf " -h, --help: Show help message" inf "" @@ -838,18 +855,16 @@ else fi fi -if ! inf_follow "Checking for cronjob..." "\e[1;32mYes\e[0m" "\e[1;31mNo\e[0m" check_crontab; then - if ! inf_follow "Adding borgmatic cron job..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" add_crontab; then - err "Failed to add borgmatic cron job." +if ! inf_follow "Adding borgmatic cron job..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" add_crontab; then + err "Failed to add borgmatic cron job." + err "If the error persists, please contact the support at $SCRIPT_SUPPORT." + exit 1 +else + if ! inf_follow "Validating borgmatic cron job..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" check_crontab; then + err "Validation of borgmatic cron job failed." + err "Please check if the file exists using 'cat /etc/cron.d/borgmatic'." err "If the error persists, please contact the support at $SCRIPT_SUPPORT." exit 1 - else - if ! inf_follow "Validating borgmatic cron job..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" check_crontab; then - err "Validation of borgmatic cron job failed." - err "Please check the cron job with crontab -l." - err "If the error persists, please contact the support at $SCRIPT_SUPPORT." - exit 1 - fi fi fi