fhnw-tools/fhnw-ad-sync

82 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env sh
set -o errexit
mount_dir="/mnt/fhnw-share"
dest_dir="$HOME/documents/fhnw/ad"
excluded_files=".DS_Store|Thumbs.db"
sync_dirs=""
display_help() {
echo "Usage: $0 [option...] " >&2
echo
echo " -m, mount directory for the ad (must already exist) [default: $mount_dir]"
echo " -d, root destination directory [default: $dest_dir]"
echo " -e, list of excluded files (pipe separated) [default: $excluded_files]"
echo " -s, list of ad directories, relative to the mount directory that will be synced (pipe separated)"
echo " -h, display this help and exit"
echo
}
parse_args() {
while getopts ":hm:d:e:s:" opt; do
case $opt in
h)
display_help
exit 0
;;
m)
mount_dir=$OPTARG
;;
d)
dest_dir=$OPTARG
;;
e)
excluded_files=$OPTARG
;;
s)
sync_dirs=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
display_help
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
display_help
exit 1
;;
esac
done
}
cleanup() {
rm "$exclude_file"
}
trap cleanup EXIT
create_exclude_file() {
exclude_file="$(mktemp)"
oldIFS=$IFS
IFS='|'
for ex in $excluded_files; do
echo "$ex" >>"$exclude_file"
done
IFS=$oldIFS
}
sync_ad() {
echo "syncing ad..."
oldIFS=$IFS
IFS='|'
for s in $sync_dirs; do
rsync -avz --exclude-from "$exclude_file" --progress "$mount_dir/$s" "$dest_dir"
done
IFS=$oldIFS
}
parse_args "$@"
create_exclude_file
sync_ad