From 6567c44fb192d41d9069a6a823d6901ab1f8f76d Mon Sep 17 00:00:00 2001 From: Simon Templer Date: Mon, 10 Sep 2018 13:34:34 +0200 Subject: [PATCH] add local mode for backup --- Dockerfile | 14 ++++++++++---- README.md | 12 ++++++++++++ scripts/backup.sh | 4 ++-- scripts/local/backup.sh | 5 +++++ scripts/local/restore.sh | 10 ++++++++++ scripts/notifications.sh | 4 ++++ test-backup.sh | 2 ++ test-env.txt.sample | 3 ++- 8 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 scripts/local/backup.sh create mode 100644 scripts/local/restore.sh diff --git a/Dockerfile b/Dockerfile index 71f5e16..45696ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,10 +15,6 @@ COPY ./scripts /dockup/ RUN chmod 755 /dockup/*.sh CMD ["/dockup/run.sh"] -ENV S3_BUCKET_NAME docker-backups.example.com -ENV AWS_ACCESS_KEY_ID **DefineMe** -ENV AWS_SECRET_ACCESS_KEY **DefineMe** -ENV AWS_DEFAULT_REGION us-east-1 ENV PATHS_TO_BACKUP auto ENV BACKUP_NAME backup ENV RESTORE false @@ -29,4 +25,14 @@ ENV BACKUP_TAR_TRIES 5 ENV BACKUP_TAR_RETRY_SLEEP 30 ENV DOCKUP_MODE aws +# aws +ENV S3_BUCKET_NAME docker-backups.example.com +ENV AWS_ACCESS_KEY_ID **DefineMe** +ENV AWS_SECRET_ACCESS_KEY **DefineMe** +ENV AWS_DEFAULT_REGION us-east-1 + +# local +ENV LOCAL_TARGET /dockup/target +RUN mkdir $LOCAL_TARGET + WORKDIR /dockup diff --git a/README.md b/README.md index 10bf43e..8ac1a57 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,18 @@ It should either be empty or hold a path and end with a slash. For more complex backup tasks as dumping a database, you can optionally define the environment variables `BEFORE_BACKUP_CMD` and `AFTER_BACKUP_CMD`. +### Backup target + +Although dockup was initially designed to specifically support backups so AWS S3, you can also use different kinds of backup targets. + +This is possible via the environment variable `DOCKUP_MODE`. +It allows running custom logic to do the actual backup/restoration (see subfolders in `scripts/`). + +By default, the following modes are available: + +- `aws` - store in AWS S3 +- `local` - store in a local folder (e.g. in a volume mounted into the dockup container, defaults to `/dockup/target`) + ### Detect volumes Instead of providing paths manually you can set the `PATHS_TO_BACKUP` to `auto`. diff --git a/scripts/backup.sh b/scripts/backup.sh index f15b351..7967b28 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -115,10 +115,10 @@ cleanup end_time=`date +%Y-%m-%d\\ %H:%M:%S\\ %Z` backup_duration=`date -u -d @"$SECONDS" +'%-Mm %-Ss'` if [ $rc -ne 0 ]; then - notifyFailure "Error uploading backup to S3." + notifyFailure "Error backuping up archive." echo -e "[$end_time] Backup failed\n\n" exit $rc else notifySuccess - echo -e "[$end_time] Archive successfully uploaded to S3\n\n" + echo -e "[$end_time] Archive successfully backed up\n\n" fi diff --git a/scripts/local/backup.sh b/scripts/local/backup.sh new file mode 100644 index 0000000..70efdb9 --- /dev/null +++ b/scripts/local/backup.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# Move the tarball to the destination folder +echo "Move the archive to the destination folder..." +mv $tarball ${LOCAL_TARGET}/ diff --git a/scripts/local/restore.sh b/scripts/local/restore.sh new file mode 100644 index 0000000..b4c6710 --- /dev/null +++ b/scripts/local/restore.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -n "${LAST_BACKUP}" ]; then + # Find last backup file + : ${LAST_BACKUP:=$(ls $LOCAL_TARGET/ -1 | grep ^$BACKUP_NAME | sort -r | head -n1)} +fi + +# Retrieve from local storage +echo "Retrieving backup archive $LAST_BACKUP..." +mv "${LOCAL_TARGET}/${LAST_BACKUP}" $LAST_BACKUP diff --git a/scripts/notifications.sh b/scripts/notifications.sh index 7dd78d5..4a86f83 100644 --- a/scripts/notifications.sh +++ b/scripts/notifications.sh @@ -1,5 +1,9 @@ #!/bin/bash +# +# Note: Notification texts assume storage in S3 +# + function notifySuccess { if [ "$NOTIFY_BACKUP_SUCCESS" == "true" ]; then notify_summary="[${S3_FOLDER}$BACKUP_NAME] Backup succeeded." diff --git a/test-backup.sh b/test-backup.sh index a1dc6ad..3ac9ea3 100755 --- a/test-backup.sh +++ b/test-backup.sh @@ -46,6 +46,7 @@ docker run --rm \ -e GPG_KEYRING=/$GPG_KEYNAME.pub \ --volumes-from dockup-data-test \ -v $(pwd)/$GPG_KEYNAME.pub:/$GPG_KEYNAME.pub \ + -v $(pwd)/target:/dockup/target \ --name dockup-run-test wetransform/dockup:local rc=$?; if [ $rc -ne 0 ]; then echo "ERROR: Error running backup" @@ -69,6 +70,7 @@ docker run --rm \ --volumes-from dockup-data-test \ -v $(pwd)/$GPG_KEYNAME.pub:/$GPG_KEYNAME.pub \ -v $(pwd)/$GPG_KEYNAME.sec:/$GPG_KEYNAME.sec \ + -v $(pwd)/target:/dockup/target \ --name dockup-run-test wetransform/dockup:local rc=$?; if [ $rc -ne 0 ]; then echo "ERROR: Error running restore" diff --git a/test-env.txt.sample b/test-env.txt.sample index a359fe9..2f3554f 100644 --- a/test-env.txt.sample +++ b/test-env.txt.sample @@ -4,4 +4,5 @@ AWS_DEFAULT_REGION=eu-central-1 S3_BUCKET_NAME=??? S3_FOLDER=dockup-test/ NOTIFY_BACKUP_SUCCESS=false -NOTIFY_BACKUP_FAILURE=false \ No newline at end of file +NOTIFY_BACKUP_FAILURE=false +DOCKUP_MODE=aws \ No newline at end of file