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 rw"
13 #TODO: change autoclean behaviour from auto_* to *.snap
14 # Autosnaps to keep. Counting/Considering *.snap only. There should be adate_*.snap snapshots only.
19 if [ -e /tmp/btrfs_cleaner.lock ]; then
20 echo "Lockfile found. Assuming another instance is running. exitting."
24 touch /tmp/btrfs_cleaner.lock
25 mount /mnt/btrfs &> /dev/null
28 # Keep current and last MONTH
29 for vol in $LASTLIST; do
30 CHECK=$(grep "`date +"%Y-%m-"`" amon_curr_${vol}.txt 2> /dev/null)
31 if [ "$CHECK" == "" ]; then
32 echo "Autosnap cleaner: Rotating last and current month for ${vol}"
33 if [ -e "amon_curr_${vol}.snp" -a -e "amon_curr_${vol}.txt" ]; then
34 if [ -e "amon_last_${vol}.snp" -a -e "amon_last_${vol}.txt" ]; then
35 mv "amon_last_${vol}.snp" "`cat amon_last_${vol}.txt`"
37 mv "amon_curr_${vol}.snp" "amon_last_${vol}.snp"
38 mv "amon_curr_${vol}.txt" "amon_last_${vol}.txt"
40 CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-"`'*_'${vol}'.snap' | sort | head -n 1)
41 if [ "$CANDIDATE" != "" ]; then
42 echo $CANDIDATE > amon_curr_${vol}.txt
43 mv $CANDIDATE amon_curr_${vol}.snp
48 # Keep current and last DAY
49 for vol in $LASTLIST; do
50 CHECK=$(grep "`date +"%Y-%m-%d"`" aday_curr_${vol}.txt 2> /dev/null)
51 if [ "$CHECK" == "" ]; then
52 echo "Autosnap cleaner: Rotating last and current day for ${vol}"
53 if [ -e "aday_curr_${vol}.snp" -a -e "aday_curr_${vol}.txt" ]; then
54 if [ -e "aday_last_${vol}.snp" -a -e "aday_last_${vol}.txt" ]; then
55 mv "aday_last_${vol}.snp" "`cat aday_last_${vol}.txt`"
57 mv "aday_curr_${vol}.snp" "aday_last_${vol}.snp"
58 mv "aday_curr_${vol}.txt" "aday_last_${vol}.txt"
60 CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-%d"`'*_'${vol}'.snap' | sort | head -n 1)
61 if [ "$CANDIDATE" != "" ]; then
62 echo $CANDIDATE > aday_curr_${vol}.txt
63 mv $CANDIDATE aday_curr_${vol}.snp
69 # CLEAN *.snap if needed (too many or low on space)
71 while [ $LOOPDEL -eq 2 ]; do
74 btrfs fi show inthdd &> /tmp/btrfs_fishow.txt
75 SPACESIZE="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?size ([0-9]+\.[0-9]*)GB .*?$~\1~g'`"
76 SPACEUSED="`grep 'bytes used' /tmp/btrfs_fishow.txt | sed -E 's~^.*?bytes used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`"
77 SPACEALLOC="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?GB used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`"
78 rm /tmp/btrfs_fishow.txt
80 SNAPCNT="`find . -maxdepth 1 -name 'auto_*' -o -name '*.snap' | wc -l`"
81 SPACEPERCENT="`python -c 'print int(100.0*'${SPACEUSED}'/'${SPACESIZE}')'`"
83 echo "Autosnap cleaner: Snap count min/curr/limit [${MINSNAPCNT}/${SNAPCNT}/${MAXSNAPCNT}]."
84 echo "Autosnap cleaner: Space used/alloc/size/limit [${SPACEUSED}/${SPACEALLOC}/${SPACESIZE}][${SPACEPERCENT}%/${MAXSPACEPERCENT}%]"
86 if [ $SNAPCNT -gt $MAXSNAPCNT -o $SPACEPERCENT -gt $MAXSPACEPERCENT ]; then
87 DELFILE="`find . -maxdepth 1 -name 'auto_*' | sort | head -n -${MINSNAPCNT} | head -n 1`"
88 if [ $SNAPCNT -gt $MINSNAPCNT -a "$DELFILE" != "" ]; then
89 btrfs subvolume delete "${DELFILE}" && LOOPDEL=2
92 if [ $LOOPDEL -eq 2 ]; then
97 umount /mnt/btrfs &> /dev/null
98 rm -rf /tmp/btrfs_cleaner.lock