Skip to content

Instantly share code, notes, and snippets.

@medington
Forked from necolas/rsync_backup
Last active December 28, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medington/7514609 to your computer and use it in GitHub Desktop.
Save medington/7514609 to your computer and use it in GitHub Desktop.
Script to clone / backup a directory excluding certain files that may not be of interest for pure archival backups that aren't intended to serve as full fidelity backups of the original.
#!/bin/sh
# Script to clone / backup a directory using rsync excluding certain files that may not be of
# interest for pure archival backups that aren't intended be full fidelity. The intention is
# that the exclude file will be populated with cached data or uninteresting metadata.
# Use sudo to call this script if you need to copy directories where you don't have permissions.
# rsync options:
# -a archive mode; equals -rlptgoD (no -H,-A,-X)
# -x don't cross device boundaries (ignore mounted volumes)
# -v increase verbosity
# --sparse handle sparse files efficiently
# --progress show progress during transfer
# --stats give some file-transfer stats
# --human-readable output numbers in a human-readable format
# --delete deletes any files that have been deleted locally
# --exclude-from reference a list of files to exclude (uses rsync_exclude.txt)
# options NOT included (needed for a working backups only)
# -E preserve executability and resource forks (._* files)
SRC="$1"
DST="$2"
RSYNC_OPTS="-axv --sparse --progress --stats --human-readable --delete"
#
# In order to load the exclude file list we get directory of the shell script resolving symlinks
# as needed. We do this in case the script is symlinked into someplace like /usr/local/bin
# so we don't have to put the exclude file in the bin directory too.
# Credit: https://gist.github.com/sourcerer-mike/7395428
#
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" #
done
PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Define the exclude variable only if we find the exclude file
if [ -f "$PROG_DIR/rsync_excludes.txt" ]; then
RSYNC_EXCL="--exclude-from=$PROG_DIR/rsync_excludes.txt"
fi
echo "\nStart directory copy: $SRC ==> $DST\n"
rsync $RSYNC_OPTS "$RSYNC_EXCL" "$SRC" "$DST"
echo "\nEnd directory backup\a"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment