]> git.sesse.net Git - backup.sh/blobdiff - backup.sh
Less forks!
[backup.sh] / backup.sh
index 0a4cb4ac2a61e7869b115fe1777974f3bd40f201..5bbf1378b09c83cf736881e59926fd752b5d1f02 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -3,14 +3,21 @@
 # Locking
 LOCKFILE=/home/backup/backuprun.lockfile
 
+# Initially, we consider everything to be just fine.
+R=0
+
 # Die gracefully (ie. remove lockfile)
 die() {
-       rm $LOCKFILE
+       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
@@ -49,11 +56,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å
@@ -94,6 +100,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)
@@ -104,18 +111,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"
+ TARCMD="ssh $computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \
+       --exclude-from=$exf\" > $TARFILE"
  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
+
+ if eval $TARCMD; then
   mv $TARFILE $DATE.tgz
   #make a filelist.
   #update the datefile if the filelist is ok.
@@ -134,6 +134,9 @@ backup()
   #it did not work
   rm $TARFILE
   echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
+
+  # We don't want to return 0
+  R=1
  fi
 
 }
@@ -149,6 +152,9 @@ do
   echo "Passwordless SSH to $computer works."
  else
   echo "Could not use passwordless SSH to $computer."
+
+  # We don't want to return 0
+  R=1
   break;
  fi
  
@@ -167,23 +173,34 @@ do
  #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"
+  # 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"
+  # 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 "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 "Could not copy postexec.$computer to $computer:~/.backup/postexec"
+       break
+       R=1
+     )
     )
 
  #try to run preexec if it exist
@@ -244,8 +261,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
@@ -260,5 +275,13 @@ wait
 echo "Current disk usage:"
 df -k $storagedir
 
-echo `date`": Backup run ended"
+# Remove lockfile
+rm $LOCKFILE
 
+# 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"
+fi