summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@shackspace.de>2015-01-11 18:37:01 +0100
committertv <tv@shackspace.de>2015-01-11 18:37:01 +0100
commit09ecd36890b1b27214f5296134941177f07f9bfa (patch)
treeba04dbad1c1a30b413100e81a863a5e0efbdc13f
-rwxr-xr-xmake-snapshot75
1 files changed, 75 insertions, 0 deletions
diff --git a/make-snapshot b/make-snapshot
new file mode 100755
index 0000000..d677c40
--- /dev/null
+++ b/make-snapshot
@@ -0,0 +1,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 \{\} \;)"