Cron job is now created as file at /etc/cron.d/borgmatic

This commit is contained in:
Enrico Ludwig 2024-05-15 21:47:45 +02:00
parent b3a4b2a2e5
commit 0c1d60ca22

View File

@ -57,6 +57,7 @@ AUTO=0
NO_UPGRADE=0 NO_UPGRADE=0
FIRST_BACKUP=0 FIRST_BACKUP=0
IS_REMOTE_SYNOLGY=0 IS_REMOTE_SYNOLGY=0
OVERRIDE_CRONTAB=0
# constants # constants
readonly SCRIPT_NAME="Borgmatic Backup Setup Tool" readonly SCRIPT_NAME="Borgmatic Backup Setup Tool"
@ -324,17 +325,27 @@ function dir_exists {
} }
function add_crontab { 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 $? return $?
} }
function check_crontab { function check_crontab {
crontab -l | grep -q "# borgmatic backup job" # check for file /etc/cron.d/borgmatic
if [ -f /etc/cron.d/borgmatic ]; then
return $? return 0
else
return 1
fi
} }
function is_remote_synology { function is_remote_synology {
@ -351,6 +362,7 @@ function is_remote_synology {
# -h, --help: Show help message # -h, --help: Show help message
# -n, --no-upgrade: Skip package upgrades # -n, --no-upgrade: Skip package upgrades
# -f, --first-backup: Run the first backup after setup # -f, --first-backup: Run the first backup after setup
# -o, --override-crontab: Override the existing crontab file
# #
# -R, --repo: Set the backup repository path # -R, --repo: Set the backup repository path
# -E, --encryption: Set the encryption method # -E, --encryption: Set the encryption method
@ -419,6 +431,10 @@ while [[ $# -gt 0 ]]; do
FIRST_BACKUP=1 FIRST_BACKUP=1
shift shift
;; ;;
-o|--override-crontab)
OVERRIDE_CRONTAB=1
shift
;;
-h|--help) -h|--help)
inf "Usage: borgmatic_setup.sh [options]" inf "Usage: borgmatic_setup.sh [options]"
inf "" inf ""
@ -433,6 +449,7 @@ while [[ $# -gt 0 ]]; do
inf " -n, --no-upgrade: Skip package upgrades" inf " -n, --no-upgrade: Skip package upgrades"
inf " -d, --debug: Enable debug mode" inf " -d, --debug: Enable debug mode"
inf " -f, --first-backup: Run the first backup after setup" 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 " -v, --version: Show script version"
inf " -h, --help: Show help message" inf " -h, --help: Show help message"
inf "" inf ""
@ -838,19 +855,17 @@ else
fi fi
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
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 "Failed to add borgmatic cron job."
err "If the error persists, please contact the support at $SCRIPT_SUPPORT." err "If the error persists, please contact the support at $SCRIPT_SUPPORT."
exit 1 exit 1
else else
if ! inf_follow "Validating borgmatic cron job..." "\e[1;32mOK\e[0m" "\e[1;31mFAILED\e[0m" check_crontab; then 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 "Validation of borgmatic cron job failed."
err "Please check the cron job with crontab -l." 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." err "If the error persists, please contact the support at $SCRIPT_SUPPORT."
exit 1 exit 1
fi fi
fi
fi fi
if [ $FIRST_BACKUP -eq 1 ]; then if [ $FIRST_BACKUP -eq 1 ]; then