Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Prevent multiple restic backups from running at once #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

glimberg
Copy link

I'm doing an initial backup of ~2.5TB which is far more than my home bandwidth can upload offsite in a 24 hour period. I noticed on the 2nd day of running, cron had launched a second restic backup process. Per comments in this issue thread in the Restic repo, restic/restic#519 (comment), it's not necessarily an issue to have multiple Restic backups of the same data going on at once, however it will waste some space.

Rather than wasting space on B2 and using additional system resources on a resource constrained system, I made the following modifications:

  • Moved backup.sh -> do_backup.sh
  • Create new backup.sh that uses flock to ensure only a single instance of do_backup.sh is launched, and therefore only a single restic backup is launched as well.

backup.sh wraps do_backup.sh via flock so that multiple `restic backup`s dont pile up on each other in cases where a backup can take longer than the period specified in cron
@csumpter
Copy link
Owner

csumpter commented Feb 28, 2020

Hello glimberg, thank you for the code and thoughts. I read through the various conversations around the issue on if to lock or not, when doing some sort of scheduled backup process on the restic repository GitHub issues. It would seem like adding a locking mechanism for our intended use would be appropriate here.

Did you consider adding the flock logic to the main backup code block?

exec {lock_fd}>/var/lock/backup || exit 1
flock -n "$lock_fd" || { log "[ERROR] Backup lockfile is in place." >&2; exit 1; }

restic backup /data ${RESTIC_BACKUP_ARGS} --tag="${RESTIC_TAG}" | tee -a "$logFile"
rc=$?
if [[ $rc == 0 ]]; then
    log "[INFO] Backup succeeded"
else
    log "[ERROR] Backup failed with status ${rc}"
    restic unlock
    copyErrorLog
    kill 1
fi
end=$(date +%s)
log "[INFO] Finished backup at $(date)"
showTime $((end-start))

Alternatively, wrapping the main block in a flock with the appropriate options. I do not think adding an entirely separate file here is needed, let me know your thoughts.

Thank you,
Cameron

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants