Add support for pre/post scripts

Allows adding scripts to a directory to be executed before backups and
after restorating. This can allow backing up a mysql dump or something.

Using these could (and probably does) break verify checks

Fixes #8
This commit is contained in:
ViViDboarder 2018-09-27 17:32:55 -07:00
parent 7b4e31c00d
commit b2d95cc775
10 changed files with 129 additions and 0 deletions

View File

@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
ENV FULL_CRON_SCHEDULE=""
ENV VERIFY_CRON_SCHEDULE=""
# Create script dirs
RUN mkdir -p /scripts/backup/before
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 /

View File

@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
ENV FULL_CRON_SCHEDULE=""
ENV VERIFY_CRON_SCHEDULE=""
# Create script dirs
RUN mkdir -p /scripts/backup/before
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 /

View File

@ -20,10 +20,12 @@ build-all: build-x86 build-arm
.PHONY: test-x86
test-x86: build-x86
cd tests && ./test.sh $(DOCKER_TAG):ubuntu
cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):ubuntu
.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

View File

@ -7,6 +7,13 @@ set -e
exit 1
fi
# Run pre-backup scripts
for f in /scripts/backup/before/*; do
if [ -f $f -a -x $f ]; then
bash $f
fi
done
duplicity \
$1 \
--asynchronous-upload \
@ -22,4 +29,12 @@ set -e
--name $BACKUP_NAME \
$BACKUP_DEST
fi
# Run post-backup scripts
for f in /scripts/backup/after/*; do
if [ -f $f -a -x $f ]; then
bash $f
fi
done
) 200>/var/lock/duplicity/.duplicity.lock

View File

@ -7,6 +7,13 @@ set -e
exit 1
fi
# Run pre-restore scripts
for f in /scripts/restore/before/*; do
if [ -f $f -a -x $f ]; then
bash $f
fi
done
duplicity restore \
--force \
--log-file /root/duplicity.log \
@ -15,4 +22,12 @@ set -e
$@ \
$BACKUP_DEST \
$PATH_TO_BACKUP
# Run post-restore scripts
for f in /scripts/restore/after/*; do
if [ -f $f -a -x $f ]; then
bash $f
fi
done
) 200>/var/lock/duplicity/.duplicity.lock

4
tests/scripts/post-backup.sh Executable file
View File

@ -0,0 +1,4 @@
set -e
# Clear dump
rm /data/dump.txt

3
tests/scripts/post-restore.sh Executable file
View File

@ -0,0 +1,3 @@
set -e
cat /data/dump.txt > /mydb.txt

7
tests/scripts/pre-backup.sh Executable file
View File

@ -0,0 +1,7 @@
set -e
# Pretend we have some database
echo "insert foo into bar;" > /mydb.txt
# Let's dump that db
cat /mydb.txt > /data/dump.txt

3
tests/scripts/pre-restore.sh Executable file
View File

@ -0,0 +1,3 @@
set -e
# Don't really need to do anything here

68
tests/test-pre-scripts.sh Executable file
View File

@ -0,0 +1,68 @@
#! /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)/scripts/pre-backup.sh:/scripts/backup/before/1.sh" \
-v "$(pwd)/scripts/post-backup.sh:/scripts/backup/after/1.sh" \
-v "$(pwd)/scripts/pre-restore.sh:/scripts/restore/before/1.sh" \
-v "$(pwd)/scripts/post-restore.sh:/scripts/restore/after/1.sh" \
$image \
bash -c "/test.sh"
else
echo "Performing backup tests"
echo "Verify cron and crontab exist"
type cron
type crontab
echo "Create test data..."
mkdir -p /data && echo Test > /data/test.txt
echo "Making backup..."
/backup.sh
echo "Delete test data..."
rm -fr /data/*
rm -fr /mydb.txt
echo "Verify deleted..."
test -f /data/test.txt && exit 1 || echo "Gone"
test -f /mydb.txt && exit 1 || echo "Gone"
echo "Restore backup..."
/restore.sh
echo "Verify pre-post script backups..."
test -f /mydb.txt
cat /mydb.txt
echo "Delete test data again..."
rm -fr /data/*
rm -fr /mydb.txt
echo "Verify deleted..."
test -f /data/test.txt && exit 1 || echo "Gone"
test -f /mydb.txt && 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.txt
cat /data/test.txt
echo "Verify pre-post script backups..."
test -f /mydb.txt
cat /mydb.txt
echo "Verify restore with incorrect passphrase fails..."
echo "Fail to restore backup..."
PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK"
fi