From 1730956734932d49e6183717d7a4e080650e12f3 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 18 Jan 2013 05:48:52 +0100 Subject: [PATCH] moved btrfs scripts out of overall script dir --- cleaner.sh | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ snapshot.sh | 35 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100755 cleaner.sh create mode 100755 snapshot.sh diff --git a/cleaner.sh b/cleaner.sh new file mode 100755 index 0000000..f040eb2 --- /dev/null +++ b/cleaner.sh @@ -0,0 +1,99 @@ +#!/bin/bash +# +# Clean old BTRFS snapshots +# */15 * * * * /root/somescripts/btrfs/cleanup.sh &> /dev/null +# +# This script will keep current/last day/month snapshots. +# This script will update current/last_day/week/month snapshots and will delete snapshots if needed (too many or low on space) +# +#set volumes to do curr/last day/month for. +LASTLIST="root home rw" + + +#TODO: change autoclean behaviour from auto_* to *.snap +# Autosnaps to keep. Counting/Considering *.snap only. There should be adate_*.snap snapshots only. +let MINSNAPCNT=3*5 +let MAXSNAPCNT=3*300 +MAXSPACEPERCENT=85 + +if [ -e /tmp/btrfs_cleaner.lock ]; then + echo "Lockfile found. Assuming another instance is running. exitting." + exit 1 +fi + +touch /tmp/btrfs_cleaner.lock +mount /mnt/btrfs &> /dev/null +cd /mnt/btrfs + +# Keep current and last MONTH +for vol in $LASTLIST; do + CHECK=$(grep "`date +"%Y-%m-"`" amon_curr_${vol}.txt 2> /dev/null) + if [ "$CHECK" == "" ]; then + echo "Autosnap cleaner: Rotating last and current month for ${vol}" + if [ -e "amon_curr_${vol}.snp" -a -e "amon_curr_${vol}.txt" ]; then + if [ -e "amon_last_${vol}.snp" -a -e "amon_last_${vol}.txt" ]; then + mv "amon_last_${vol}.snp" "`cat amon_last_${vol}.txt`" + fi + mv "amon_curr_${vol}.snp" "amon_last_${vol}.snp" + mv "amon_curr_${vol}.txt" "amon_last_${vol}.txt" + fi + CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-"`'*_'${vol}'.snap' | sort | head -n 1) + if [ "$CANDIDATE" != "" ]; then + echo $CANDIDATE > amon_curr_${vol}.txt + mv $CANDIDATE amon_curr_${vol}.snp + fi + fi +done + +# Keep current and last DAY +for vol in $LASTLIST; do + CHECK=$(grep "`date +"%Y-%m-%d"`" aday_curr_${vol}.txt 2> /dev/null) + if [ "$CHECK" == "" ]; then + echo "Autosnap cleaner: Rotating last and current day for ${vol}" + if [ -e "aday_curr_${vol}.snp" -a -e "aday_curr_${vol}.txt" ]; then + if [ -e "aday_last_${vol}.snp" -a -e "aday_last_${vol}.txt" ]; then + mv "aday_last_${vol}.snp" "`cat aday_last_${vol}.txt`" + fi + mv "aday_curr_${vol}.snp" "aday_last_${vol}.snp" + mv "aday_curr_${vol}.txt" "aday_last_${vol}.txt" + fi + CANDIDATE=$(find . -maxdepth 1 -name 'aadate_'`date +"%Y-%m-%d"`'*_'${vol}'.snap' | sort | head -n 1) + if [ "$CANDIDATE" != "" ]; then + echo $CANDIDATE > aday_curr_${vol}.txt + mv $CANDIDATE aday_curr_${vol}.snp + fi + fi +done + + +# CLEAN *.snap if needed (too many or low on space) +LOOPDEL=2 +while [ $LOOPDEL -eq 2 ]; do + LOOPDEL=1 + + btrfs fi show inthdd &> /tmp/btrfs_fishow.txt + SPACESIZE="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?size ([0-9]+\.[0-9]*)GB .*?$~\1~g'`" + SPACEUSED="`grep 'bytes used' /tmp/btrfs_fishow.txt | sed -E 's~^.*?bytes used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`" + SPACEALLOC="`grep 'size' /tmp/btrfs_fishow.txt | sed -E 's~^.*?GB used ([0-9]+\.[0-9]*)GB.*?$~\1~g'`" + rm /tmp/btrfs_fishow.txt + + SNAPCNT="`find . -maxdepth 1 -name 'auto_*' -o -name '*.snap' | wc -l`" + SPACEPERCENT="`python -c 'print int(100.0*'${SPACEUSED}'/'${SPACESIZE}')'`" + + echo "Autosnap cleaner: Snap count min/curr/limit [${MINSNAPCNT}/${SNAPCNT}/${MAXSNAPCNT}]." + echo "Autosnap cleaner: Space used/alloc/size/limit [${SPACEUSED}/${SPACEALLOC}/${SPACESIZE}][${SPACEPERCENT}%/${MAXSPACEPERCENT}%]" + + if [ $SNAPCNT -gt $MAXSNAPCNT -o $SPACEPERCENT -gt $MAXSPACEPERCENT ]; then + DELFILE="`find . -maxdepth 1 -name 'auto_*' | sort | head -n -${MINSNAPCNT} | head -n 1`" + if [ $SNAPCNT -gt $MINSNAPCNT -a "$DELFILE" != "" ]; then + btrfs subvolume delete "${DELFILE}" && LOOPDEL=2 + fi + fi + if [ $LOOPDEL -eq 2 ]; then + sleep 60 + fi +done + +umount /mnt/btrfs &> /dev/null +rm -rf /tmp/btrfs_cleaner.lock + diff --git a/snapshot.sh b/snapshot.sh new file mode 100755 index 0000000..d675e72 --- /dev/null +++ b/snapshot.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# +# take BTRFS snapshots +# 0 * * * * /root/somescripts/btrfs/snapshot.sh &> /dev/null +# This script will generate: current_$vol.snp/.txt + last_$vol.snp/.txt and date_$date_$vol.snp/.txt +# +# Set volumes to autosnap +SNAPLIST="root home rw" + +# Timestamp of creation. +STARTTS="`date +'%Y-%m-%d--%H-%M'`" +CALLPATH="`pwd`" + +mount /mnt/btrfs &> /dev/null +cd /mnt/btrfs + +for vol in $SNAPLIST; do + if [ -e "ahour_curr_${vol}.snp" -a -e "ahour_curr_${vol}.txt" ]; then + if [ -e "ahour_last_${vol}.snp" -a -e "ahour_last_${vol}.txt" ]; then + echo "Autosnap: Moving ahour_last_${vol}.snp to: `cat ahour_last_${vol}.txt`" + mv "ahour_last_${vol}.snp" "`cat ahour_last_${vol}.txt`" + fi + echo "Autosnap: Moving ahour_curr_${vol}.snp to: ahour_last_${vol}.snp" + mv "ahour_curr_${vol}.snp" "ahour_last_${vol}.snp" + mv "ahour_curr_${vol}.txt" "ahour_last_${vol}.txt" + fi + echo "Autosnap: Snapshotting ahour_curr_${vol}.snp (${STARTTS})" + echo "aadate_${STARTTS}_${vol}.snap" > ahour_curr_${vol}.txt + btrfs subvolume snapshot "${vol}" "ahour_curr_${vol}.snp" + touch -m -a "ahour_curr_${vol}.snp" +done + +cd $CALLPATH +umount /mnt/btrfs &> /dev/null || echo "Failed to unmount" + -- 2.43.0