From db69183b90eaae0bf23044f1b73b8e820df343d7 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 18 Jan 2013 05:48:52 +0100 Subject: [PATCH] Reformatted code and added copyright/licence headers --- cleaner.sh | 128 +++++++++++++++++++++++++++++----------------------- snapshot.sh | 46 +++++++++++++------ 2 files changed, 103 insertions(+), 71 deletions(-) diff --git a/cleaner.sh b/cleaner.sh index f623090..7e006b7 100755 --- a/cleaner.sh +++ b/cleaner.sh @@ -1,6 +1,22 @@ #!/bin/bash +# +# Copyright 2011 by Jan Vales (Someone ) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + # -# Clean old BTRFS snapshots +# Purge old BTRFS snapshots # 30 * * * * /root/somescripts/btrfs/cleanup.sh &> /dev/null # # This script will keep current/last day/month snapshots. @@ -16,8 +32,8 @@ MAXSPACEPERCENT=95 echo "***START: `date`***" if [ -e /tmp/btrfs_cleaner.lock ]; then - echo "Lockfile found. Assuming another instance is running. exitting." - exit 1 + echo "Lockfile found. Assuming another instance is running. exitting." + exit 1 fi touch /tmp/btrfs_cleaner.lock @@ -26,73 +42,73 @@ 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 current month for ${vol}" - if [ -e "amon_curr_${vol}.snp" -a -e "amon_curr_${vol}.txt" ]; then - echo "Autosnap cleaner: Rotating last month for ${vol}" - 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 + CHECK=$(grep "`date +"%Y-%m-"`" amon_curr_${vol}.txt 2> /dev/null) + if [ "$CHECK" == "" ]; then + echo "Autosnap cleaner: Rotating current month for ${vol}" + if [ -e "amon_curr_${vol}.snp" -a -e "amon_curr_${vol}.txt" ]; then + echo "Autosnap cleaner: Rotating last month for ${vol}" + 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 current day for ${vol}" - if [ -e "aday_curr_${vol}.snp" -a -e "aday_curr_${vol}.txt" ]; then - echo "Autosnap cleaner: Rotating last day for ${vol}" - 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 + CHECK=$(grep "`date +"%Y-%m-%d"`" aday_curr_${vol}.txt 2> /dev/null) + if [ "$CHECK" == "" ]; then + echo "Autosnap cleaner: Rotating current day for ${vol}" + if [ -e "aday_curr_${vol}.snp" -a -e "aday_curr_${vol}.txt" ]; then + echo "Autosnap cleaner: Rotating last day for ${vol}" + 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 + 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 + 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 '*.snap' | wc -l`" - SPACEPERCENT="`python -c 'print int(100.0*'${SPACEUSED}'/'${SPACESIZE}')'`" + SNAPCNT="`find . -maxdepth 1 -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}%]" + 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 '*.snap' | 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 + if [ $SNAPCNT -gt $MAXSNAPCNT -o $SPACEPERCENT -gt $MAXSPACEPERCENT ]; then + DELFILE="`find . -maxdepth 1 -name '*.snap' | 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 diff --git a/snapshot.sh b/snapshot.sh index 0ce283c..0867d7c 100755 --- a/snapshot.sh +++ b/snapshot.sh @@ -1,6 +1,22 @@ #!/bin/bash # -# take BTRFS snapshots +# Copyright 2011 by Jan Vales (Someone ) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# +# 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 # @@ -16,20 +32,20 @@ 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" - touch -m -a "${vol}" + 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" + touch -m -a "${vol}" done cd $CALLPATH -- 2.43.0