about summary refs log tree commit diff
path: root/rdumpfs
blob: 8c2e0fba25327a0519fa8071d220f30ee8c1c9d9 (plain) (blame)
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
#!/bin/bash
# rdumpfs - rsync-based dump file system backup tool
# Usage: rdumpfs [-f] [RSYNCOPT...] SRC [SRC...] DST
#   -f: force potentially dangerous operations
#
# Written by Christian Neukirchen <http://purl.org/net/chneukirchen>.
# rdumpfs is in the public domain.
#
# To the extent possible under law, the creator of this work has waived
# all copyright and related or neighboring rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/

fail() {
  echo "$0: $1" 1>&2
  exit 111
}

force=false
[[ "$1" = -f ]] && force=true && shift

(( $# < 2 )) && fail "too few arguments
Usage: rdumpfs [-f] [RSYNCOPT...] SRC [SRC...] DST"

src=("${@:1:$#-1}")
dst=${!#}

now=$(date +%Y%m%d)
dumps=($(rsync --no-h $dst/ | cut -c44- | grep '^[0-9]*$' | sort -nr))
last=${dumps[0]}

rsync_args=(-aHAX --stats --human-readable
            --out-format='%10l %n%L' --log-file-format='%10l %i %n%L'
            --filter='dir-merge /.rdumpfs' --filter='protect .rdumpfs.*.log')
rsync_args+=(${VARIANT:+--filter='dir-merge /.rdumpfs.'$VARIANT})

if [[ -z "$last" ]]; then
  $force || fail "no dump found, use -f on first dump."
else
  rsync_args+=(--link-dest=../$last)
fi

if [[ "$last" = "$now" ]]; then
  $force || fail "dump $now exists, use -f to overwrite/update."
  [[ -n "${dumps[1]}" ]] && rsync_args+=(--link-dest=../${dumps[1]})
  rsync_args+=(--delete-delay --delete-excluded)
fi

LOGFILE=$(mktemp -t .rdumpfs.XXXXXXXX.log)
rsync --log-file $LOGFILE "${rsync_args[@]}" "${src[@]}" "$dst/$now"
EC=$?
rsync $LOGFILE "$dst/$now/"
(( $EC )) || rm -f $LOGFILE
exit $EC