#!/bin/bash -l set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR sv() { echo "$@"; "$@"; } date=unison-$(date "+%Y:%m:%d") for mountp in /mnt/root/q /mnt/iroot/i; do [[ -e $mountp ]] || continue date_path=${mountp%/*}/$date todays=( $date_path-* ) IFS=$'\n' todays=($(sort -Vr <<<"${todays[*]}")); unset IFS if [[ -e ${todays[0]} ]]; then snapshot=$date_path-$(( ${todays[0]##*-} + 1 )) else snapshot=$date_path-1 fi sv s btrfs subvolume snapshot -r $mountp $snapshot existing=( ${mountp%/*}/unison-* ) IFS=$'\n' existing=($(sort -Vr <<<"${existing[*]}")); unset IFS # keep 10 unison snapshots around if (( ${#existing[@]} > 10 )); then sv s btrfs subvolume delete ${existing[@]:10} fi done