1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#!/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 |