minor fix and add automatic updates
[distro-setup] / unison-snapshot
1 #!/bin/bash -l
2 set -eE -o pipefail
3 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4
5 sv() { echo "$@"; "$@"; }
6
7 date=unison-$(date "+%Y:%m:%d")
8 for mountp in /mnt/root/q /mnt/iroot/i; do
9 [[ -e $mountp ]] || continue
10 date_path=${mountp%/*}/$date
11 todays=( $date_path-* )
12 IFS=$'\n' todays=($(sort -Vr <<<"${todays[*]}")); unset IFS
13 if [[ -e ${todays[0]} ]]; then
14 snapshot=$date_path-$(( ${todays[0]##*-} + 1 ))
15 else
16 snapshot=$date_path-1
17 fi
18 sv s btrfs subvolume snapshot -r $mountp $snapshot
19 existing=( ${mountp%/*}/unison-* )
20 IFS=$'\n' existing=($(sort -Vr <<<"${existing[*]}")); unset IFS
21 # keep 10 unison snapshots around
22 if (( ${#existing[@]} > 10 )); then
23 sv s btrfs subvolume delete ${existing[@]:10}
24 fi
25 done