]> git.sesse.net Git - backup.sh/blobdiff - backup.sh
now run backups in parallell again
[backup.sh] / backup.sh
index 7d4ee304118f5c95a509b618fa4d03bda8a7c55c..73188a298006307316faa7509faf4694b313d77d 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -1,5 +1,35 @@
 #!/bin/sh
 
+# TODO(sesse, 2007-12-16): 
+#  * Fiks så parallelliseringen fungerer igjen
+#  * Bruk tee i stedet for en separat tar ztvf-prosess
+#  * Sjekk hvorfor .idx-filene til UD er blitt 0 for siste fullbackuper
+
+# Locking
+LOCKFILE=/home/backup/backuprun.lockfile
+
+# Initially, we consider everything to be just fine.
+R=0
+
+# Die more or less gracefully (at least notify user)
+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."
+       echo `date`": $LOCKFILE exists, exiting." >&2
+       exit 1
+fi;
+touch $LOCKFILE
+
 #dirs
 confdir=/home/backup/conf/           #configuration files
 storagedir=/backup                   #mountpoint of huge disc
@@ -20,12 +50,7 @@ PATH=/local/bin:$PATH:/store/bin
 export PATH
 
 #start the logfile
-date
-echo
-echo "Exclude pattern:"
-cat $exclude
-echo
-echo
+echo `date`": Backup run starting" >&2
 
 umask 027
 
@@ -34,11 +59,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å
@@ -57,12 +81,13 @@ 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
   lastd=`cat ../.date`
-  lastcmd="--newer=\"$lastd\""
+  lastcmd="--newer='$lastd'"
  else
   lastcmd=""
  fi
@@ -76,20 +101,26 @@ backup()
  fi
 
  #expand the exclude-path for use with tar
- exf=`ssh $computer "ls ~/.backup/exclude"`
+ exf=`ssh root@$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 cfCl - $filesystem . $lastcmd --exclude-from=$exf | gzip"
- if `ssh -c blowfish $computer "$tar cfCl - $filesystem . $lastcmd \
-      --exclude-from=$exf \
-        | gzip" \
-   |  dd of=$DATE.tmp 2>/dev/null` 
- then
-  #perhaps it did work
-  mv $DATE.tmp $DATE.tgz
+ #    z gzip it
+ #    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 root@$computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \
+       --exclude-from=$exf\" > $TARFILE"
+ echo `date`" $computer: Running $TARCMD" >&2
+ eval $TARCMD;
+ if [ -s $TARFILE ]; then
+  mv $TARFILE $DATE.tgz
   #make a filelist.
   #update the datefile if the filelist is ok.
   tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
@@ -97,7 +128,7 @@ backup()
     touch -t $DATE ../.date
 
   #make a sortet filelist
-  grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx
+  grep -v ^d $DATE.idx | sort -n -r -k 2 > $DATE.sdx
 
   #fix perm
   chmod 600 *tgz                      #only for us
@@ -105,9 +136,11 @@ 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`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
+
+  # We don't want to return 0
+  R=1
  fi
 
 }
@@ -116,31 +149,77 @@ backup()
 for computer in $unixcomputers
 do
 ( 
+ # Check that the target filesystem is mounted 
+ # (actually check that it's not the root filesystem)
+ rootfilesystem=`df -P /`
+ targetfilesystem=`df -P "$storagedir/$computer"`
+ if [ "$rootfilesystem" == "$targetfilesystem" ]; then
+       echo `date`" $computer: Target filesystem ($storagedir/$computer) was mounted on /. Aborting"
+       continue
+ fi
+
+ echo `date`" $computer: Backing up $computer" >&2
+
+ # Try to SSH to the computer without entering a password.
+ if ! `ssh -n -o NumberOfPasswordPrompts=0 root@$computer /bin/true`; then
+  echo `date`" $computer: Could not use passwordless SSH."
+
+  # We don't want to return 0
+  R=1
+  # Do next computer
+  continue;
+ fi
  #tømmer variabelen for sikkerhets skyld
  filesystems=""
 
  #Sjekker nest siste felt i fstab. Om det er 0 tar vi ikke backup
- filesystems=`ssh -n $computer "cat /etc/fstab" \
+ filesystems=`ssh -n root@$computer "cat /etc/fstab" \
   | grep -v nfs \
   | grep -v "^#" \
   | grep -v "^$" \
   | awk '{ if ( $(NF-1) != "0" ) print $2}' `
 
  #clean up our dir at this client
- ssh $computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"
+ if ! ssh root@$computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then
+  echo `date`" $computer: Could not create backup staging area at $computer:~/.backup - skipping backup of $computer"
+  # We don't want to return 0
+  R=1
+  continue;
+ fi
 
  #try to copy $exclude to $computer
- scp $exclude $computer:~/.backup/exclude ||
-    echo "could not copy exclude.txt to $computer :("
+ if ! scp $exclude root@$computer:~/.backup/exclude > /dev/null; then
+  echo `date`" $computer: Could not copy exclude.txt to $computer - skipping backup of $computer"
+  # We don't want to return 0
+  R=1
+  continue;
+ 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
+# TODO: Gah, clean this mess!
+ [ -f $confdir/preexec.$computer ] && (
+    scp $confdir/preexec.$computer  root@$computer:~/.backup/preexec >&2 ||
+     ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec - skipping backup of $computer"
+       R=1
+       continue
+     )
+    )
+ [ -f $confdir/postexec.$computer ] && (
+    scp $confdir/postexec.$computer root@$computer:~/.backup/postexec >&2 ||
+     ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec - skipping backup of $computer"
+       R=1
+       continue
+     )
+    )
 
  #try to run preexec if it exist
- ssh $computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec"
+ if ! ssh root@$computer "[ ! -f ~/.backup/preexec ] || /bin/bash -x ~/.backup/preexec" >&2; then
+       echo `date`" $computer: Could not run $computer:~/.backup/preexec - skipping backup of $computer"
+       R=1
+       continue
+ fi
+
 
  for filesystem in $filesystems
  do
@@ -150,7 +229,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
 
@@ -165,15 +243,13 @@ 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
  
   #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
@@ -186,10 +262,10 @@ do
   fi
 
   #delete complete backups
-  for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf`
+  for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$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
 
@@ -200,24 +276,28 @@ 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
- ssh $computer "[ -f ~/.backup/postexec ] && /bin/bash -x ~/.backup/postexec"
+ if ! ssh root@$computer "[ ! -f ~/.backup/postexec ] || /bin/bash -x ~/.backup/postexec" >&2; then
+       echo `date`" $computer: Could not run $computer:~/.backup/postexec"
+       R=1
+ fi
 
-)  
-done &
+) &
+done
 
 wait
 
-#create report
-/home/backup/report.sh $storagedir > /tmp/report.txt
-#[ ! -z $remotestat ] && scp /tmp/report.txt $remotestat
-###
-
-# print diskusage to logfile
-df -k
-
-date
+# Remove lockfile
+rm $LOCKFILE
+
+# Did anything go wrong?
+# (IMPORTANT NOTE: The R-business does not work as expected, since this script
+# forks itself.
+if [ $R != 0 ]; then
+       echo `date`": Backup run ended with errors, check logs."
+       exit 1
+else
+       echo `date`": Backup run ended" >&2
+fi