Backup Management [delete X but keep at least Y]

Hi,

I'm trying to achieve a script which looks for expired backups in a folder and deletes them BUT keeps at least 2 of the youngest backups in spare - even if they would be older then ${EXPIRY}

Code:
EXPIRY="3"

while read FILE; do
    #stat -f "%-10m %40N" "${FILE}"
    echo "delete ${FILE}"
done <<< "$( find ${backup_dir} -type f -mtime +${EXPIRY} | sort -d )" | awk 'NR > 2 {print $2}'



Files in the backup folder would look like this - and yes, they could also contain spaces:
WS-01_-_2014-06-09_21-33-00.xva
WS-01_-_2014-06-10_21-33-00.xva
WS-01_-_2014-06-11_21-33-00.xva
WS-02_-_2014-06-09_21-33-00.xva
WS-02_-_2014-06-10_21-33-00.xva
WS-02_-_2014-06-11_21-33-00.xva
WS-03_-_2014-06-09_21-33-00.xva
WS-03_-_2014-06-10_21-33-00.xva
WS-03_-_2014-06-11_21-33-00.xva
WS-05_-_2014-06-09_21-33-00.xva
WS-05_-_2014-06-10_21-33-00.xva
WS-05_-_2014-06-11_21-33-00.xva



Any idea how I could achieve this with minimum code, since the awk 'NR > 2 {print $2}' doesn't do the job right
Thanks
 
Something like the following pseudocode is not enough?
Code:
i = count all files #i.e. find $backup_dir -type f | wc
while read FILE;
  do
    if [ $i > 2 ]
      delete $FILE
      i = i - 1
    else exit
  done
 
Thanks for the inspiration - this way, I would have to separate backups of one VM into one folder, but it's toally fine. Thanks again

Kind regards
Leander
 
It depends on your file/backup naming conventions, but find has the -name parameter, which matches given pattern, so given that WS-05 identifies one virtual machine, you can use something like find $backup_dir -name WS-05* for search, count results and then remove them one by one until desired count remains.

All together may looks like
Code:
$VM_PATTERN = 'WS-05'
$BACKUPS = `/full/path/to/find ${WHERE} -name ${WM_PATTERN}`
$i = `/full/path/to/wc -l ${BACKUPS}`
while read $BACKUPS;
  do
    if [ $i > 2 ]
      delete $FILE
      i = i - 1
    else exit
  done
 
This is my final solution to this:

Code:
backup_dir="/mnt/Backup/Xen"
KEEP=1    # Keep at least [n] backups

while read DIR; do

    i=0
    while read FILE; do
        if [ ! -z "${FILE}" ]; then
            if [ ! ${i} -lt ${KEEP} ]; then
                [[ ! -z "${FILE}" ]] && rm "${FILE}"
            fi
            i=$(( ${i} + 1 ))
        fi
    done <<< "$( find "${DIR}"/* -type f | sort -dr | grep -i '.xva' )"

done <<< "$( find "${backup_dir}"/* -maxdepth 1 -type d | sort -d )"

Kind regards
 
Back
Top