Browse Source

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
ViViDboarder 4 months ago
parent
commit
b2fd9d4259

+ 6
- 0
Dockerfile.raspbian View File

@@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
43 43
 ENV FULL_CRON_SCHEDULE=""
44 44
 ENV VERIFY_CRON_SCHEDULE=""
45 45
 
46
+# Create script dirs
47
+RUN mkdir -p /scripts/backup/before
48
+RUN mkdir -p /scripts/backup/after
49
+RUN mkdir -p /scripts/restore/before
50
+RUN mkdir -p /scripts/restore/after
51
+
46 52
 ADD backup.sh /
47 53
 ADD restore.sh /
48 54
 ADD start.sh /

+ 6
- 0
Dockerfile.ubuntu View File

@@ -43,6 +43,12 @@ ENV CRON_SCHEDULE=""
43 43
 ENV FULL_CRON_SCHEDULE=""
44 44
 ENV VERIFY_CRON_SCHEDULE=""
45 45
 
46
+# Create script dirs
47
+RUN mkdir -p /scripts/backup/before
48
+RUN mkdir -p /scripts/backup/after
49
+RUN mkdir -p /scripts/restore/before
50
+RUN mkdir -p /scripts/restore/after
51
+
46 52
 ADD backup.sh /
47 53
 ADD restore.sh /
48 54
 ADD start.sh /

+ 2
- 0
Makefile View File

@@ -20,10 +20,12 @@ build-all: build-x86 build-arm
20 20
 .PHONY: test-x86
21 21
 test-x86: build-x86
22 22
 	cd tests && ./test.sh $(DOCKER_TAG):ubuntu
23
+	cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):ubuntu
23 24
 
24 25
 .PHONY: test-arm
25 26
 test-arm: build-arm
26 27
 	cd tests && ./test.sh $(DOCKER_TAG):raspbian
28
+	cd tests && ./test-pre-scripts.sh $(DOCKER_TAG):raspbian
27 29
 
28 30
 .PHONY: test-all
29 31
 test-all: test-x86 test-arm

+ 15
- 0
backup.sh View File

@@ -7,6 +7,13 @@ set -e
7 7
         exit 1
8 8
     fi
9 9
 
