summaryrefslogtreecommitdiffstats
path: root/make-snapshot
blob: d677c40a1e2c81046972aaa599e43e4af6c513d6 (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
#! /bin/sh
#
# make-snapshot {source-directory} {snapshot-directory}
#
set -euf

source="$1"
snapshot="$2"

echo "source directory is $source"
echo "snapshot directory is $snapshot"

# TODO check whether $snapshot is a snapshot directory
ls "$snapshot/by-date" >/dev/null

# Set $link_dest to the latest snapshot, if there is one.
# pre:
#   $snapshot/by-date is a directory that contains only directories
#   where names are produced by $(date +%Y-%d-%mT%H:%M:%S) or
#   start with a . (and thus are not listed by ls).
# post:
#   $link_dest is set iff there is a latest snapshot
#   $link_dest is set to the filename of the latest snapshot
# 2012-11-08 tv TODO check pre-condition
latest="$(ls -r "$snapshot/by-date" | grep ^[0-9] | head -n 1)"
if test -n "$latest"; then
  link_dest="$snapshot/by-date/$latest"
  echo latest snapshot is $link_dest
fi

timestamp="$(date +%Y-%m-%dT%H:%M:%S)"
complete="$snapshot/by-date/$timestamp"
partial="$snapshot/by-date/.$timestamp.part"

# TODO incooperate into ach's version
if present_partials="$(ls -ar "$snapshot/by-date" | grep '^\..*\.part$')"; then
case $(echo "$present_partials" | wc -l) in
  (0) : ;; # nothing to do
  (1)
    echo "found partial snapshot: $present_partials"
    printf "do you want to continue [y/N] "
    read answer
    case "$answer" in
      (y|Y) partial="$snapshot/by-date/$present_partials";;
      (*)
        echo "abort"; exit 23
        ;;
    esac
    ;;
  (*)
    echo "found too many partial snapshots:"
    echo "$present_partials"
    echo "abort"; exit 23
    ;;
esac
fi

# sync $source to $partial
if test -d "${link_dest-}"; then
  cp -al "$link_dest" "$partial"
fi
rsync -v \
    -aAXF \
    --delete \
    "$source/" "$partial"
sync

mv "$partial" "$complete"
sync

# mark this snapshot as current
rm -f "$snapshot/current"
ln -snf "by-date/$timestamp" "$snapshot/current"

echo "current snapshot is $(find "$snapshot/current" -exec readlink -f \{\} \;)"