local/bin/btrfs-snap-all: Add preview option.

master
Pierre Neidhardt 2019-10-16 15:53:11 +02:00
parent f4e4f65acf
commit fa93bc5b10
1 changed files with 40 additions and 17 deletions

View File

@ -6,36 +6,59 @@ Usage: ${0##*/}
Snapshot all Btrfs subvolumes older than 1 day to a read-only '.snapshots/SUBVOLUME/DATE'.
Options:
-n: No preview.
EOF
}
if [ $# -ne 0 ]; then
usage
exit 1
fi
OPT_PREVIEW=true
while getopts ":hn" opt; do
case $opt in
h)
usage
exit ;;
n)
OPT_PREVIEW=false ;;
\?)
usage
exit 1 ;;
esac
done
while IFS= read -r fs; do
shift $(($OPTIND - 1))
snap () { # $1=fs $2=subvol
local fs=$1
local subvol=$2
local choice=N
local last=$(find "$fs/.snapshots/$subvol" -maxdepth 1 | tail -1)
$OPT_PREVIEW && rmirror "$fs/$subvol" "$last"
echo -n "==> Snapshot '$fs/${subvol}'? (y/N) "
read -r choice
echo ""
case "$choice" in
Y|y)
btrfs-snap "$fs/$subvol" ;;
esac
}
for fs in $(findmnt -t btrfs -n --output TARGET); do
for subvol in private public; do
last=$(ls -1 .snapshots/"$subvol" | tail -1 | sed 's/_/ /')
last=$(ls -1 "$fs"/.snapshots/"$subvol" | tail -1 | sed 's/_/ /')
if [ -z "$last" ]; then
continue
fi
last_date=$(date --date="$last" +%s)
now=$(date +%s)
if [ $(($now - $last_date)) -gt 86400 ]; then
choice=N
echo -n "==> Snapshot '$fs/${subvol}'? (y/N) "
read -r choice
case "$choice" in
Y|y)
btrfs-snap "$fs/$subvol" ;;
esac
unset choice
snap "$fs" "$subvol"
fi
unset now
unset last_date
unset last
done
done <<EOF
$(findmnt -t btrfs -n --output TARGET)
EOF
done