Skip to content

Commit

Permalink
Merge pull request #8 from christophetd/pre_post_scripts
Browse files Browse the repository at this point in the history
Add support for pre-backup and post-backup scripts (closes #2)
  • Loading branch information
christophetd authored Apr 25, 2018
2 parents 2d0b032 + df63eb2 commit 7d7dec8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
21 changes: 21 additions & 0 deletions duplicacy-autobackup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/sh

PRE_BACKUP_SCRIPT="/scripts/pre-backup.sh"
POST_BACKUP_SCRIPT="/scripts/post-backup.sh"

cd /data

do_init() {
Expand All @@ -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
Expand Down
79 changes: 79 additions & 0 deletions tests/test-pre-post-scripts.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
hello, world!
EOF

cat > pre.sh <<EOF
touch /data/pre-script-executed
EOF

cat > post.sh <<EOF
touch /data/post-script-executed
EOF

docker run -d --name duplicacy-autobackup \
-v $data_dir:/data \
-v $(pwd)/pre.sh:/scripts/pre-backup.sh \
-v $(pwd)/post.sh:/scripts/post-backup.sh \
-e BACKUP_NAME=$BACKUP_NAME \
-e BACKUP_LOCATION="$BACKUP_LOCATION" \
-e BACKUP_SCHEDULE='0 2 * * *' \
-e BACKUP_IMMEDIATLY='yes' \
-e BACKUP_ENCRYPTION_KEY="$PASSPHRASE" \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_KEY=$AWS_SECRET_ACCESS_KEY \
$IMAGE_NAME

echo "Waiting for backup to be performed..."
sleep 10

if [[ -z $(aws s3 ls $TEST_BUCKET) ]]; then
echo "Nothing in test bucket. Exiting" >&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"

0 comments on commit 7d7dec8

Please sign in to comment.