From df63eb20b13f56a91cca72e300194cbbb53366a0 Mon Sep 17 00:00:00 2001 From: Christophe Tafani-Dereeper Date: Wed, 25 Apr 2018 11:22:06 +0200 Subject: [PATCH] Add support for pre-backup and post-backup scripts (closes #2) --- README.md | 2 + duplicacy-autobackup.sh | 21 +++++++++ tests/test-pre-post-scripts.sh | 79 ++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tests/test-pre-post-scripts.sh diff --git a/README.md b/README.md index 6e7e705..8f69173 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,8 @@ More: see [Duplicacy's documentation](https://github.com/gilbertchen/duplicacy/w ## Other options +You can have duplicacy-autobackup run a script before and after the backup process by mounting scripts on `/scripts/pre-backup.sh` and `/scripts/post-backup.sh`. For instance if you're backing up a MySQL database, this script can involve doing a `mysqldump` into `/data/mydb.sql`. If `pre-backup.sh` exits with a non-zero status code, the backup will not be performed until the next scheduled backup. + Use the following environment variables if you want to customize duplicacy's behavior. - `BACKUP_IMMEDIATLY` (`yes`/`no`): indicates if a backup should be performed immediatly after the container is started. Equivalent to launching the container and then running `docker exec duplicacy-autobackup /app/duplicacy-autobackup.sh backup`. By default, `no`. diff --git a/duplicacy-autobackup.sh b/duplicacy-autobackup.sh index e8e406d..c0b0e55 100644 --- a/duplicacy-autobackup.sh +++ b/duplicacy-autobackup.sh @@ -1,5 +1,8 @@ #!/bin/sh +PRE_BACKUP_SCRIPT="/scripts/pre-backup.sh" +POST_BACKUP_SCRIPT="/scripts/post-backup.sh" + cd /data do_init() { @@ -20,7 +23,25 @@ do_init() { } do_backup() { + status=0 + if [[ -f $PRE_BACKUP_SCRIPT ]]; then + echo "Running pre-backup script" + sh $PRE_BACKUP_SCRIPT + status=$? + fi + if [[ $status != 0 ]]; then + echo "Pre-backup script exited with status code $status. Not performing backup." >&2 + return + fi + duplicacy backup $DUPLICACY_BACKUP_OPTIONS + + if [[ -f $POST_BACKUP_SCRIPT ]]; then + echo "Running post-backup script" + sh $POST_BACKUP_SCRIPT + status=$? + echo "Post-backup script exited with status $status" + fi } export DUPLICACY_PASSWORD=$BACKUP_ENCRYPTION_KEY diff --git a/tests/test-pre-post-scripts.sh b/tests/test-pre-post-scripts.sh new file mode 100644 index 0000000..9753ac3 --- /dev/null +++ b/tests/test-pre-post-scripts.sh @@ -0,0 +1,79 @@ +#!/bin/sh + + +: ${AWS_ACCESS_KEY_ID:?"Missing AWS secret key"} +: ${AWS_SECRET_ACCESS_KEY:?"Missing AWS secret key"} + +TEST_BUCKET=duplicacy-autobackup-tests +REGION=eu-central-1 +BACKUP_LOCATION="s3://$REGION@amazon.com/$TEST_BUCKET" +IMAGE_NAME=duplicacy-autobackup # local image +PASSPHRASE='correct horse battery staple' +BACKUP_NAME='prod-db-backups' +data_dir=$(mktemp -d) + +if [[ ! -z $(aws s3 ls $TEST_BUCKET) ]]; then + echo "Test bucket is not empty. Exiting" >&2 + exit 1 +fi + +cleanup() { + echo 'Cleaning up...' + aws s3 rm s3://$TEST_BUCKET/ --recursive + docker rm -f duplicacy-autobackup + rm -f pre.sh + rm -f post.sh + rm -f $data_dir/post-script-executed + rm -f $data_dir/pre-script-executed +} +trap cleanup EXIT + +cat > $data_dir/hello.txt < pre.sh < post.sh <&2 + exit 2 +fi + +if [[ ! -d $data_dir/.duplicacy ]]; then + echo "No duplicacy folder created. Exiting" >&2 + exit 3 +fi + +echo "Backup performed" + +if [[ ! -f $data_dir/pre-script-executed ]]; then + echo "Pre backup script wasn't executed" >&2 + exit 4 +elif [[ ! -f $data_dir/post-script-executed ]]; then + echo "Pre backup script wasn't executed" >&2 + exit 5 +fi + +echo "Pre and post backup script properly executed" \ No newline at end of file