10
+    # Run pre-backup scripts
11
+    for f in /scripts/backup/before/*; do
12
+        if [ -f $f -a -x $f ]; then
13
+            bash $f
14
+        fi
15
+    done
16
+
10 17
     duplicity \
11 18
         $1 \
12 19
         --asynchronous-upload \
@@ -22,4 +29,12 @@ set -e
22 29
             --name $BACKUP_NAME \
23 30
             $BACKUP_DEST
24 31
     fi
32
+
33
+    # Run post-backup scripts
34
+    for f in /scripts/backup/after/*; do
35
+        if [ -f $f -a -x $f ]; then
36
+            bash $f
37
+        fi
38
+    done
39
+
25 40
 ) 200>/var/lock/duplicity/.duplicity.lock

+ 15
- 0
restore.sh View File

@@ -7,6 +7,13 @@ set -e
7 7
         exit 1
8 8
     fi
9 9
 
10
+    # Run pre-restore scripts
11
+    for f in /scripts/restore/before/*; do
12
+        if [ -f $f -a -x $f ]; then
13
+            bash $f
14
+        fi
15
+    done
16
+
10 17
     duplicity restore \
11 18
         --force \
12 19
         --log-file /root/duplicity.log \
@@ -15,4 +22,12 @@ set -e
15 22
         $@ \
16 23
         $BACKUP_DEST \
17 24
         $PATH_TO_BACKUP
25
+
26
+    # Run post-restore scripts
27
+    for f in /scripts/restore/after/*; do
28
+        if [ -f $f -a -x $f ]; then
29
+            bash $f
30
+        fi
31
+    done
32
+
18 33
 ) 200>/var/lock/duplicity/.duplicity.lock

+ 71
- 0
tests/test-pre-scripts.sh View File

@@ -0,0 +1,71 @@
1
+#! /bin/bash
2
+set -e
3
+
4
+image=$1
5
+
6
+if [ "$IN_CONTAINER" != "true" ] ; then
7
+    # Run the test script within the container
8
+    docker run --rm \
9
+        -e IN_CONTAINER=true \
10
+        -e SKIP_ON_START=true \
11
+        -v "$(pwd)/test-pre-scripts.sh:/test.sh" \
12
+        -v "$(pwd)/test-pre-scripts:/scripts" \
13
+        $image \
14
+        bash -c "/test.sh"
15
+else
16
+    echo "Performing backup tests"
17
+
18
+    echo "Verify cron and crontab exist"
19
+    type cron
20
+    type crontab
21
+
22
+    echo "Install sqlite3"
23
+    apt-get update
24
+    apt-get install -y --no-install-recommends sqlite3
25
+
26
+    echo "Create test data..."
27
+    mkdir -p /data
28
+    touch /data/test_database.db
29
+    sqlite3 /data/test_database.db < /scripts/create-test-data.sql
30
+
31
+    echo "Making backup..."
32
+    /backup.sh
33
+
34
+    echo "Verify intermediary file is gone"
35
+    test -f /data/test_database.db.bak && exit 1 || echo "Gone"
36
+
37
+    echo "Delete test data..."
38
+    rm -fr /data/*
39
+
40
+    echo "Verify deleted..."
41
+    test -f /data/test_database.db && exit 1 || echo "Gone"
42
+
43
+    echo "Restore backup..."
44
+    /restore.sh
45
+
46
+    echo "Verify restored files exist..."
47
+    test -f /data/test_database.db
48
+    test -f /data/test_database.db.bak && exit 1 || echo "Gone"
49
+    sqlite3 /data/test_database.db "select data from test_table where id = 1"
50
+
51
+    echo "Delete test data again..."
52
+    rm -fr /data/*
53
+
54
+    echo "Verify deleted..."
55
+    test -f /data/test_database.db && exit 1 || echo "Gone"
56
+
57
+    echo "Simulate a restart with RESTORE_ON_EMPTY_START..."
58
+    RESTORE_ON_EMPTY_START=true /start.sh
59
+
60
+    echo "Verify restore happened..."
61
+    test -f /data/test_database.db
62
+    test -f /data/test_database.db.bak && exit 1 || echo "Gone"
63
+    sqlite3 /data/test_database.db "select data from test_table where id = 1"
64
+
65
+    echo "Delete test data..."
66
+    rm -fr /data/*
67
+
68
+    echo "Verify restore with incorrect passphrase fails..."
69
+    echo "Fail to restore backup..."
70
+    PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK"
71
+fi

+ 6
- 0
tests/test-pre-scripts/backup/after/post-backup.sh View File

@@ -0,0 +1,6 @@
1
+set -e
2
+
3
+cd /data
4
+
5
+# Remove backed up copy
6
+rm test_database.db.bak

+ 6
- 0
tests/test-pre-scripts/backup/before/pre-backup.sh View File

@@ -0,0 +1,6 @@
1
+set -e
2
+
3
+cd /data
4
+
5
+# Dump the SQLite database
6
+sqlite3 test_database.db ".backup test_database.db.bak"

+ 7
- 0
tests/test-pre-scripts/create-test-data.sql View File

@@ -0,0 +1,7 @@
1
+CREATE TABLE test_table (
2
+    id integer PRIMARY KEY,
3
+    data text NOT NULL
4
+);
5
+
6
+INSERT INTO test_table (data)
7
+VALUES ("Test row");

+ 6
- 0
tests/test-pre-scripts/restore/after/post-restore.sh View File

@@ -0,0 +1,6 @@
1
+set -e
2
+
3
+cd /data
4
+
5
+# Restore the backedup database
6
+mv test_database.db.bak test_database.db

+ 3
- 0
tests/test-pre-scripts/restore/before/pre-restore.sh View File

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

Loading…
Cancel
Save