Script for Samba recycle bin management

Hi

I wrote a script for the recycle bin management with Samba shares but I guess it could also be used for other purposes. Feel free to use and or modify it according to your needs. Optimization is always welcome ;)

Please note, that the following option is a requirement in order to let the script detect the age of the files properly:
Code:
recycle:touch = Yes

Code:
#!/usr/local/bin/bash
# ============================================================ #
#                        Clean .Trashes                        #
# ============================================================ #
#
# <EXPIREY[unit]>|<MAX_SIZE[Bytes]>|<PATH_OF_RECYLCE_BIN>
#
# This script iterates through recycle bin and removes expired
# files and folders.
#
# If max. space is exceeded, then it will remove the file(s) with
# the oldest access time until du of recyle bin is below 40% of
# max. space. (ls -lutr)
#
#
# Expirey can be expressed in following units:
#
# s  second
# m  minute (60 seconds)
# h  hour   (60 minutes)
# d  day    (24 hours)
# w  week   (7 days)
#
#   5 GB =   5368709120 Byte
#  10 GB =  10737418240 Byte
#  20 GB =  21474836480 Byte
#  50 GB =  53687091200 Byte
# 100 GB = 107374182400 Byte
#
# ============================================================ #


TRASHES="                                           \
5d|10737418240|10%|/mnt/FireFly/.Trashes            \
5d|21474836480|10%|/mnt/ISO-Library/.Trashes        \
5d|107374182400|10%|/mnt/vMachines/.Trashes         \
"

TIME="$( time (

# Check if Recycle bin is full an delete oldest files untill it is below ${REDUCE_TILL}
delete_oldest_file() {
    local TRASH="${1}"
    OLDEST="$( find "${TRASH}" -print0 | xargs -0 ls -lutr | head -n 1 | sed -E 's|.*[0-9]+[[:space:]]+[[:alpha:]]{3}[[:space:]]+[0-9]{1,2}[[:space:]]+[0-9]{1,2}:[0-9]{1,2}[[:space:]]+(.*)|\1|' )"
    #echo "${OLDEST}"
    rm -r "${OLDEST}"
}

for TRASH in ${TRASHES}; do

    EXPIRY="$(      echo "${TRASH}" | awk -F'|' '{print $1}' )"
    MAX_SIZE="$(    echo "${TRASH}" | awk -F'|' '{print $2}' )"
    REDUCE_TILL="$( echo "${TRASH}" | awk -F'|' '{print $3}' | sed -E 's|%||I'  )"
    TRASH="$(       echo "${TRASH}" | awk -F'|' '{print $4}' )"

    if [ -d ${TRASH} ]; then
        cd ${TRASH}

        # Delete everything expired except directories
        find ./ -atime +${EXPIRY} -print | while read ITEM; do
            if [ "${ITEM}" != "./" ] && [ ! -d "${ITEM}" ]; then
                #echo "rm -rf \"${ITEM}\""
                rm -rf "${ITEM}"
            fi
        done


        REQ_FREE_SIZE="$( echo $(printf %.$2f $(echo "scale=2; (${MAX_SIZE} * (${REDUCE_TILL}/100) ) + ${MAX_SIZE}" | bc)) )"
        SIZE=$(( $(du -sk ${TRASH} | awk '{print $1}') * 1024))
        if [ ${SIZE} -gt ${MAX_SIZE} ]; then
            while [ ${SIZE} -gt ${REQ_FREE_SIZE} ]; do
                delete_oldest_file ${TRASH}
                SIZE="$(du -sk ${TRASH} | awk '{print $1}')"
                SIZE=$((${SIZE} * 1024))
            done
        fi


        # Delete empty expired directories
        for (( i=1; i<=5; i++ )); do    # This is a workarround - actually recursiv function required!
            find ./ -type d -empty -atime +${EXPIRY} -print | while read ITEM; do
                if [ "${ITEM}" != "./" ]; then
                    #echo "rmdir \"${ITEM}\""
                    rmdir "${ITEM}" > /dev/null 2>&1
                fi
            done
        done

    fi
done
) 2>&1
)"

TIME_REAL="$( echo "${TIME}" | grep 'real' | sed -E 's|real[[:space:]]+(.*)|\1|' )"
TIME_REAL_MINUTES="$( echo "${TIME_REAL}" | sed -E 's|([[:digit:]]+)(m[[:digit:]]+.*)|\1|' | awk '{print $1}' )"

if [ ${TIME_REAL_MINUTES} -gt 30 ]; then
    # Send a testmail
    SUBJECT="WARNING - $(hostname -s) - Samba Recycle Bin - Takes way too long (exec_time > 30 Min)"

CONTENT="Samba has Recycle Bin activated. It's script, which maintains the recycle bins is beeing executed way too long. It took more than 30 minutes to execute.
The limits of the recycle bin size are set in the script itselfs under the location: ${0}

Configuration Syntax:
<EXPIREY[unit]>|<MAX_SIZE[Bytes]>|<PATH_OF_RECYLCE_BIN>

Current Configuration:
${TRASHES}
"

    echo -e "${CONTENT}" | mail -s "${SUBJECT}" root
fi
 
Will these one-liners be easier?
Code:
find /mnt/*/.Trashes/ -type f -mtime +5d -delete
find /mnt/*/.Trashes/ -type d -empty -depth -delete

find /mnt/*/.Trashes/ -type f -mtime +5d -exec rm -fv {} \;
find /mnt/*/.Trashes/ -type d \! -path "*/" -empty -depth -exec rm -frv {} \;
 
Back
Top