Created
November 30, 2016 13:22
-
-
Save JoeyBurzynski/f7fd0c71dce32c1db652143404fd401d to your computer and use it in GitHub Desktop.
Quick and dirty backups using rdiff-backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Quick and dirty backups using rdiff-backup | |
| # Usage: /path/to/local-backup --source /mnt/source \ | |
| # --destination /mnt/source-mirror \ | |
| # --lock-file /var/lock/local-backup/job-name \ | |
| # --age 4W | |
| eval set -- "$(getopt -o "adls:" --long "age:,destination:,lock-file:,mount-point:,source:" -- "$@")" | |
| while true; do | |
| case "$1" in | |
| -a|--age ) MAXAGE="$2" ; shift 2 ;; | |
| -d|--destination) TARGETMNTDIR="$2" ; shift 2 ;; | |
| -l|--lock-file ) LOCKFILE="$2" ; shift 2 ;; | |
| -s|--source ) SRCDIR="$2" ; shift 2 ;; | |
| * ) break ;; | |
| esac | |
| done | |
| TARGETDIR="$TARGETMNTDIR/backup" | |
| TARGETRESERVED="$TARGETMNTDIR/reserved" | |
| if [ "$USER" != "root" ]; then | |
| echo "This script must be executed as root; aborting!" >&2 | |
| exit | |
| fi | |
| if [ -f "$LOCKFILE" ]; then | |
| echo "Lock file detected; aborting" >&2 | |
| echo "This usually means the previous job crashed. Ensure no jobs are active, remove the lock file and retry." >&2 | |
| exit | |
| fi | |
| if ! rdiff-backup --version &> /dev/null; then | |
| echo "rdiff-backup is not on PATH; aborting" >&2 | |
| echo "Is rdiff-backup installed?" >&2 | |
| exit | |
| fi | |
| touch "$LOCKFILE" | |
| if [ "$(ls -A $TARGETMNTDIR)" ]; then | |
| echo "$TARGETMNTDIR contains files; refusing to proceed mounting." >&2 | |
| echo "Maybe a prior backup run left the target device mounted?" >&2 | |
| exit | |
| fi | |
| mount "$TARGETMNTDIR" | |
| if ! mountpoint -q "$TARGETMNTDIR"; then | |
| echo "Failed to mount $TARGETMNTDIR; aborting" >&2 | |
| echo "Does the mount point appear in /etc/fstab?" >&2 | |
| exit | |
| fi | |
| if [ ! -f "$TARGETRESERVED" ]; then | |
| echo -n "Reserve file not present, writing... " | |
| dd if=/dev/zero of="$TARGETRESERVED" bs=1M count=1000 | |
| echo "done." | |
| fi | |
| mkdir -p "$TARGETDIR" | |
| echo "Removing older files..." | |
| rdiff-backup -v 9 --remove-older-than "$MAXAGE" --force "$TARGETDIR" | |
| echo "done." | |
| echo "Performing the backup..." | |
| rdiff-backup -v 9 "$SRCDIR" "$TARGETDIR" | |
| echo "done." | |
| umount "$TARGETMNTDIR" | |
| if mountpoint -q "$TARGETMNTDIR"; then | |
| echo "Attempted to unmount $TARGETMNTDIR butit still appears to be a mountpount!" >&2 | |
| echo "Refusing to remove the directory automatically." >&2 | |
| exit | |
| fi | |
| rm -f "$LOCKFILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment