Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elif Block Not Entering as Expected When BACKUP_CRON Is Unset #375

Open
LeoColman opened this issue Dec 11, 2024 · 0 comments
Open

elif Block Not Entering as Expected When BACKUP_CRON Is Unset #375

LeoColman opened this issue Dec 11, 2024 · 0 comments

Comments

@LeoColman
Copy link
Contributor

LeoColman commented Dec 11, 2024

# Disable cron if it's set to disabled.
if [[ "$BACKUP_CRON" = "false" ]]; then
echo "Disabling cron, removing configuration"
# crontab -r # quite destructive
# echo -n > /etc/crontabs/root # Empty config, doesn't look as nice with "crontab -l"
echo "# Cron disabled" > /etc/crontabs/root
echo "Cron is now disabled"
# Apply default or custom cron if $BACKUP_CRON is unset or set (not null):
elif [[ -v BACKUP_CRON ]]; then
BACKUP_CRON="${BACKUP_CRON:-"0 1 * * *"}"
CRON_COMMAND="${CRON_COMMAND:-"/usr/local/bin/borgmatic --stats -v 0 2>&1"}"
echo "$BACKUP_CRON $CRON_COMMAND" > /etc/crontabs/root
echo "Applying custom cron"
# If nothing is set, revert to default behaviour
else
echo "Applying crontab.txt"
crontab /etc/borgmatic.d/crontab.txt
fi

The current behavior of the script does not align with the intended functionality. Specifically, the elif [[ -v BACKUP_CRON ]] block is not entered when the BACKUP_CRON variable is unset.

The intended behavior is for the script to handle the case where BACKUP_CRON is not set and to enter the elif block with a default value.


Steps to Reproduce

  1. Run the script with the BACKUP_CRON environment variable unset.
  2. Observe that the elif [[ -v BACKUP_CRON ]] block is not executed, and the script proceeds to the else block instead.

Expected Behavior

When the BACKUP_CRON variable is unset, the script should enter the elif block and set BACKUP_CRON to the default value (0 1 * * *) and apply the corresponding cron job.


Actual Behavior

When BACKUP_CRON is unset, the script skips the elif block and falls into the else block, applying the default crontab.txt instead.


Proposed Fix

Update the condition for the elif block to explicitly handle the unset BACKUP_CRON variable. Suggested change:

elif [[ -z "${BACKUP_CRON+x}" ]]; then
    BACKUP_CRON="${BACKUP_CRON:-"0 1 * * *"}"
    CRON_COMMAND="${CRON_COMMAND:-"/usr/local/bin/borgmatic --stats -v 0 2>&1"}"
    echo "$BACKUP_CRON $CRON_COMMAND" > /etc/crontabs/root
    echo "Applying custom cron"

The [[ -z "${BACKUP_CRON+x}" ]] check ensures that the block is entered when BACKUP_CRON is not set.


Current Workaround

The current workaround that I found was to set the default value in the config too:

  backup:
    image: b3vis/borgmatic
    volumes:
      - database:/data/database
      - redmine:/data/redmine
      - ssh:/root/.ssh/
    environment:
      - BORG_PASSPHRASE
      - BACKUP_CRON=0 1 * * * # Remove when https://github.com/borgmatic-collective/docker-borgmatic/issues/375 is fixed
    configs:
      - source: borgmatic-config-redmine
        target: /etc/borgmatic.d/config.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant