#!/bin/bash
# Vars
log="rotate_backup.log"
backupPath="/backup/machines/"
oldCopies=($(ssh $user@$backupHost "ls $backupPath$localHost | sort -r"))
maxNumOfCopies=3
# Log file
cat /dev/null > $log
# If they are more than the max of copies
if [ ${#oldCopies[@]} -gt $maxNumOfCopies ]; then
# Unset of array values with index < of maxNumOfCopies
i=0
while [ $i -lt ${#oldCopies[@]} ]; do
if [ $i -lt $(( $maxNumOfCopies )) ]; then
unset oldCopies[$i]
fi
let "i++"
done
# We remove the old copies
for copy in ${oldCopies[@]}; do
ssh $user@$backupHost "rm -rf $backupPath/$localHost/$copy"
result=$(evalOperation $?)
echo "<p>Removing copy of $copy [$result]</p>" >> $log
done
fi
if [ ! -s rotate_backup.log ]; then
echo "<p>No copies to delete<p>" >> $log
fi
# We send a email
sendMailStatus "Removing old copies of $localHost done $date" $log