diff --git a/Makefile b/Makefile index 161ee69..ac462b7 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,12 @@ build-all: build-x86 build-arm .PHONY: test-x86 test-x86: build-x86 cd tests && ./test.sh $(DOCKER_TAG) + cd tests && ./test-pre-scripts.sh $(DOCKER_TAG) .PHONY: test-arm test-arm: build-arm cd tests && .test.sh $(DOCKER_TAG):raspbian + cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):raspbian .PHONY: test-all test-all: test-x86 test-arm diff --git a/backup.sh b/backup.sh index 1b80f81..750f9f5 100755 --- a/backup.sh +++ b/backup.sh @@ -1,6 +1,13 @@ #! /bin/bash set -e +# Run pre-backup scripts +for f in /scripts/backup/before/*; do + if [ -f $f -a -x $f ]; then + bash $f + fi +done + restic \ -r $BACKUP_DEST \ $OPT_ARGUMENTS \ @@ -13,3 +20,10 @@ if [ -n "$CLEANUP_COMMAND" ]; then forget \ $CLEANUP_COMMAND fi + +# Run post-backup scripts +for f in /scripts/backup/after/*; do + if [ -f $f -a -x $f ]; then + bash $f + fi +done diff --git a/restore.sh b/restore.sh index b45c4a0..9e8f8be 100755 --- a/restore.sh +++ b/restore.sh @@ -3,9 +3,23 @@ set -e restore_snapshot=$1 +# Run pre-restore scripts +for f in /scripts/restore/before/*; do + if [ -f $f -a -x $f ]; then + bash $f + fi +done + restic \ -r $BACKUP_DEST \ $OPT_ARGUMENTS \ restore \ $restore_snapshot \ -t / + +# Run post-restore scripts +for f in /scripts/restore/after/*; do + if [ -f $f -a -x $f ]; then + bash $f + fi +done diff --git a/tests/test-pre-scripts.sh b/tests/test-pre-scripts.sh new file mode 100755 index 0000000..8482406 --- /dev/null +++ b/tests/test-pre-scripts.sh @@ -0,0 +1,71 @@ +#! /bin/bash +set -e + +image=$1 + +if [ "$IN_CONTAINER" != "true" ] ; then + # Run the test script within the container + docker run --rm \ + -e IN_CONTAINER=true \ + -e SKIP_ON_START=true \ + -v "$(pwd)/test-pre-scripts.sh:/test.sh" \ + -v "$(pwd)/test-pre-scripts:/scripts" \ + $image \ + bash -c "/test.sh" +else + echo "Performing backup tests" + + echo "Verify cron and crontab exist" + type cron + type crontab + + echo "Install sqlite3" + apt-get update + apt-get install -y --no-install-recommends sqlite3 + + echo "Create test data..." + mkdir -p /data + touch /data/test_database.db + sqlite3 /data/test_database.db < /scripts/create-test-data.sql + + echo "Making backup..." + /backup.sh + + echo "Verify intermediary file is gone" + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + + echo "Delete test data..." + rm -fr /data/* + + echo "Verify deleted..." + test -f /data/test_database.db && exit 1 || echo "Gone" + + echo "Restore backup..." + /restore.sh + + echo "Verify restored files exist..." + test -f /data/test_database.db + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + sqlite3 /data/test_database.db "select data from test_table where id = 1" + + echo "Delete test data again..." + rm -fr /data/* + + echo "Verify deleted..." + test -f /data/test_database.db && exit 1 || echo "Gone" + + echo "Simulate a restart with RESTORE_ON_EMPTY_START..." + RESTORE_ON_EMPTY_START=true /start.sh + + echo "Verify restore happened..." + test -f /data/test_database.db + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + sqlite3 /data/test_database.db "select data from test_table where id = 1" + + echo "Delete test data..." + rm -fr /data/* + + echo "Verify restore with incorrect passphrase fails..." + echo "Fail to restore backup..." + PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK" +fi diff --git a/tests/test-pre-scripts/backup/after/post-backup.sh b/tests/test-pre-scripts/backup/after/post-backup.sh new file mode 100755 index 0000000..a606858 --- /dev/null +++ b/tests/test-pre-scripts/backup/after/post-backup.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Remove backed up copy +rm test_database.db.bak diff --git a/tests/test-pre-scripts/backup/before/pre-backup.sh b/tests/test-pre-scripts/backup/before/pre-backup.sh new file mode 100755 index 0000000..b0da835 --- /dev/null +++ b/tests/test-pre-scripts/backup/before/pre-backup.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Dump the SQLite database +sqlite3 test_database.db ".backup test_database.db.bak" diff --git a/tests/test-pre-scripts/create-test-data.sql b/tests/test-pre-scripts/create-test-data.sql new file mode 100644 index 0000000..0d63db8 --- /dev/null +++ b/tests/test-pre-scripts/create-test-data.sql @@ -0,0 +1,7 @@ +CREATE TABLE test_table ( + id integer PRIMARY KEY, + data text NOT NULL +); + +INSERT INTO test_table (data) +VALUES ("Test row"); diff --git a/tests/test-pre-scripts/restore/after/post-restore.sh b/tests/test-pre-scripts/restore/after/post-restore.sh new file mode 100755 index 0000000..7ede126 --- /dev/null +++ b/tests/test-pre-scripts/restore/after/post-restore.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Restore the backedup database +mv test_database.db.bak test_database.db diff --git a/tests/test-pre-scripts/restore/before/pre-restore.sh b/tests/test-pre-scripts/restore/before/pre-restore.sh new file mode 100755 index 0000000..ea3e3e4 --- /dev/null +++ b/tests/test-pre-scripts/restore/before/pre-restore.sh @@ -0,0 +1,3 @@ +set -e + +# Don't really need to do anything here diff --git a/tests/test.sh b/tests/test.sh index c45a3c8..3e4a8cb 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -43,6 +43,10 @@ else echo "Verify restore..." cat /data/test.txt + echo "Verify restore..." + test -f /data/test.txt + cat /data/test.txt + echo "Verify backup..." /verify.sh @@ -56,6 +60,7 @@ else RESTORE_ON_EMPTY_START=true /start.sh echo "Verify restore happened..." + test -f /data/test.txt cat /data/test.txt echo "Verify restore with incorrect passphrase fails..."