#!/bin/csh -f #H# Makes and rotates archives of a dir using hardlinks #H# Params: #H# 1: dir to archive (the new empty one) #H# 2: -[vV] verbose or really verbose (optional) #H# Note that I am lazy so the order of the params matters. #H# The NEW format of the filenames is [$1].yyyy-mm-dd.hh-mm-ss #H# The .current symlink is made in the backup program itself. set DIR=$1 set MaxArchives=9 #setup command params and determine if being verbose if ("$2" == "-v") then set Verbose="y" set MVParams="-v" set RMParams="-rf" set CPParams="-al" else if ("$2" == "-V") then set Verbose="Y" set MVParams="-v" set RMParams="-rfv" set CPParams="-alv" else set Verbose="n" set MVParams="" set RMParams="-rf" set CPParams="-al" endif endif # Get the name of the dir without any date set DirName=`echo $DIR | sed -e 's/\.....-..-..\...-..-..//'` if ($Verbose != "n") echo "DirName is $DirName." # Verify that a previos backup already exists before attempting to archive if -e ${DirName}.current then # count up how many backups there are (NOT including the new which will be created soon.) set DirCount=`/bin/ls -dr ${DirName}.????-??-??.??-??-??|wc -l` if ($Verbose != "n") echo "There is currently $DirCount old backups in the backup dir." # figure out how many directories need to be deleted... @ NumDirDelete = $DirCount - $MaxArchives if ($NumDirDelete > 0) then if ($Verbose != "n") echo "$NumDirDelete old backup directories need to be deleted." # figure out which directores need to be deleted... set DirDeleteList=`/bin/ls -dr ${DirName}.????-??-??.??-??-?? | tail -n $NumDirDelete` if ($Verbose != "n") echo "Removing old archive(s) $DirDeleteList..." rm $RMParams $DirDeleteList endif # get the name of the old current backup set OldBackup=`realpath ${DirName}.current` if ($Verbose != "n") echo "Making duplicate of $OldBackup for the new backup." # remove .current symlink as it isn't going to be current for long :P rm $RMParams ${DirName}.current # Make a copy of the old backup dir into the new backup dir. # After this command $DIR and $OldBackup will be identical and the files in them will # be hard links to each other. Then rsync can come along and update $DIR breaking the # links to $OldBackup as needed. # The incomplete is just a sanity check that will be implamented later. ln -s $DIR ${DirName}.incomplete cp $CPParams $OldBackup $DIR || exec echo "Problem archiving $OldBackup." rm $RMParams ${DirName}.incomplete else if ($Verbose != "n") echo "No current backup exists. Creating NEW backup." mkdir -p $DIR endif