Open ghost opened 7 years ago
It got min age to always keep, max age to always delete, and will stay within the set free space for the chunks inbetween:
if pidof -o %PPID -x "$0"; then echo "Already running, exit" exit 1 fi
PLEXDRIVETEMP=/tmp/plexdrive/GoogleAJ/chunks MINDISKSPACE=50000000 CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') FORCEMINAGE=120 FORCEMAXAGE=1440
HRDISKSPACE=$(echo "$CURDISKSPACE" | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Starting with $HRDISKSPACE free diskspace" HRCHUNKSPACE=$(du -s $PLEXDRIVETEMP | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Starting with $HRCHUNKSPACE used chunkspace"
find $PLEXDRIVETEMP -mmin +$FORCEMAXAGE -amin +$FORCEMAXAGE -cmin +$FORCEMAXAGE -type f -delete
while [[ $CURDISKSPACE -le $MINDISKSPACE ]] do IFS= read -r -d $'\0' line < <(find $PLEXDRIVETEMP -mmin +$FORCEMINAGE -amin +$FORCEMINAGE -cmin +$FORCEMINAGE -type f -printf '%T@ %p\0' 2>/dev/null | sort -z -n) CHUNK="${line#* }" if [[ -z "$CHUNK" ]]; then break fi echo "$CHUNK" rm $CHUNK CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') done
find $PLEXDRIVETEMP -mindepth 1 -empty -delete
CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') HRDISKSPACE=$(echo "$CURDISKSPACE" | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Ended with $HRDISKSPACE free diskspace" HRCHUNKSPACE=$(du -s $PLEXDRIVETEMP | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Ended with $HRCHUNKSPACE used chunkspace"
Thanks this works well for me. Can you tell me what these fields are in MINDISKSPACE=50000000 is this Kb,GB or MB FORCEMINAGE=120 is this days mins hours? FORCEMAXAGE=1440 same as above.
It got min age to always keep, max age to always delete, and will stay within the set free space for the chunks inbetween:
!/bin/bash
if pidof -o %PPID -x "$0"; then echo "Already running, exit" exit 1 fi
PLEXDRIVETEMP=/tmp/plexdrive/GoogleAJ/chunks MINDISKSPACE=50000000 CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') FORCEMINAGE=120 FORCEMAXAGE=1440
HRDISKSPACE=$(echo "$CURDISKSPACE" | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Starting with $HRDISKSPACE free diskspace" HRCHUNKSPACE=$(du -s $PLEXDRIVETEMP | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Starting with $HRCHUNKSPACE used chunkspace"
find $PLEXDRIVETEMP -mmin +$FORCEMAXAGE -amin +$FORCEMAXAGE -cmin +$FORCEMAXAGE -type f -delete
while [[ $CURDISKSPACE -le $MINDISKSPACE ]] do IFS= read -r -d $'\0' line < <(find $PLEXDRIVETEMP -mmin +$FORCEMINAGE -amin +$FORCEMINAGE -cmin +$FORCEMINAGE -type f -printf '%T@ %p\0' 2>/dev/null | sort -z -n) CHUNK="${line#* }" if [[ -z "$CHUNK" ]]; then break fi echo "$CHUNK" rm $CHUNK CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') done
find $PLEXDRIVETEMP -mindepth 1 -empty -delete
CURDISKSPACE=$(df -k $PLEXDRIVETEMP | tail -1 | awk '{print $4}') HRDISKSPACE=$(echo "$CURDISKSPACE" | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Ended with $HRDISKSPACE free diskspace" HRCHUNKSPACE=$(du -s $PLEXDRIVETEMP | awk '{ sum=$1 ; hum[10242]="Gb";hum[1024]="Mb";hum[0]="Kb"; for (x=10243; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}') echo "Ended with $HRCHUNKSPACE used chunkspace"