Merge remote-tracking branch 'duplicity/health-check' into health-check

This commit is contained in:
ViViDboarder 2019-02-01 14:00:45 -08:00
commit 8cc87d0a16
5 changed files with 39 additions and 18 deletions

View File

@ -20,9 +20,13 @@ ENV PATH_TO_BACKUP="/data"
ENV CRON_SCHEDULE="" ENV CRON_SCHEDULE=""
ENV VERIFY_CRON_SCHEDULE="" ENV VERIFY_CRON_SCHEDULE=""
ADD backup.sh / COPY backup.sh /
ADD restore.sh / COPY restore.sh /
ADD start.sh / COPY start.sh /
ADD verify.sh / COPY verify.sh /
COPY healthcheck.sh /
COPY cron-exec.sh /
HEALTHCHECK CMD /healthcheck.sh
CMD [ "/start.sh" ] CMD [ "/start.sh" ]

10
cron-exec.sh Executable file
View File

@ -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; }

5
healthcheck.sh Executable file
View File

@ -0,0 +1,5 @@
#! /bin/bash
HEALTH_FILE=/unhealthy
test -f $HEALTH_FILE || exit 0 && exit 1

View File

@ -16,29 +16,29 @@ restic -r $BACKUP_DEST snapshots || restic -r $BACKUP_DEST init
# If set to restore on start, restore if the data volume is empty # 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 if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then
/restore.sh latest /cron-exec.sh /restore.sh latest
fi fi
# Unless explicitly skipping, take a backup on startup # Unless explicitly skipping, take a backup on startup
if [ "$SKIP_ON_START" != "true" ]; then if [ "$SKIP_ON_START" != "true" ]; then
/backup.sh /cron-exec.sh /backup.sh
fi fi
if [ -n "$CRON_SCHEDULE" ]; then if [ -n "$CRON_SCHEDULE" ]; then
# Export the environment to a file so it can be loaded from cron # 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 # 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 # Use bash for cron
echo "SHELL=/bin/bash" > /crontab.conf echo "SHELL=/bin/bash" > /crontab.conf
# Schedule the backups # 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" echo "Backups scheduled as $CRON_SCHEDULE"
if [ -n "$VERIFY_CRON_SCHEDULE" ]; then 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" echo "Verify scheduled as $VERIFY_CRON_SCHEDULE"
fi fi

View File

@ -26,10 +26,10 @@ else
CRON_SCHEDULE="" /start.sh CRON_SCHEDULE="" /start.sh
echo "Making backup..." echo "Making backup..."
/backup.sh /cron-exec.sh /backup.sh || { cat /cron.log && exit 1; }
echo "Verify backup..." echo "Verify backup..."
/verify.sh /cron-exec.sh /verify.sh || { cat /cron.log && exit 1; }
echo "Delete test data..." echo "Delete test data..."
rm -fr /data/* rm -fr /data/*
@ -38,10 +38,8 @@ else
test -f /data/test.txt && exit 1 || echo "Gone" test -f /data/test.txt && exit 1 || echo "Gone"
echo "Restore backup..." echo "Restore backup..."
/restore.sh latest /cron-exec.sh /restore.sh latest || { cat /cron.log && exit 1; }
/healthcheck.sh
echo "Verify restore..."
cat /data/test.txt
echo "Verify restore..." echo "Verify restore..."
test -f /data/test.txt test -f /data/test.txt
@ -57,7 +55,8 @@ else
test -f /data/test.txt && exit 1 || echo "Gone" test -f /data/test.txt && exit 1 || echo "Gone"
echo "Simulate a restart with RESTORE_ON_EMPTY_START..." echo "Simulate a restart with RESTORE_ON_EMPTY_START..."
RESTORE_ON_EMPTY_START=true /start.sh RESTORE_ON_EMPTY_START=true /start.sh || { cat /cron.log && exit 1; }
/healthcheck.sh || { echo "Failed healthcheck"; cat /cron.log; exit 1; }
echo "Verify restore happened..." echo "Verify restore happened..."
test -f /data/test.txt test -f /data/test.txt
@ -65,5 +64,8 @@ else
echo "Verify restore with incorrect passphrase fails..." echo "Verify restore with incorrect passphrase fails..."
echo "Fail to restore backup..." echo "Fail to restore backup..."
RESTIC_PASSWORD=Incorrect.Mule.Solar.Paperclip /restore.sh latest && exit 1 || echo "OK" RESTIC_PASSWORD=Incorrect.Mule.Solar.Paperclip /cron-exec.sh /restore.sh latest && exit 1 || echo "OK"
echo "Verify failed healthcheck"
/healthcheck.sh && exit 1 || echo "OK"
fi fi