1
1
Fork 0

better output messages in btrfs-snapshots.sh, getting close to a acceptable state for v0.2.0

This commit is contained in:
david 2023-10-30 09:30:05 +01:00
parent c31fd02092
commit 5ca2729bff
1 changed files with 28 additions and 15 deletions

View File

@ -46,6 +46,7 @@ BTRFS_OPTIONS="-q"
BTRFS_COMMAND="$BTRFS_BINARY $BTRFS_OPTIONS"
# How many snapshots should be kept?
# Can be overridden with an option flag (-s)
SNAPSHOTS=128
@ -55,9 +56,9 @@ SNAPSHOTS=128
# Print help information
function print_help() {
printf "%s\n\n%s\n%b\n\n%s\n %-15s %s\n %-15s %s\n %-15s %s\n %-15s %s\n %-15s %s\n" \
printf "%s\n\n%s\n%b\n\n%s\n %-21s %s\n %-21s %s\n %-21s %s\n %-21s %s\n %-21s %s\n" \
"$DESCRIPTION" "Usage:" "$LIB_BOLD$EXECUTABLE <options> <subvolume>$LIB_CLEAR" "Options:" \
"-s, --snapshots <number>" "Override how many snapshots to keep (default: 128)" \
"-s, --snapshots <int>" "Override how many snapshots to keep (default: 128)" \
"-h, --help" "Print help screen and exit" \
"-i, --info" "Print script information and exit" \
"-v, --verbose" "More verbose output" \
@ -118,19 +119,19 @@ while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
lib_print "!Input for --snapshots must be an intager"
exit 1
fi
;;
;;
-h|--help)
H=1
;;
;;
-i|--info)
I=1
;;
;;
-v|--verbose)
V=1
;;
;;
-q|--quiet)
Q=1
;;
;;
*)
lib_print "!Unknown option [$1]"
lib_print "Try --help or -h for available options"
@ -164,28 +165,40 @@ else
mkdir "$SNAP_FOLDER"
fi
if [[ -d "$SNAP_FOLDER/$SNAP_NAME" ]]; then
lib_print "?Skipping snapshot because it already exists. [$SNAP_NAME]"
lib_print "?Skipping snapshot because it already exists. [$SNAP_FOLDER/$SNAP_NAME]"
else
# Take the snapshot
$BTRFS_COMMAND subvolume snapshot -r "${1%/}" "$SNAP_FOLDER/$SNAP_NAME"
#TODO: handle failed snapshot creations
if $($BTRFS_COMMAND subvolume snapshot -r "${1%/}" "$SNAP_FOLDER/$SNAP_NAME"); then
lib_print "Created new snapshot [$SNAP_FOLDER/$SNAP_NAME]"
else
lib_print "!Error occured while creating new snapshot [$SNAP_FOLDER/$SNAP_NAME]"
exit 1
fi
if [ -h "$SNAP_FOLDER/latest" ]; then
rm "$SNAP_FOLDER/latest"
fi
ln -sf "$SNAP_FOLDER/$SNAP_NAME" "$SNAP_FOLDER/latest"
lib_print "?Relinked latest to new snapshot [$SNAP_FOLDER/$SNAP_NAME]"
# Delete old snapshots
SNAPS=$(ls -r $SNAP_FOLDER | grep $SNAP_PREFIX)
SNAPS=($(ls -r $SNAP_FOLDER | grep $SNAP_PREFIX))
lib_print "?Snapshot retention is set to: $SNAPSHOTS"
lib_print "?Existing snapshots found: ${#SNAPS[@]}"
i=0
for SNAP in $SNAPS; do
for SNAP in ${SNAPS[@]}; do
if [[ $i -ge $SNAPSHOTS ]]; then
#TODO: handle failed snapshot deletions
$BTRFS_COMMAND subvolume delete "$SNAP_FOLDER/$SNAP"
lib_print "Deleted old snapshot [$SNAP_FOLDER/$SNAP]"
if $($BTRFS_COMMAND subvolume delete "$SNAP_FOLDER/$SNAP"); then
lib_print "Deleted old snapshot [$SNAP_FOLDER/$SNAP]"
else
lib_print "!Error occured while deleting old snapshot [$SNAP_FOLDER/$SNAP]"
exit 1
fi
fi
i=$((i+1))
done
fi
else
lib_print "!Given path does not appear to be a Btrfs subvolume [$1]"
lib_print "!Input does not appear to be a Btrfs subvolume [$1]"
exit 1
fi
fi