script delete folders according to the current date

0

I have made a script to delete folders when the disk is, for example 80%, I want to use it in centOS with asterisk. where folders are organized "/ var / spool / monitor / 2018 / {01/02 /.../ 12}"

Well, I did this script that erased 2 months ago, according to the percentage of available space, but the question arises for example if you make many calls in the month 02 (February) and we are in March and February or January has the highest percentage of occupied space. What do I do in this situation? I'll schedule in the crontab to run the script every month. and if the space is still> 80% because the calls are in February and not January.

was to ask you a suggestion of how to raise it or what thing I wrote mál, as well as show you the code and it's super easy, the most likely to save code (that's for sure !!) but I just started in linux :(

 #!bin/bash

limit="80" ## porcentaje de espacio cuando debe actuar" ##
dir="/var/spool/asterisk/monitor"
mes='date +%m'
anyo='date +%Y'
ref="02";

# espacio en disco #
df -h | grep /dev/xvdb1 | sed s/%//g | awk '{print $5}' >> temp.log

temp=$( cat temp.log)

if [ $limit -le $temp ]; then
        BORRAR=0;
        BORRAR=$((10#$mes-$ref))

                case $BORRAR in ## esto es porque rm no logra encontrar la carpeta "2" porque estan nomradas como "02".
                1) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                2) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                3) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                4) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                5) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                6) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                7) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                8) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                9) rm -rf "$dir"/"$anyo"/0"$BORRAR"/
                ;;
                10) rm -rf "$dir"/"$anyo"/"$BORRAR"/
                ;;
                11) rm -rf "$dir"/"$anyo"/"$BORRAR"/
                ;;
                12) rm -rf "$dir"/"$anyo"/"$BORRAR"/
                ;;
                esac
else
        echo "nada para borrar"
fi

rm -rf temp.log

echo ___________________________________________________ >> log ## para hacer un pequeño log de cuando se ejecute el crontab ##
echo "El mes borrado fue: $BORRAR " >> log
date >> log
tail -n 22 /var/spool/mail/root >> log
df -h | grep /dev/xvdb1 | sed s/%//g | awk '{print $5}' >> log

porfas, tell me ideas or opinions, I think my script is being a bit sloppy: (

    
asked by Dani A. 20.04.2018 в 18:55
source

1 answer

0

You can try the following in your code:

# espacio en disco #
temp=$(df /dev/xvdb1 | sed s/%//g | awk '{print $5}')

if [ $limit -le $temp ]; then
  rm -rf $dir/$annio/$(printf %02 'expr $mes - $ref')
fi

Using printf will make the result of the subtraction always have 2 digits.

You can also use find with the parameter -ctime or -mtime to search files older than 2 months, for example: find $dir -mtime -60 -exec rm -rf '{}' \;

You can check the disk space occupied by each folder with the command du , for example: du -s $dir/* and you can order it with sort in the following way: du -s $dir/* | sort -nr .

    
answered by 06.06.2018 в 06:26