#!/bin/bash set -e -o pipefail confdir=/etc/btrfs blacklist=$confdir/blacklist.txt with_subvolumes() { btrfs subvolume list / -a | while read _ id _ gen _ _ toplevel _ path do "$@" done } fixup_path() { if ! [ "$root" ] then case "$path" in ''/* ) root=${path#*/} ;; * ) exit 1 ;; esac fi case "$path" in ''/"$root"/* ) path_fixed=${path#*/$root} ;; "$root"/* ) path_fixed=${path#$root} ;; * ) return ;; esac "$@" } is_our_snapshot() { case "$1" in */.snapshot~* ) true ;; *) false ;; esac } filter_readonly() { if btrfs subvolume show -r "$id" / | sed -Ene '/^\tFlags:.*\breadonly\b/q' -e '$q1' then if [ -e "$conf_name" ] then printf '%20s %s\n' rm "$conf_name" >&2 rm "$conf_name" fi printf '%20s %s\n' '(readonly) lose' "$path_fixed" >&2 return fi "$@" } filter_snapshot_filename() { if is_our_snapshot "$path_fixed" then if [ -e "$conf_name" ] then printf '%20s %s\n' rm "$conf_name" >&2 rm "$conf_name" fi printf '%20s %s\n' '(snapshot) lose' "$path_fixed" >&2 return fi "$@" } systemctl_disable_quiet() { ! systemctl is-enabled "$@" 2>/dev/null || systemctl disable "$@" } timer_prefix=/etc/systemd/system/default.target.wants/push-btrfs@borges unset blacklist_patterns declare -a blacklist_patterns filter_blacklist() { if [ "$blacklist" -a -f "$blacklist" ] then if ! [ -v blacklist_patterns ] then readarray -t blacklist_patterns < "$blacklist" fi for pattern in "${blacklist_patterns[@]}" do if [[ $path_fixed == $pattern ]] then printf '%20s %s\n' '(blacklist) lose' "$path_fixed" >&2 if [ -e "$conf_name" ] then printf '%20s %s\n' rm "$conf_name" >&2 rm "$conf_name" fi timer=$destination_host$(systemd-escape "$path_fixed").timer systemctl_disable_quiet "$timer" return fi done fi "$@" } compute_paths() { source=$path_fixed source_escaped=$(systemd-escape "$source") conf_dir=/etc/btrfs/remotes conf_name="$conf_dir"/$destination_host$source_escaped.json "$@" } mirror() { if [ -e "$conf_name" ] then return fi set -- \ jq -c -n '{source: $source, destination: $destination}' \ --arg destination "$destination$source_escaped" \ --arg source "$source" if [ ! "$NO_ACT" ] then conf_temp=$conf_name~$(date -Ins) { "$@" > "$conf_temp" mv -T -- "$conf_temp" "$conf_name" printf '%20s %s\n' 'add' "$path_fixed" >&2 } || { rm -- "$conf_temp" false } fi if [ "$ONLY_ONCE" ] then exit 0 fi } usage() { cat <