diff --git a/btrfs-snapshots.sh b/btrfs-snapshots.sh index 296a2eb..f15dd2e 100755 --- a/btrfs-snapshots.sh +++ b/btrfs-snapshots.sh @@ -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 $LIB_CLEAR" "Options:" \ - "-s, --snapshots " "Override how many snapshots to keep (default: 128)" \ + "-s, --snapshots " "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