docker-mysql-stack/nettoyer_quotas.sh

40 lines
1.0 KiB
Bash
Raw Permalink Normal View History

2019-06-25 16:08:17 +00:00
#!/bin/bash
#find /mnt/dumps -mtime +30 -print
# This script removes old database dumps.
2019-06-25 16:08:17 +00:00
PURGEPATH=/mnt/dumps
cd $PURGEPATH
OLD_IFS="$IFS"
# We want to keep 5 Go free (unit: kB)
minfree=5000000
# We want to keep 30 dumps max (min is 2)
maxkeep=30
count=$(ls | wc -l)
count_removed=0
2019-06-25 16:08:17 +00:00
IFS=$(echo -en "\n\b")
for file in `ls -rt`
do
IFS="$OLD_IFS"
# unit: KB
free_space=`df -P "$PURGEPATH" | grep "$PURGEPATH" | head -n 1 | awk 'BEGIN{FS=" "} {print $4}'`
if [ $(( $count - $count_removed )) -gt $(( $maxkeep - 2 )) ]
then
rm -rf -- "$file" && echo "Removed $file"
count_removed=$(( $count_removed + 1 ))
elif [ "$free_space" -lt $minfree ]
2019-06-25 16:08:17 +00:00
then
rm -rf -- "$file" && echo "Removed $file"
count_removed=$(( $count_removed + 1 ))
2019-06-25 16:08:17 +00:00
else
echo "Enough free space retrieved"
break
fi
sleep 1
IFS=$(echo -en "\n\b")
done
echo "$free_space KB free on $PURGEPATH"
2019-06-25 16:08:17 +00:00
IFS="$OLD_IFS"