Skip to content

Instantly share code, notes, and snippets.

@star26bsd
Last active December 29, 2018 11:10
Show Gist options
  • Save star26bsd/2563cb0b8be61a60076c7340b1a57433 to your computer and use it in GitHub Desktop.
Save star26bsd/2563cb0b8be61a60076c7340b1a57433 to your computer and use it in GitHub Desktop.
Convenience script for rsync - to check for availability of a locally mounted network share and mirror files one way using rsync.
#!/bin/bash
#
# Copyright (c) 2018 Stephan Rickauer <stephan@rickauer.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Convenience script for rsync - to check for availability of a locally
# mounted network share and mirror files one way using rsync. Supposed to
# run on Mac OS X only, tested on Mojave. Requires rsync >= 3.
# Home directory of the user.
stem='/Users/username'
# Sub directories to mirror (replace with $stem/* for all).
sources=(
$stem/Documents
$stem/Desktop
$stem/Downloads
$stem/Applications
$stem/Software
$stem/Movies
$stem/Public
$stem/Pictures
$stem/Music
$stem/Applications
$stem/.ssh
)
# Local mount point of network share (destination share).
share='/Volumes/Your-Share'
# Remote destination sub folder to mirror into. Set to "$share" for 'none'.
dest="$share/rsync"
# On Mac, typically not required to change anything beyond here
df='/bin/df'
awk='/usr/bin/awk'
rsync='/usr/local/bin/rsync'
mount='/sbin/mount'
bins=($df $awk $rsync $mount)
# Functions go first
function check_binaries() {
for bin in ${bins[*]}; do
[[ -x $bin ]] || { echo "$bin does not execute!"; exit 1; }
done
}
function check_rsync_version() {
# rsync versions < 3 lack support for UTF8 conversion (--iconv).
# see https://www.kigmbh.com/2011/04/rsync_delete_umlaut_problem/
rsync_version=`$rsync --version | $awk -F'[ .]' '/version/{print $4}'`
if [ "$rsync_version" -lt '3' ]; then
echo "This $rsync version lacks iconv support for UTF8! Consider updating."
exit 1
fi
}
function check_sources() {
for source in ${sources[*]}; do
[[ -d $source ]] || { echo "$source not found!"; exit 1; }
done
}
function check_destination() {
# Check if given share is listed under mounted file systems.
$mount | grep $share >/dev/null
if [ $? == '1' ]; then
echo "$share does not seem to be mounted, mount share manually!"
exit 1
fi
# Verify if mount point directory was created during mount of network share.
[[ -d $dest ]] || { echo "Directory $dest missing, try to mount share!"; exit 1; }
# Require at least 100 MB free disk space on remote share.
available_space=`$df -k "$share" | $awk '/[0-9]%/{print $(NF-5)}'`
if [ "$available_space" -lt '100000' ]; then
echo "$available_space is not enough!"
exit 1
fi
}
function mirror_files() {
$rsync -avz --iconv=UTF8,UTF8-MAC --delete --exclude {.cache,.Trash} ${sources[*]} $dest/
}
# Main
check_binaries
check_rsync_version
check_sources
check_destination
mirror_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment