summaryrefslogtreecommitdiff
path: root/mark-as-expendable-dir.in
blob: e615d3cedf25fc3e032f9a21125b05e845cae2a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

set -e

verwendung() {
  >&2 echo 'mark-as-expendable-dir creates CACHEDIR.TAG and .rsync-filter files to hide directory from backups by "tar --exclude-caches" and "rsync -F".'
  >&2 echo ''
  >&2 echo 'Usage: mark-as-expendable-dir [OPTIONS] [DIRECTORIES]'
  >&2 echo '  -c,--caller=caller  name of caller to put into comment of CACHEDIR.TAG'
  >&2 echo \
'#HELPTEXT#           #'
  exit 1
}

eval set -- "$(getopt -o c: --long caller:,help,version -n "$(basename "$0")" -- "$@" || echo verwendung)"

caller="$(ps -o comm= $PPID)"

while true
do
  case "$1" in
    -c|--caller)
      shift
      caller="$1"
    ;;
    --help)
      verwendung 0
      ;;
    --version)
      echo '#VERSION#'
      exit 0
      ;;
    --)
      shift
      break
      ;;
    *)
      >&2 echo "FEHLER: Verstehe Option \"$1\" doch nicht! Ich beende."
      verwendung
      ;;
  esac
  shift
done

if [ $# -eq 0 ]
then
  >&2 echo 'No directories given - was this intented?'
  verwendung
fi

for dir in "$@"
do
  if [ ! -d "${dir}" ]
  then
    >&2 echo "Argument '${dir}' is not a directory."
    verwendung
  fi
done

for dir in "$@"
do
  {
    printf 'Signature: '
    printf '.IsCacheDirectory' | \
      md5sum - | \
      cut -d' ' -f 1
    printf '# This file is a cache directory tag created by %s for %s.\n' \
      "$(basename "$0")" \
      "${caller}"
    printf '# For information about cache directory tags, see:\n'
    printf '#      http://www.brynosaurus.com/cachedir/\n'
  } > "${dir}/CACHEDIR.TAG"
  {
    printf '+ .rsync-filter\n'
    printf -- '- *\n'
  } > "${dir}/.rsync-filter"
  touch "${dir}/.nobackup"
done