mirror of
https://github.com/ViViDboarder/docker-restic-cron.git
synced 2024-11-23 05:36:38 +00:00
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:
parent
f4b9383c2a
commit
b2fd9d4259
@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
|
|||||||
ENV FULL_CRON_SCHEDULE=""
|
ENV FULL_CRON_SCHEDULE=""
|
||||||
ENV VERIFY_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 backup.sh /
|
||||||
ADD restore.sh /
|
ADD restore.sh /
|
||||||
ADD start.sh /
|
ADD start.sh /
|
||||||
|
@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
|
|||||||
ENV FULL_CRON_SCHEDULE=""
|
ENV FULL_CRON_SCHEDULE=""
|
||||||
ENV VERIFY_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 backup.sh /
|
||||||
ADD restore.sh /
|
ADD restore.sh /
|
||||||
ADD start.sh /
|
ADD start.sh /
|
||||||
|
2
Makefile
2
Makefile
@ -20,10 +20,12 @@ build-all: build-x86 build-arm
|
|||||||
.PHONY: test-x86
|
.PHONY: test-x86
|
||||||
test-x86: build-x86
|
test-x86: build-x86
|
||||||
cd tests && ./test.sh $(DOCKER_TAG):ubuntu
|
cd tests && ./test.sh $(DOCKER_TAG):ubuntu
|
||||||
|
cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):ubuntu
|
||||||
|
|
||||||
.PHONY: test-arm
|
.PHONY: test-arm
|
||||||
test-arm: build-arm
|
test-arm: build-arm
|
||||||
cd tests && ./test.sh $(DOCKER_TAG):raspbian
|
cd tests && ./test.sh $(DOCKER_TAG):raspbian
|
||||||
|
cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):raspbian
|
||||||
|
|
||||||
.PHONY: test-all
|
.PHONY: test-all
|
||||||
test-all: test-x86 test-arm
|
test-all: test-x86 test-arm
|
||||||
|
15
backup.sh
15
backup.sh
@ -7,6 +7,13 @@ set -e
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Run pre-backup scripts
|
||||||
|
for f in /scripts/backup/before/*; do
|
||||||
|
if [ -f $f -a -x $f ]; then
|
||||||
|
bash $f
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
duplicity \
|
duplicity \
|
||||||
$1 \
|
$1 \
|
||||||
--asynchronous-upload \
|
--asynchronous-upload \
|
||||||
@ -22,4 +29,12 @@ set -e
|
|||||||
--name $BACKUP_NAME \
|
--name $BACKUP_NAME \
|
||||||
$BACKUP_DEST
|
$BACKUP_DEST
|
||||||
fi
|
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
|
) 200>/var/lock/duplicity/.duplicity.lock
|
||||||
|
15
restore.sh
15
restore.sh
@ -7,6 +7,13 @@ set -e
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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 \
|
duplicity restore \
|
||||||
--force \
|
--force \
|
||||||
--log-file /root/duplicity.log \
|
--log-file /root/duplicity.log \
|
||||||
@ -15,4 +22,12 @@ set -e
|
|||||||
$@ \
|
$@ \
|
||||||
$BACKUP_DEST \
|
$BACKUP_DEST \
|
||||||
$PATH_TO_BACKUP
|
$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
|
) 200>/var/lock/duplicity/.duplicity.lock
|
||||||
|
71
tests/test-pre-scripts.sh
Executable file
71
tests/test-pre-scripts.sh
Executable file
@ -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
|
6
tests/test-pre-scripts/backup/after/post-backup.sh
Executable file
6
tests/test-pre-scripts/backup/after/post-backup.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
set -e
|
||||||
|
|
||||||
|
cd /data
|
||||||
|
|
||||||
|
# Remove backed up copy
|
||||||
|
rm test_database.db.bak
|
6
tests/test-pre-scripts/backup/before/pre-backup.sh
Executable file
6
tests/test-pre-scripts/backup/before/pre-backup.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
set -e
|
||||||
|
|
||||||
|
cd /data
|
||||||
|
|
||||||
|
# Dump the SQLite database
|
||||||
|
sqlite3 test_database.db ".backup test_database.db.bak"
|
7
tests/test-pre-scripts/create-test-data.sql
Normal file
7
tests/test-pre-scripts/create-test-data.sql
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE test_table (
|
||||||
|
id integer PRIMARY KEY,
|
||||||
|
data text NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO test_table (data)
|
||||||
|
VALUES ("Test row");
|
6
tests/test-pre-scripts/restore/after/post-restore.sh
Executable file
6
tests/test-pre-scripts/restore/after/post-restore.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
set -e
|
||||||
|
|
||||||
|
cd /data
|
||||||
|
|
||||||
|
# Restore the backedup database
|
||||||
|
mv test_database.db.bak test_database.db
|
3
tests/test-pre-scripts/restore/before/pre-restore.sh
Executable file
3
tests/test-pre-scripts/restore/before/pre-restore.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
set -e
|
||||||
|
|
||||||
|
# Don't really need to do anything here
|
Loading…
Reference in New Issue
Block a user