From 6bf15b49a77237a549be136b5aa29daaa69cc2f4 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 31 May 2023 16:07:56 -0400 Subject: push-btrfs-scan: remove timer and conf file for blacklisted subvolumes --- src/btrfs-scan | 158 ------------------------------------------- src/push-btrfs-scan | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 158 deletions(-) delete mode 100755 src/btrfs-scan create mode 100755 src/push-btrfs-scan diff --git a/src/btrfs-scan b/src/btrfs-scan deleted file mode 100755 index 9213dfc..0000000 --- a/src/btrfs-scan +++ /dev/null @@ -1,158 +0,0 @@ -#!/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_blacklist() -{ - if is_our_snapshot "$path_fixed" - then - if [ -e "$conf_name" ] - then - printf '%20s %s\n' delete "$conf_name" >&2 - printf '%20s %s\n' '(snapshot) lose' "$path_fixed" >&2 - fi - return - - # Re-reading the blacklist file once per snapshot is not ideal, - # but is fine. - elif [ "$blacklist" -a -f "$blacklist" ] - then - while read pattern - do - if [[ $path_fixed == $pattern ]] - then - if [ -e "$conf_name" ] - then - printf '%20s %s\n' delete "$conf_name" >&2 - fi - printf '%20s %s\n' '(blacklist) lose' "$path_fixed" >&2 - return - fi - done < "$blacklist" - 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 <'/* ) 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/{p;q1}' -e '$q' + then + if [ -e "$conf_name" ] + then + printf '%20s %s\n' delete "$conf_name" >&2 + fi + printf '%20s %s\n' '(snapshot) 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' delete "$conf_name" >&2 + 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' delete "$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 <