| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -u |
| 4 |
|
| 5 |
usage() { |
| 6 |
cat <<EOF |
| 7 |
Usage: sync-tree [-h] [-c] [-x] SOURCE/ DEST/ |
| 8 |
Make the DEST tree match SOURCE as closely as possible |
| 9 |
OPTIONS: |
| 10 |
-h |
| 11 |
Display help |
| 12 |
EOF |
| 13 |
} |
| 14 |
|
| 15 |
while getopts "h" OPTION |
| 16 |
do |
| 17 |
case "$OPTION" in |
| 18 |
h) usage; exit 0;; |
| 19 |
?) usage 1>&2; exit 1;; |
| 20 |
esac |
| 21 |
done |
| 22 |
|
| 23 |
shift $(($OPTIND - 1)) || exit $? |
| 24 |
|
| 25 |
if ! test $# -eq 2 |
| 26 |
then |
| 27 |
usage 1>&2 |
| 28 |
exit 1 |
| 29 |
fi |
| 30 |
|
| 31 |
src="$1" |
| 32 |
dest="$2" |
| 33 |
|
| 34 |
rsync_opts="-aH --delete" |
| 35 |
|
| 36 |
rsync_version=$(rsync --version) |
| 37 |
if [[ ! "$rsync_version" =~ "ACLs" ]] || [[ "$rsync_version" =~ "no ACLs" ]]; then |
| 38 |
echo "Not syncing ACLs (not supported by available rsync)" 1>&2 |
| 39 |
else |
| 40 |
case $OSTYPE in |
| 41 |
cygwin|darwin|netbsd) |
| 42 |
echo "Not syncing ACLs (not yet supported on $OSTYPE)" 1>&2 |
| 43 |
;; |
| 44 |
*) |
| 45 |
rsync_opts="$rsync_opts -A" |
| 46 |
;; |
| 47 |
esac |
| 48 |
fi |
| 49 |
|
| 50 |
xattrs_available='' |
| 51 |
if [[ ! "$rsync_version" =~ "xattrs" ]] || [[ "$rsync_version" =~ "no xattrs" ]]; then |
| 52 |
echo "Not syncing xattrs (not supported by available rsync)" 1>&2 |
| 53 |
else |
| 54 |
xattrs_available=yes |
| 55 |
fi |
| 56 |
|
| 57 |
|
| 58 |
# rsync may fail if -X is specified and the filesystems don't support |
| 59 |
# xattrs. |
| 60 |
|
| 61 |
if test "$xattrs_available"; then |
| 62 |
rsync $rsync_opts -X "$src" "$dest" |
| 63 |
if test $? -ne 0; then |
| 64 |
# Try again without -X |
| 65 |
exec rsync $rsync_opts "$src" "$dest" |
| 66 |
fi |
| 67 |
else |
| 68 |
exec rsync $rsync_opts "$src" "$dest" |
| 69 |
fi |