I use my QNAP NAS to download backups from my root server. The backups are downloaded via rsync and are piling up on an external HDD of the NAS. For this, I have to ensure that there is always enough free space by periodically deleting the oldest backup files that are on the NAS. Existing solutions on stackoverflow are quite complex and often do not work on the limited busybox of the NAS. There, I came up with a simple and elegant solution:
#!/bin/bash PATH="/share/external/sdza3/backup/download" FREE=300000 echo Working on ${PATH} echo Goal is ${FREE} free MiB echo while [ `/bin/df -m ${PATH} | /usr/bin/tail -n 1 | /bin/awk -F ' ' ' { print $4 } '` -lt ${FREE} ]; do FILES=`/usr/bin/find ${PATH} -type f` FILE=`/bin/ls -t ${FILES} | /usr/bin/tail -n 1` echo Deleting ${FILE} /bin/rm ${FILE} echo New free space is `/bin/df -m ${PATH} | /usr/bin/tail -n 1 | /bin/awk -F ' ' ' { print $4 } '` MiB /bin/sleep 1 echo done echo done