]> git.sesse.net Git - backup.sh/commitdiff
Various bug fixes and improvements
authorroot <empty>
Thu, 26 Jul 2007 02:11:06 +0000 (02:11 +0000)
committerroot <empty>
Thu, 26 Jul 2007 02:11:06 +0000 (02:11 +0000)
-berge

backup.sh

index adeea18272ce1860731430b7fb3e4b6434c2497e..ed089e58c866b9bcdff7bf30139a557444e13bd0 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -20,12 +20,10 @@ PATH=/local/bin:$PATH:/store/bin
 export PATH
 
 #start the logfile
-date
-echo
+echo `date`": Backup run starting"
 echo "Exclude pattern:"
 cat $exclude
-echo
-echo
+echo "End exclude pattern"
 
 umask 027
 
@@ -62,7 +60,7 @@ backup()
  if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
  then
   lastd=`cat ../.date`
-  lastcmd="--newer=\"$lastd\""
+  lastcmd="--newer='$lastd'"
  else
   lastcmd=""
  fi
@@ -79,17 +77,29 @@ backup()
  exf=`ssh $computer "ls ~/.backup/exclude"`
 
  #We try to run tar on the remote computer
- #    C is instead of 'cd $filesystem'
- #    - is 'this directory'
- #    l is 'stay in local filesystem
- echo "cmdline: $tar --one-file-system -cfC - $filesystem . $lastcmd --exclude-from=$exf | gzip"
- if `ssh $computer "$tar --one-file-system -cfC - $filesystem . $lastcmd \
-      --exclude-from=$exf \
-        | gzip" \
-   |  dd of=$DATE.tmp 2>/dev/null` 
- then
+ #    c create archive
+ #    C change to directory first
+ #    - output to stdout (we pipe to gzip, then to dd)
+ #    . where to start taring (see C)
+ #    $lastcmd only files newer than this
+ #    --exclude-from file to get exclusion pattern from
+ #    pipe to gzip, which in turn pipes over the ssh-stream
+ #    ..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
-  mv $DATE.tmp $DATE.tgz
+  mv $TARFILE $DATE.tgz
   #make a filelist.
   #update the datefile if the filelist is ok.
   tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
@@ -105,9 +115,8 @@ backup()
 
  else
   #it did not work
-  rm $DATE.tmp 2>/dev/null
-  echo "$backuplevel backup of $computer:$filesystem failed and deleted"
-  # TODO send mail!
+  rm $TARFILE
+  echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
  fi
 
 }
@@ -116,13 +125,14 @@ backup()
 for computer in $unixcomputers
 do
 ( 
+ echo `date`": Backing up $computer"
 
  # 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. Exiting."
-  exit 1;
+  echo "Could not use passwordless SSH to $computer."
+  break;
  fi
  
  #tømmer variabelen for sikkerhets skyld
@@ -135,18 +145,29 @@ do
   | grep -v "^$" \
   | awk '{ if ( $(NF-1) != "0" ) print $2}' `
 
+ echo "Filesystems to backup on $computer: $filesystems"
+
  #clean up our dir at this client
- ssh $computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"
+ if ! ssh $computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then
+  echo "Could not create backup staging area at $computer:~/.backup"
+  break;
+ fi
 
  #try to copy $exclude to $computer
- scp $exclude $computer:~/.backup/exclude ||
-    echo "could not copy exclude.txt to $computer :("
+ if ! scp $exclude $computer:~/.backup/exclude > /dev/null; then
+  echo "Could not copy exclude.txt to $computer"
+  break;
+ fi
 
  #try to copy preeexec and postexec if they exist
- [ -f $confdir/preexec.$computer ] &&
-    scp $confdir/preexec.$computer  $computer:~/.backup/preexec
- [ -f $confdir/postexec.$computer ] &&
-    scp $confdir/postexec.$computer $computer:~/.backup/postexec
+ [ -f $confdir/preexec.$computer ] && (
+    scp $confdir/preexec.$computer  $computer:~/.backup/preexec ||
+     ( echo "Could not copy preexec.$computer to $computer:~/.backup/preexec"; break )
+    )
+ [ -f $confdir/postexec.$computer ] && (
+    scp $confdir/postexec.$computer $computer:~/.backup/postexec ||
+     ( echo "Could not copy postexec.$computer to $computer:~/.backup/postexec"; break )
+    )
 
  #try to run preexec if it exist
  ssh $computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec"
@@ -159,7 +180,6 @@ do
   #lager det som trengs av kataloger
   mkdir -m 755 -p $storagedir/$computer/$sfilesystem/{full,daglig} 2>/dev/null
 
-  #
   echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name 
   chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name
 
@@ -174,9 +194,7 @@ do
   fi
 
   #sjekker om det er på tide med en full
-  if 
- [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]
-  then
+  if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]; then
    backuplevel=full
   fi
  
@@ -228,6 +246,7 @@ wait
 ###
 
 # print diskusage to logfile
+echo "Current disk usage:"
 df -k
 
-date
+echo `date`": Backup run ended"