1

It seems to me strange that there is no simple script that deletes oldest BTRFS snapshots after disk quota is reached. It was part of BTRFS tool family but it is not anymore.

Since I am not that good programmer in bash - does anyone know how to check for free disk space and if it reaches certain threshold, then to find which is oldest BTRFS snapshot and delete it? Thanks very much!

1 Answer 1

1

OK, no answer, so I hope my own solution will help:

#!/bin/bash

value=80  #Disk % threshold  - if disk full above, then oldest snapshots will be deleted until disk is below threshold

for i in 1 2 3 4 5 6 7 8 9
do
  echo Attempt: $i of 9
  echo Getting snapshot list...
  to=$(sudo btrfs subvol list /mnt/timeshift/backup/timeshift-btrfs/snapshots)
  search="snapshots"
  prefix=${to%%$search*}
  position=$(awk -v a="$to" -v b=$search 'BEGIN{print index(a,b)}')
  fn=$(echo $to | cut -c $(($position+10))-$(($position+28)))
  echo Oldest snapshot name: $fn 

  diskfull=$(df -hP / | awk '{print $5}' |tail -1|sed 's/%$//g')
  if [ $value -gt $diskfull ]; then
    echo Threshold $value% greater than Used disk $diskfull%
    echo should not delete any more snapshots
    echo Creating one more snapshot
    sudo timeshift --create --comments "automatic"
    exit 1
  else
    echo Threshold $value% is less than Used disk $diskfull%
    echo Deleting snapshot $fn...
    sudo timeshift --delete --snapshot "$fn"
    echo Waiting for changes to be written...
    sudo btrfs subvolume sync /
    echo Checking if disk space resolved...
    fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .