]> git.somenet.org - root/btrfs.git/blob - cleaner.sh
some changes, added timestamping, ...
[root/btrfs.git] / cleaner.sh
1 #!/bin/bash
2
3 # Clean old BTRFS snapshots
4 # */15 * * * * /root/somescripts/btrfs/cleanup.sh &> /dev/null
5 #
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)
8
9 #set volumes to do curr/last day/month for.
10 LASTLIST="root home homeroot var"
11
12 # Autosnaps to keep. Counting/Considering *.snap only. There should be adate_*.snap snapshots only.
13 let MINSNAPCNT=4*5
14 let MAXSNAPCNT=4*200
15 MAXSPACEPERCENT=90
16
17 echo "***START: `date`***"
18 if [ -e /tmp/btrfs_cleaner.lock ]; then
19   echo "Lockfile found. Assuming another instance is running. exitting."
20   exit 1
21 fi
22
23 touch /tmp/btrfs_cleaner.lock
24 mount /mnt/btrfs &> /dev/null
25 cd /mnt/btrfs
26
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`"
35       fi
36       mv "amon_curr_${vol}.snp" "amon_last_${vol}.snp"
37       mv "amon_curr_${vol}.txt" "amon_last_${vol}.txt"
38     fi
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
43     fi
44   fi
45 done
46
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`"
55       fi
56       mv "aday_curr_${vol}.snp" "aday_last_${vol}.snp"
57       mv "aday_curr_${vol}.txt" "aday_last_${vol}.txt"
58     fi
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
63     fi
64   fi
65 done
66
67
68 # CLEAN *.snap if needed (too many or low on space)
69 LOOPDEL=2
70 while [ $LOOPDEL -eq 2 ]; do
71   LOOPDEL=1
72
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
78
79   SNAPCNT="`find . -maxdepth 1 -name '*.snap' | wc -l`"
80   SPACEPERCENT="`python -c 'print int(100.0*'${SPACEUSED}'/'${SPACESIZE}')'`"
81
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}%]"
84
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
89     fi
90   fi
91   if [ $LOOPDEL -eq 2 ]; then
92     sleep 60
93   fi
94 done
95
96 umount /mnt/btrfs &> /dev/null
97 rm -rf /tmp/btrfs_cleaner.lock
98 echo "***END: `date`***"
99