#!/bin/bash function delete_older_backups { typeset host="$1" typeset year=$(date '+%Y') typeset month=$(date '+%m') typeset day=$(date '+%d') # Strip leading 0 off days month=${month#0} day=${day#0} # Remove all except 5th day backups for early portion of this month if [ $day -gt 20 ] then rm -rf $(printf "$host/%04d-%02d-0[02-57-9].*" $year $month) fi # Remove all except 5th day backups for last month let month=month-1 [ $month -lt 1 ] && let month=month+12 && let year=year-1 if [ $day -gt 10 ] then rm -rf $(printf "$host/%04d-%02d-?[02-57-9].*" $year $month) else rm -rf $(printf "$host/%04d-%02d-[01][02-57-9].*" $year $month) fi # Remove all except 10th day backups for 2-5 months ago for x in 1 2 3 4 do let month=month-1 [ $month -lt 1 ] && let month=month+12 && let year=year-1 rm -rf $(printf "$host/%04d-%02d-?[02-9].*" $year $month) done # Remove all except monthly backups (1st of month) for # 6 months ago & beyond for x in 1 2 3 4 5 6 do let month=month-1 [ $month -lt 1 ] && let month=month+12 && let year=year-1 rm -rf $(printf "$host/%04d-%02d-[1-3][0-9].*" $year $month) done } # Set up paths and stuff arg0=$(basename $0) PATH="$HOME/bin:$PATH" cd ~/Backups || exit 1 d="[0-9]" status=0 weekday=$(date '+%a') for host in * do # Locate the last successful backup done priorbackup=$(eval "ls -1d $host/$d$d$d$d-$d$d-$d$d.$d 2>/dev/null" | tail -1) if [ "$priorbackup" = "" ] then echo "$host: No prior backup found; please check." continue fi # Get today's date, then if we're backing up twice on the same # day then locate the sequence number for this backup. date=$(date '+%Y-%m-%d') sequence=0 priordate=$(basename "$priorbackup" | cut -c1-10) priorsequence=$(basename "$priorbackup" | cut -c12) if [ "$date" = "$priordate" ] then let sequence=priorsequence+1 if [ $sequence -gt 9 ] then echo "$host: Out of sequence numbers for today." continue fi fi # Pull the backup from the remote host to the current server. # This varies a lot, so run a script to do it for each backup # directory if present. if [ -x $host/get-backup ] then mkdir -p "$host/Logs" $host/get-backup "$host" "$host/$date.$sequence" \ "../$priordate.$priorsequence" \ > "$host/Logs/get-backup.$weekday" backupstatus=$? else echo "$host: no get-backup script, or not executable." backupstatus=1 fi if [ $backupstatus -eq 0 ] then delete_older_backups "$host" else [ -f "$host/$date.$sequence" ] && mv $host/$date.$sequence $host/$date.$sequence.incomplete status=1 fi done exit $status