diff --git a/docker-compose/.env.example b/docker-compose/.env.example index 4449fe0..6ed4e38 100644 --- a/docker-compose/.env.example +++ b/docker-compose/.env.example @@ -3,6 +3,8 @@ PBS_ENCRYPTION_PASSWORD="123456789abcdefghijklmn" PBS_ENDPOINT="pbs.mydomain.com" PBS_DATASTORE="test-datastore" CRON_SCHEDULE="0 */4 * * *" +# If you want to skip backup on startup, set CRON_BACKUP_ONLY=1 otherwise CRON_BACKUP_ONLY=0 +CRON_BACKUP_ONLY=0 # Use of the PBS_API_KEY_NAME and PBS_API_KEY_SECRET is recommended! # If unset, ensure PBS_USER and PBS_PASSWORD are set. diff --git a/docker/src/s6-services/backup/run_include b/docker/src/s6-services/backup/run_include index 28f8af9..f41c357 100755 --- a/docker/src/s6-services/backup/run_include +++ b/docker/src/s6-services/backup/run_include @@ -8,6 +8,8 @@ path_to_filename() { echo "${filename}" } +lastrunfile="/tmp/pbs_client_lastrun" + BACKUP_DIRECTORIES=() # Iterate over each subdirectory under /backup and add its full path to the array for dir in /backups/*; do @@ -16,16 +18,18 @@ for dir in /backups/*; do fi done -# Print the contents of the array -echo "## Detected backup directories:" -echo -e "#" -if [ ${#BACKUP_DIRECTORIES[@]} -eq 0 ]; then - echo "# Nothing to backup." -else - for path in "${BACKUP_DIRECTORIES[@]}"; do - echo -e "# $path" - done - echo -e "#\n" +# Print the contents of the array if this is first run only. +if [ ! -e "${lastrunfile}" ]; then + echo "## Detected backup directories:" + echo -e "#" + if [ ${#BACKUP_DIRECTORIES[@]} -eq 0 ]; then + echo "# Nothing to backup." + else + for path in "${BACKUP_DIRECTORIES[@]}"; do + echo -e "# $path" + done + echo -e "#\n" + fi fi # Construct the directory target list with the proxmox-backup-client syntax. @@ -43,20 +47,36 @@ if [ -n "$PBS_DATASTORE_NS" ]; then BACKUPCMD+=" --ns ${PBS_DATASTORE_NS}" fi - # Source the variables from the setup_check scripting include file. source /etc/s6-overlay/s6-rc.d/setup_check/run_include -echo -e "## Backing up to repository: \n# ${PBS_REPOSITORY}\n" -echo -e "## Executing backup command: \n# ${BACKUPCMD}\n" - -if [ -n "$HEALTHCHECKSURL" ]; then - curl -fsS -m 10 --retry 5 $HEALTHCHECKSURL/start +# Print out the repository and backup command on first run only. +if [ ! -e "${lastrunfile}" ]; then + echo -e "## Backing up to repository: \n# ${PBS_REPOSITORY}\n" + echo -e "## Executing backup command: \n# ${BACKUPCMD}\n" fi -${BACKUPCMD} 2>&1 -BACKUP_EXIT_CODE=$? +# Note the first evaluation is key and will initiate backup if the user opts +# to allow the first backup or we're +if [ "$CRON_BACKUP_ONLY" = "0" ] || [ -e "${lastrunfile}" ]; then + if [ -n "$HEALTHCHECKSURL" ]; then + curl -fsS -m 10 --retry 5 $HEALTHCHECKSURL/start + fi -if [ -n "$HEALTHCHECKSURL" ]; then - curl -fsS -m 10 --retry 5 ${HEALTHCHECKSURL}/${BACKUP_EXIT_CODE} -fi \ No newline at end of file + # Run the actual backup command. + ${BACKUPCMD} 2>&1 + BACKUP_EXIT_CODE=$? + + if [ -n "$HEALTHCHECKSURL" ]; then + # We pipe the exit code to healthchecks, if it isn't zero, a warning will fire. + curl -fsS -m 10 --retry 5 ${HEALTHCHECKSURL}/${BACKUP_EXIT_CODE} + fi +elif [ "$CRON_BACKUP_ONLY" = "1" ]; then + echo "CRON_BACKUP_ONLY=1, skipping container start up initial backup." +elif [ "$CRON_BACKUP_ONLY" = "0" ] || [ "$CRON_BACKUP_ONLY" = "1" ] ]; then +echo "CRON_BACKUP_ONLY set to invalid value, skipping container start up initial backup." +fi + +# Set this so backups always happen after the first run via CRON given logic above. +# The date may also be useful for something like a health check if I write it... +echo "$(date +"%Y-%m-%d %H:%M:%S")" > /tmp/pbs_client_lastrun \ No newline at end of file diff --git a/docker/src/s6-services/setup_check/run_include b/docker/src/s6-services/setup_check/run_include index 82d0437..3001af4 100755 --- a/docker/src/s6-services/setup_check/run_include +++ b/docker/src/s6-services/setup_check/run_include @@ -1,8 +1,6 @@ #!/usr/bin/with-contenv bash # shellcheck shell=bash -echo -e "## Conducting variable checks." - # If an API key is set we should use it over any password / username. if [ -n "$PBS_API_KEY_SECRET" ]; then PBS_PASSWORD="${PBS_API_KEY_SECRET}"