3 # Clean old BTRFS snapshots
4 # */15 * * * * /root/somescripts/btrfs/cleanup.sh &> /dev/null
6 # This script will keep current/last day/month snapshots.
7 # This script will update current/last_day/week/month snapshots and will delete snapshots if needed (too many or low on space)
9 #set volumes to do curr/last day/month for.
10 LASTLIST="root home homeroot var"
12 # Autosnaps to keep. Counting/Considering *.snap only. There should be adate_*.snap snapshots only.
17 echo "***START: `date`***"
18 if [ -e /tmp/btrfs_cleaner.lock ]; then
19 echo "Lockfile found. Assuming another instance is running. exitting."
23 touch /tmp/btrfs_cleaner.lock
24 mount /mnt/btrfs &> /dev/null
27 # Keep current and last MONTH
28 for vol in $LASTLIST; do
29 CHECK=$(grep "`date +"%Y-%m-"`" amon_curr_${vol}.txt 2> /dev/null)
30 if [ "$CHECK" == "" ]; then
31 echo "Autosnap cleaner: Rotating last and current month for ${vol}"
32 if [ -e "amon_curr_${vol}.snp" -a -e "amon_curr_${vol}.txt" ]; then
33 if [ -e "amon_last_${vol}.snp" -a -e "amon_last_${vol}.txt" ]; then
34 mv "amon_last_${vol}.snp" "`cat amon_last_${vol}.txt`"
36 mv "amon_curr_${vol}.snp" "amon_last_${vol}.snp"
37 mv "amon_curr_${vol}.txt" "amon_last_${vol}.txt"
39 CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-"`'*_'${vol}'.snap' | sort | head -n 1)
40 if [ "$CANDIDATE" != "" ]; then
41 echo $CANDIDATE > amon_curr_${vol}.txt
42 mv $CANDIDATE amon_curr_${vol}.snp
47 # Keep current and last DAY
48 for vol in $LASTLIST; do
49 CHECK=$(grep "`date +"%Y-%m-%d"`" aday_curr_${vol}.txt 2> /dev/null)
50 if [ "$CHECK" == "" ]; then
51 echo "Autosnap cleaner: Rotating last and current day for ${vol}"
52 if [ -e "aday_curr_${vol}.snp" -a -e "aday_curr_${vol}.txt" ]; then
53 if [ -e "aday_last_${vol}.snp" -a -e "aday_last_${vol}.txt" ]; then
54 mv "aday_last_${vol}.snp" "`cat aday_last_${vol}.txt`"
56 mv "aday_curr_${vol}.snp" "aday_last_${vol}.snp"
57 mv "aday_curr_${vol}.txt" "aday_last_${vol}.txt"
59 CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-%d"`'*_'${vol}'.snap' | sort | head -n 1)
60 if [ "$CANDIDATE" != "" ]; then
61 echo $CANDIDATE > aday_curr_${vol}.txt
62 mv $CANDIDATE aday_curr_${vol}.snp
68 # CLEAN *.snap if needed (too many or low on space)
70 while [ $LOOPDEL -eq 2 ]; do
73 btrfs fi show inthdd &> /tmp/btrfs_fishow.txt
74 SPACESIZE="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?size ([0-9]+\.[0-9]*)GB .*?$~\1~g'`"
75 SPACEUSED="`grep 'bytes used' /tmp/btrfs_fishow.txt | sed -E 's~^.*?bytes used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`"
76 SPACEALLOC="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?GB used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`"
77 rm /tmp/btrfs_fishow.txt
79 SNAPCNT="`find . -maxdepth 1 -name '*.snap' | wc -l`"
80 SPACEPERCENT="`python -c 'print int(100.0*'${SPACEUSED}'/'${SPACESIZE}')'`"
82 echo "Autosnap cleaner: Snap count min/curr/limit [${MINSNAPCNT}/${SNAPCNT}/${MAXSNAPCNT}]."
83 echo "Autosnap cleaner: Space used/alloc/size/limit [${SPACEUSED}/${SPACEALLOC}/${SPACESIZE}][${SPACEPERCENT}%/${MAXSPACEPERCENT}%]"
85 if [ $SNAPCNT -gt $MAXSNAPCNT -o $SPACEPERCENT -gt $MAXSPACEPERCENT ]; then
86 DELFILE="`find . -maxdepth 1 -name '*.snap' | sort | head -n -${MINSNAPCNT} | head -n 1`"
87 if [ $SNAPCNT -gt $MINSNAPCNT -a "$DELFILE" != "" ]; then
88 btrfs subvolume delete "${DELFILE}" && LOOPDEL=2
91 if [ $LOOPDEL -eq 2 ]; then
96 umount /mnt/btrfs &> /dev/null
97 rm -rf /tmp/btrfs_cleaner.lock
98 echo "***END: `date`***"