From f57ea36f9859527ab9534d030a2f33544af8f7ca Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Sun, 13 May 2018 10:30:02 -0700 Subject: [PATCH] Obtain locks before restoring or backing up --- Dockerfile.raspbian | 6 ++++-- Dockerfile.ubuntu | 6 ++++-- Readme.md | 1 + backup.sh | 29 ++++++++++++++++++----------- restore.sh | 23 +++++++++++++++-------- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/Dockerfile.raspbian b/Dockerfile.raspbian index f711111..7545f7c 100644 --- a/Dockerfile.raspbian +++ b/Dockerfile.raspbian @@ -28,13 +28,15 @@ RUN apt-get update \ RUN [ "cross-build-end" ] -VOLUME "/root/.cache/duplicity" -VOLUME "/backups" +VOLUME /root/.cache/duplicity +VOLUME /backups +VOLUME /var/lock/duplicity ENV BACKUP_DEST="file:///backups" ENV BACKUP_NAME="backup" ENV PATH_TO_BACKUP="/data" ENV PASSPHRASE="Correct.Horse.Battery.Staple" +ENV FLOCK_WAIT=60 # Cron schedules ENV CRON_SCHEDULE="" diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu index 4a14904..d7c8529 100644 --- a/Dockerfile.ubuntu +++ b/Dockerfile.ubuntu @@ -28,13 +28,15 @@ RUN apt-get update \ && apt-get clean \ && rm -rf /var/apt/lists/* -VOLUME "/root/.cache/duplicity" -VOLUME "/backups" +VOLUME /root/.cache/duplicity +VOLUME /backups +VOLUME /var/lock/duplicity ENV BACKUP_DEST="file:///backups" ENV BACKUP_NAME="backup" ENV PATH_TO_BACKUP="/data" ENV PASSPHRASE="Correct.Horse.Battery.Staple" +ENV FLOCK_WAIT=60 # Cron schedules ENV CRON_SCHEDULE="" diff --git a/Readme.md b/Readme.md index e9e433f..cbaf633 100644 --- a/Readme.md +++ b/Readme.md @@ -15,6 +15,7 @@ Mount any directories you'd like to back up as a volume and run |BACKUP_NAME|backup|What the name for the backup should be. If using a single store for multiple backups, make sure this is unique| |CLEANUP_COMMAND| |An optional duplicity command to execute after backups to clean older ones out (eg. "remove-all-but-n-full 2")| |CRON_SCHEDULE| |If you want to periodic incremental backups on a schedule, provide it here. By default we just backup once and exit| +|FLOCK_WAIT|60|Seconds to wait for a lock before skipping a backup| |FTP_PASSWORD| |Used to provide passwords for some backends. May not work without an attached TTY| |FULL_CRON_SCHEDULE| |If you want to periodic full backups on a schedule, provide it here. This requires an incremental cron schedule too.| |GPG_KEY_ID| |The ID of the key you wish to use. See [Encryption](#encryption) section below| diff --git a/backup.sh b/backup.sh index b062349..4473a4d 100755 --- a/backup.sh +++ b/backup.sh @@ -1,18 +1,25 @@ #! /bin/bash set -e -duplicity \ - $1 \ - --asynchronous-upload \ - --log-file /root/duplicity.log \ - --name $BACKUP_NAME \ - $OPT_ARGUMENTS \ - $PATH_TO_BACKUP \ - $BACKUP_DEST +( + if ! flock -x -w $FLOCK_WAIT 200 ; then + echo 'ERROR: Could not obtain lock. Exiting.' + exit 1 + fi -if [ -n "$CLEANUP_COMMAND" ]; then - duplicity $CLEANUP_COMMAND \ + duplicity \ + $1 \ + --asynchronous-upload \ --log-file /root/duplicity.log \ --name $BACKUP_NAME \ + $OPT_ARGUMENTS \ + $PATH_TO_BACKUP \ $BACKUP_DEST -fi + + if [ -n "$CLEANUP_COMMAND" ]; then + duplicity $CLEANUP_COMMAND \ + --log-file /root/duplicity.log \ + --name $BACKUP_NAME \ + $BACKUP_DEST + fi +) 200>/var/lock/duplicity/.duplicity.lock diff --git a/restore.sh b/restore.sh index d31ec25..eec941f 100755 --- a/restore.sh +++ b/restore.sh @@ -1,11 +1,18 @@ #! /bin/bash set -e -duplicity restore \ - --force \ - --log-file /root/duplicity.log \ - --name $BACKUP_NAME \ - $OPT_ARGUMENTS \ - $@ \ - $BACKUP_DEST \ - $PATH_TO_BACKUP +( + if ! flock -x -w $FLOCK_WAIT 200 ; then + echo 'ERROR: Could not obtain lock. Exiting.' + exit 1 + fi + + duplicity restore \ + --force \ + --log-file /root/duplicity.log \ + --name $BACKUP_NAME \ + $OPT_ARGUMENTS \ + $@ \ + $BACKUP_DEST \ + $PATH_TO_BACKUP +) 200>/var/lock/duplicity/.duplicity.lock