diff --git a/Dockerfile.raspbian b/Dockerfile.raspbian index 9e039be..d5cec48 100644 --- a/Dockerfile.raspbian +++ b/Dockerfile.raspbian @@ -49,9 +49,13 @@ RUN mkdir -p /scripts/backup/after RUN mkdir -p /scripts/restore/before RUN mkdir -p /scripts/restore/after -ADD backup.sh / -ADD restore.sh / -ADD start.sh / -ADD verify.sh / +COPY backup.sh / +COPY restore.sh / +COPY start.sh / +COPY verify.sh / +COPY healthcheck.sh / +COPY cron-exec.sh / + +HEALTHCHECK CMD /healthcheck.sh CMD [ "/start.sh" ] diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu index dce74a7..1f3ffd8 100644 --- a/Dockerfile.ubuntu +++ b/Dockerfile.ubuntu @@ -49,9 +49,13 @@ RUN mkdir -p /scripts/backup/after RUN mkdir -p /scripts/restore/before RUN mkdir -p /scripts/restore/after -ADD backup.sh / -ADD restore.sh / -ADD start.sh / -ADD verify.sh / +COPY backup.sh / +COPY restore.sh / +COPY start.sh / +COPY verify.sh / +COPY healthcheck.sh / +COPY cron-exec.sh / + +HEALTHCHECK CMD /healthcheck.sh CMD [ "/start.sh" ] diff --git a/cron-exec.sh b/cron-exec.sh new file mode 100755 index 0000000..ed4667f --- /dev/null +++ b/cron-exec.sh @@ -0,0 +1,10 @@ +#! /bin/bash + +ENV=/env +LOG=/cron.log +HEALTH_FILE=/unhealthy + +test -f $ENV || echo NO_ENV=true > $ENV + +# Execute command and write output to log +env `cat $ENV | xargs` $@ 2>> $LOG && rm -f $HEALTH_FILE || { touch $HEALTH_FILE; exit 1; } diff --git a/healthcheck.sh b/healthcheck.sh new file mode 100755 index 0000000..b7052a1 --- /dev/null +++ b/healthcheck.sh @@ -0,0 +1,5 @@ +#! /bin/bash + +HEALTH_FILE=/unhealthy + +test -f $HEALTH_FILE || exit 0 && exit 1 diff --git a/start.sh b/start.sh index 1480490..4b65f1f 100755 --- a/start.sh +++ b/start.sh @@ -18,34 +18,34 @@ fi # If set to restore on start, restore if the data volume is empty if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then - /restore.sh + /cron-exec.sh /restore.sh fi # Unless explicitly skipping, take a backup on startup if [ "$SKIP_ON_START" != "true" ]; then - /backup.sh + /cron-exec.sh /backup.sh fi if [ -n "$CRON_SCHEDULE" ]; then # Export the environment to a file so it can be loaded from cron - env | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/g' > /env.sh + env > /env # Remove some vars we don't want to keep - sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env.sh + sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env # Use bash for cron echo "SHELL=/bin/bash" > /crontab.conf # Schedule the backups - echo "$CRON_SCHEDULE source /env.sh && /backup.sh 2>> /cron.log" >> /crontab.conf + echo "$CRON_SCHEDULE /cron-exec.sh /backup.sh" >> /crontab.conf echo "Backups scheduled as $CRON_SCHEDULE" if [ -n "$FULL_CRON_SCHEDULE" ]; then - echo "$FULL_CRON_SCHEDULE source /env.sh && /backup.sh full 2>> /cron.log" >> /crontab.conf + echo "$FULL_CRON_SCHEDULE /cron-exec.sh /backup.sh full" >> /crontab.conf echo "Full backup scheduled as $VERIFY_CRON_SCHEDULE" fi if [ -n "$VERIFY_CRON_SCHEDULE" ]; then - echo "$VERIFY_CRON_SCHEDULE source /env.sh && /verify.sh 2>> /cron.log" >> /crontab.conf + echo "$VERIFY_CRON_SCHEDULE /cron-exec.sh /verify.sh" >> /crontab.conf echo "Verify scheduled as $VERIFY_CRON_SCHEDULE" fi diff --git a/tests/test.sh b/tests/test.sh index 84546dc..8ebf31f 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -22,10 +22,10 @@ else mkdir -p /data && echo Test > /data/test.txt echo "Making backup..." - /backup.sh + /cron-exec.sh /backup.sh echo "Verify backup..." - /verify.sh + /cron-exec.sh /verify.sh echo "Delete test data..." rm -fr /data/* @@ -34,7 +34,8 @@ else test -f /data/test.txt && exit 1 || echo "Gone" echo "Restore backup..." - /restore.sh + /cron-exec.sh /restore.sh + /healthcheck.sh echo "Verify restore..." test -f /data/test.txt @@ -51,6 +52,7 @@ else echo "Simulate a restart with RESTORE_ON_EMPTY_START..." RESTORE_ON_EMPTY_START=true /start.sh + /healthcheck.sh echo "Verify restore happened..." test -f /data/test.txt @@ -58,5 +60,8 @@ else echo "Verify restore with incorrect passphrase fails..." echo "Fail to restore backup..." - PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK" + PASSPHRASE=Incorrect.Mule.Solar.Paperclip /cron-exec.sh /restore.sh && exit 1 || echo "OK" + + echo "Verify failed healthcheck" + /healthcheck.sh && exit 1 || echo "OK" fi