如何自动删除最旧的 BTRFS 快照?

如何自动删除最旧的 BTRFS 快照?

我觉得很奇怪,没有简单的脚本可以在达到磁盘配额后删除最旧的 BTRFS 快照。它曾经是 BTRFS 工具系列的一部分,但现在不再是了。

由于我不是那么优秀的 bash 程序员 - 有谁知道如何检查可用磁盘空间,如果达到某个阈值,那么找出最旧的 BTRFS 快照并删除它?非常感谢!

答案1

好的,没有答案,所以我希望我自己的解决方案能够有所帮助:

#!/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

相关内容