X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=backup.sh;h=c4330309ea2d685208766d08a3aa3d99ad6adbe2;hb=22aae2730e071334116c61dc67d9f00ea960b364;hp=ed089e58c866b9bcdff7bf30139a557444e13bd0;hpb=4f1dd8763d5c27debcda625e7343ba7f1457409e;p=backup.sh diff --git a/backup.sh b/backup.sh index ed089e5..c433030 100755 --- a/backup.sh +++ b/backup.sh @@ -1,5 +1,29 @@ #!/bin/sh +# Locking +LOCKFILE=/home/backup/backuprun.lockfile + +# Initially, we consider everything to be just fine. +R=0 + +# Die gracefully (ie. remove lockfile) +die() { + echo `date`": Something nasty happened, and since I fork a lot:" + echo `date`": I CANNOT CLEAN UP THE MESS MYSELF." + echo `date`": You need to get rid of lost process named stuff like $0, tar and ssh." + exit 255 +} + +# Trap C-c and kill +trap die SIGINT SIGTERM + +# Don't start if we're already running +if [ -e $LOCKFILE ]; then + echo `date`": $LOCKFILE exists, exiting." + exit 1 +fi; +touch $LOCKFILE + #dirs confdir=/home/backup/conf/ #configuration files storagedir=/backup #mountpoint of huge disc @@ -20,10 +44,7 @@ PATH=/local/bin:$PATH:/store/bin export PATH #start the logfile -echo `date`": Backup run starting" -echo "Exclude pattern:" -cat $exclude -echo "End exclude pattern" +echo `date`": Backup run starting" >&2 umask 027 @@ -32,11 +53,10 @@ unixcomputers=`cat $confdir/computers.unix \ | grep -v "^#" \ | grep -v "^$" ` - +# Backup only one computer, from command line? if [ $1 ] ; then unixcomputers=$1 fi; -### #disse bør kunne varieres fra fs til fs? maksantallfulle=3 #hvor mange fulle vi tar vare på @@ -55,7 +75,8 @@ DATEs=`date "+%Y-%m-%d %H:%M"` #format: tar backup() { - printf "%-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" + echo -n `date` >&2 + printf " $computer: %-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" >&2 if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ] then @@ -77,6 +98,7 @@ backup() exf=`ssh $computer "ls ~/.backup/exclude"` #We try to run tar on the remote computer + # z gzip it # c create archive # C change to directory first # - output to stdout (we pipe to gzip, then to dd) @@ -87,18 +109,11 @@ backup() # ..to dd, to output to a file. We surpress messages from dd. # And at last, redirect stderr to stdout, to get output logged. TARFILE=$DATE.tmp - TARCMD="ssh $computer \"$tar --one-file-system -cf - -C $filesystem . $lastcmd \ - --exclude-from=$exf | gzip\" | (dd of=$TARFILE 2> /dev/null) 2>&1" - echo "cmdline: $TARCMD" - eval $TARCMD - - # Ideally, we should check wether the tar command returned 0 or not, but it - # seems a pipe in bash returns the value of the last command in the pipe. - # Instead, we check wether the resulting file has zero size, in which case we - # consider it an error. - if [ -s $TARFILE ]; then - echo `date`": command probably ran without errors." - #perhaps it did work + TARCMD="ssh $computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \ + --exclude-from=$exf\" > $TARFILE" + echo `date`" $computer: Running $TARCMD" >&2 + + if eval $TARCMD; then mv $TARFILE $DATE.tgz #make a filelist. #update the datefile if the filelist is ok. @@ -116,7 +131,10 @@ backup() else #it did not work rm $TARFILE - echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" + echo `date`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" + + # We don't want to return 0 + R=1 fi } @@ -125,13 +143,14 @@ backup() for computer in $unixcomputers do ( - echo `date`": Backing up $computer" + echo `date`" $computer: Backing up $computer" >&2 # Try to SSH to the computer without entering a password. - if `ssh -n -o NumberOfPasswordPrompts=0 $computer /bin/true`; then - echo "Passwordless SSH to $computer works." - else - echo "Could not use passwordless SSH to $computer." + if ! `ssh -n -o NumberOfPasswordPrompts=0 $computer /bin/true`; then + echo `date`" $computer: Could not use passwordless SSH." + + # We don't want to return 0 + R=1 break; fi @@ -145,28 +164,37 @@ do | grep -v "^$" \ | awk '{ if ( $(NF-1) != "0" ) print $2}' ` - echo "Filesystems to backup on $computer: $filesystems" - #clean up our dir at this client if ! ssh $computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then - echo "Could not create backup staging area at $computer:~/.backup" + echo `date`" $computer: Could not create backup staging area at $computer:~/.backup" + # We don't want to return 0 + R=1 break; fi #try to copy $exclude to $computer if ! scp $exclude $computer:~/.backup/exclude > /dev/null; then - echo "Could not copy exclude.txt to $computer" + echo `date`" $computer: Could not copy exclude.txt to $computer" + # We don't want to return 0 + R=1 break; fi #try to copy preeexec and postexec if they exist +# TODO: Gah, clean this mess! [ -f $confdir/preexec.$computer ] && ( scp $confdir/preexec.$computer $computer:~/.backup/preexec || - ( echo "Could not copy preexec.$computer to $computer:~/.backup/preexec"; break ) + ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec"; + R=1 + break + ) ) [ -f $confdir/postexec.$computer ] && ( scp $confdir/postexec.$computer $computer:~/.backup/postexec || - ( echo "Could not copy postexec.$computer to $computer:~/.backup/postexec"; break ) + ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec" + break + R=1 + ) ) #try to run preexec if it exist @@ -200,7 +228,7 @@ do #gå ned i rett katalog, eller dø # TODO bør sende mail om dette skjer! - cd $storagedir/$computer/$sfilesystem/$backuplevel || exit 1 + cd $storagedir/$computer/$sfilesystem/$backuplevel || die #perform the actual backup backup @@ -216,7 +244,7 @@ do for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf` do prefix=`echo $full | sed "s/\.[^.]*$//"` - echo "$computer:$filesystem sletter full $prefix (for mange)" + echo `date`": $computer:$filesystem sletter full $prefix (for mange)" >&2 rm $prefix* done @@ -227,8 +255,6 @@ do -type f \ \! -newer $oldf \ -exec rm {} \; -# #denne funker bare med gnu find -# #-printf "$computer:$filesystem sletter daglig %f (for gammel)\n" done #try to run postexec if it exist @@ -239,14 +265,13 @@ done & wait -#create report -# (Hasn't been working for years -berge) -#/home/backup/report.sh $storagedir > /tmp/report.txt -#[ ! -z $remotestat ] && scp /tmp/report.txt $remotestat -### - -# print diskusage to logfile -echo "Current disk usage:" -df -k +# Remove lockfile +rm $LOCKFILE -echo `date`": Backup run ended" +# Did anything go wrong? +if [ $R != 0 ]; then + echo `date`": Backup run ended with errors, check logs." + exit 1 +else + echo `date`": Backup run ended" >&2 +fi