All pastes #2047112 Raw Edit

Anonymous

public text v1 · immutable
#2047112 ·published 2011-04-16 10:13 UTC
rendered paste body
#!/bin/bash
####################################################
####################################################
##################### OPTIONS ######################
####################################################
####################################################

### MINIMUM FREE SPACE - WHEN SPACE GOES BELOW THIS VALUE SCRIPT WILL RUN
M=260

### HOW MUCH TO DELETE (IN GB)
F=50

### PATH TO LOGS
L=/home/kalli/autodelete

### PATH TO FOLDERS FROM WHICH SUBFOLDERS NEED TO BE DELETED (SEPERATE WITH SPACE)

P="/storage2/files/"

### DRIVE TO CHECK SPACE ON
D=/dev/md2

### MAX FOLDERS TO DELETE TO FREE UP SPACE
T=100

####################################################
####################################################
#################### SCRIPT ########################
####################################################
####################################################

### CHECK VARIABLES
if [ ! -n "$M" ]; then
echo Variable M must be set >> $L/error.log
exit
fi
if [ ! -n "$F" ]; then
echo Variable F must be set >> $L/error.log
exit
fi
if [ ! -n "$P" ]; then
echo Variable P must be set >> $L/error.log
exit
fi
if [ ! -n "$D" ]; then
echo Variable D must be set >> $L/error.log
exit
fi

### SCRIPT WAS EXECUTED
touch junk.log
echo `date +%x-%X` ... SCRIPT WAS EXECUTED >> $L/junk.log

### GET FREE SPACE
function space {
#echo SPACE WAS HERE
if [ `df -h | grep $D | cut -d' ' -f19 | cut -c4` ]; then
if [ `df -h | grep $D | cut -d' ' -f19 | cut -c4` = G ]; then
   int=`df -h | grep $D | cut -d' ' -f19 | cut -c1-3`
   varspace=`echo $int | bc -l | awk -F '.' '{ print $1; exit; }'`
#echo SPACE1 >> $L/junk.log
fi
#echo 1
#echo X $varspace
#elif [ `df -h | grep $D | cut -d' ' -f19 | cut -c4` = T ]; then
#   varspace=ENOUGH
#echo 2
elif [ `df -h | grep $D | cut -d' ' -f20 | cut -c3` ]; then
if [ `df -h | grep $D | cut -d' ' -f20 | cut -c3` = G ]; then
   varspace=`df -h | grep $D | cut -d' ' -f20 | cut -c1-2`
#echo SPACE2 >> $L/junk.log
fi
#echo 3
elif [ `df -h | grep $D | cut -d' ' -f22` = 0 ]; then
   varspace=0
#echo 4
fi
}


### CALCULATE REQUIRED SPACE AFTER
let MF=$M+$F

space
#M=""$lol""$M"
#MF="$lol""$lol""$MF"
#varspace="$lol""$lol""$varspace"
#varspace="$lol""$varspace" | sed 's/[0-9]*/\\&/g'
#echo VS: $varspace
#echo M: $M
#echo MF: $MF
#if [ "$varspace" -lt "$MF" ]; then echo HAHA; fi
if [ ! "$varspace" = "ENOUGH" -a "$varspace" -lt "$M" ]; then
#echo A
   loopn=0
  if [ "$varspace" -lt "$MF" ]; then
#echo LOOP: "$loopn" AND T: "$T"
   while [ "$loopn" -lt "$T" -a "$varspace" -lt "$MF" ]
  do
dir=`find $P -mindepth 1 -maxdepth 1 -type d -printf '%Ts %p\0' | \
                    tr '\0\n' '\n\0' | sort -rn | tail -1 | cut -d' ' -f2- | \
                    tr '\0' '\n' | sed  -e 's/\[/\\[/g' -e 's/\]/\\]/g'`
#| sed -e 's/\ /\\&/g' -e 's/\[/\\[/g' -e 's/\]/\\]/g'`
echo $dir >> workingon.log
rm -rvf "$dir" | grep "removed directory" | echo "`date +"%a %b %d %H:%M:%S %Y"` AUTODELETE: "$dir"" >> /glftpd/ftp-data/logs/glftpd.log
  loopn=$(( $loopn + 1 ))
space
  done
else echo There is currently sufficient space left on $D | echo `date +%x-%X` ... There is currently sufficient space left on $D >> $L/main.log
fi
fi