| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
# Test that src and dest trees are as identical as bup is capable of |
| 6 |
# making them. For now, use rsync -niaHAX ... |
| 7 |
|
| 8 |
usage() { |
| 9 |
cat <<EOF |
| 10 |
Usage: |
| 11 |
compare-trees [-c] [-x] [--] SOURCE DEST |
| 12 |
compare-trees --features |
| 13 |
compare-trees -h |
| 14 |
OPTIONS: |
| 15 |
-h |
| 16 |
Display help |
| 17 |
-c |
| 18 |
Check file content (default) |
| 19 |
-x |
| 20 |
Don't check file content (rely on size/timestamps, etc.) |
| 21 |
--times |
| 22 |
--no-times |
| 23 |
Check or don't check timestamps (checking is the default) |
| 24 |
--features |
| 25 |
Show enabled features |
| 26 |
-- |
| 27 |
Don't treat following arguments as options (e.g. compare-trees -- -c dest) |
| 28 |
EOF |
| 29 |
} |
| 30 |
|
| 31 |
show_features='' |
| 32 |
verify_content=" --checksum" |
| 33 |
verify_times=' --times' |
| 34 |
|
| 35 |
case "$#/${1:-}" in |
| 36 |
1/-h) usage; exit 0;; |
| 37 |
1/--features) show_features=yes; shift;; |
| 38 |
*) |
| 39 |
while test $# -gt 0; do |
| 40 |
case "$1" in |
| 41 |
-h|--features) usage 1>&2; exit 2;; |
| 42 |
-c) verify_content=" --checksum"; shift;; |
| 43 |
-x) verify_content=""; shift;; |
| 44 |
--times) verify_times=' --times'; shift;; |
| 45 |
--no-times) verify_times=''; shift;; |
| 46 |
--) shift; break;; |
| 47 |
[^-]*) break;; |
| 48 |
esac |
| 49 |
done |
| 50 |
esac |
| 51 |
|
| 52 |
if test $# -ne 2 -a -z "$show_features"; then |
| 53 |
usage 1>&2 |
| 54 |
exit 2 |
| 55 |
fi |
| 56 |
|
| 57 |
src="${1:-}" |
| 58 |
dest="${2:-}" |
| 59 |
|
| 60 |
rsync_opts="-rlpgoD" # --archive, without --times |
| 61 |
rsync_opts="$rsync_opts -niH --delete" |
| 62 |
rsync_opts="$rsync_opts$verify_content" |
| 63 |
rsync_opts="$rsync_opts$verify_times" |
| 64 |
|
| 65 |
comparing_acls='' |
| 66 |
rsync_version=$(rsync --version) |
| 67 |
if [[ ! "$rsync_version" =~ "ACLs" ]] || [[ "$rsync_version" =~ "no ACLs" ]]; then |
| 68 |
echo "Not comparing ACLs (not supported by available rsync)" 1>&2 |
| 69 |
else |
| 70 |
case "$OSTYPE" in |
| 71 |
cygwin|darwin|netbsd) |
| 72 |
echo "Not comparing ACLs (not yet supported on $OSTYPE)" 1>&2 |
| 73 |
;; |
| 74 |
*) |
| 75 |
comparing_acls=yes |
| 76 |
rsync_opts="$rsync_opts -A" |
| 77 |
;; |
| 78 |
esac |
| 79 |
fi |
| 80 |
|
| 81 |
xattrs_available='' |
| 82 |
if [[ ! "$rsync_version" =~ "xattrs" ]] || [[ "$rsync_version" =~ "no xattrs" ]]; then |
| 83 |
echo "Not comparing xattrs (not supported by available rsync)" 1>&2 |
| 84 |
else |
| 85 |
xattrs_available=yes |
| 86 |
fi |
| 87 |
|
| 88 |
if test "$show_features"; then |
| 89 |
echo "POSIX ACLs: ${comparing_acls:-no}" |
| 90 |
echo "Extended attributes (xattrs): ${xattrs_available:-no}" |
| 91 |
fi |
| 92 |
|
| 93 |
if test "$show_features"; then |
| 94 |
exit 0 |
| 95 |
fi |
| 96 |
|
| 97 |
tmpfile="$(mktemp /tmp/bup-test-XXXXXXX)" || exit $? |
| 98 |
trap "rm -rf '$tmpfile'" EXIT || exit $? |
| 99 |
|
| 100 |
# Even in dry-run mode, rsync may fail if -X is specified and the |
| 101 |
# filesystems don't support xattrs. |
| 102 |
|
| 103 |
if test "$xattrs_available"; then |
| 104 |
rsync $rsync_opts -X "$src" "$dest" > "$tmpfile" |
| 105 |
if test $? -ne 0; then |
| 106 |
# Try again without -X |
| 107 |
rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $? |
| 108 |
fi |
| 109 |
else |
| 110 |
rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $? |
| 111 |
fi |
| 112 |
|
| 113 |
if test $(wc -l < "$tmpfile") != 0; then |
| 114 |
echo "Differences between $src and $dest" 1>&2 |
| 115 |
cat "$tmpfile" |
| 116 |
exit 1 |
| 117 |
fi |
| 118 |
|
| 119 |
exit 0 |