]> git.sesse.net Git - backup.sh/blobdiff - backup.sh
Cosmetic cleanups: Better comments, logging and configuration variables readability
[backup.sh] / backup.sh
index bf9c59af7cb82db3057a95f51e49b2a0918c5503..10aa460d28641d62f9c2a72137e96711bff5e351 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -1,12 +1,52 @@
 #!/bin/sh
-
-# Locking
+# backup.sh
+# 
+# Backup up Unix-like computers
+# 
+# itk@samfundet.no
+#
+# This script was first checked into RCS in 2000, and has since backed up a few
+# servers almost every day, whenever not broken.
+# 
+# It's a bit peculiar, but quite effective. The following is worth keeping in
+# mind when hacking:
+#
+# KISS. And by that I mean really simple. Only regular Unix commands, please
+# (but GNU extensions are used).
+#
+# Because of the way we run this script from cron, informational logging goes
+# to stderr and warnings and errors to stdout. This way, we get mail whenever
+# stuff fails, and other output is redirected to the log file.
+# 
+# The computers are backed up in parallell (but the filesystems on each
+# computer in serial). This is implemented highly simplistic, with forking
+# subshells. The script has no concept about threads, and stuff might go wrong,
+# which usually requires manual cleanups.
+#
+# The script is halfway Norwegian and halfway English. Newer modifications are
+# in English, we should probably stick to that.
+
+# TODO(sesse, 2007-12-16): 
+#  * Bruk tee i stedet for en separat tar ztvf-prosess
+#  * Sjekk hvorfor .idx-filene til UD er blitt 0 for siste fullbackuper
+
+# Configuration
 LOCKFILE=/home/backup/backuprun.lockfile
+confdir=/home/backup/conf/           # Configuration files
+storagedir=/backup                   # Where we keep backups
+maksantallfulle=3                    # Number of full backups
+dagermellomfulle=30                  # Days between full backups
+DATE=`date "+%Y%m%d%H%M"`            #format: touch
+DATEs=`date "+%Y-%m-%d %H:%M"`       #format: tar
+
+# Exclude-pattern
+exclude=$confdir/exclude
+[ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude
 
 # Initially, we consider everything to be just fine.
 R=0
 
-# Die gracefully (ie. remove lockfile)
+# 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."
@@ -25,16 +65,6 @@ if [ -e $LOCKFILE ]; then
 fi;
 touch $LOCKFILE
 
-#dirs
-confdir=/home/backup/conf/           #configuration files
-storagedir=/backup                   #mountpoint of huge disc
-###
-
-#exclude-pattern
-exclude=$confdir/exclude
-[ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude
-###
-
 #syntax of remotestat:
 #hostname:/directory/
 remotestatf=$confdir/remotestat
@@ -44,7 +74,6 @@ remotestatf=$confdir/remotestat
 PATH=/local/bin:$PATH:/store/bin
 export PATH
 
-#start the logfile
 echo `date`": Backup run starting" >&2
 
 umask 027
@@ -59,16 +88,6 @@ if [ $1 ] ; then
   unixcomputers=$1
 fi;
 
-#disse bør kunne varieres fra fs til fs?
-maksantallfulle=3          #hvor mange fulle vi tar vare på
-dagermellomfulle=30        #antall dager før det er på tide med ny full
-logw=40
-###
-
-#lager datovariabeler
-DATE=`date "+%Y%m%d%H%M"`            #format: touch
-DATEs=`date "+%Y-%m-%d %H:%M"`       #format: tar
-###
 
 #selve backupen
 # krever at noen variabler er satt
@@ -77,7 +96,7 @@ backup()
 {
 
  echo -n `date` >&2
- printf " $computer: %-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" >&2
+ printf " $computer: $computer:$filesystem $backuplevel backup" >&2
 
  if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
  then
@@ -102,13 +121,11 @@ backup()
  #    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
+ #    --one-file-system don't traverse file systems
  #    --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.
+ #    And at last, redirect stdout to stderr, to get output logged.
  TARFILE=$DATE.tmp
  TARCMD="ssh root@$computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \
        --exclude-from=$exf\" > $TARFILE"
@@ -260,7 +277,7 @@ do
   for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf`
   do
    prefix=`echo $full | sed "s/\.[^.]*$//"`
-   echo `date`": $computer:$filesystem sletter full $prefix (for mange)" >&2
+   echo `date`": $computer:$filesystem Deleting full backup $prefix" >&2
    rm $prefix*
   done
 
@@ -270,7 +287,8 @@ do
      $storagedir/$computer/$sfilesystem/daglig \
      -type f \
      \! -newer $oldf \
-     -exec rm {} \;
+     -printf "`date`: $computer: Deleting old incremental backup: %p\n" \
+     -exec rm {} \; >&2
  done
 
  #try to run postexec if it exist
@@ -279,8 +297,8 @@ do
        R=1
  fi
 
-)  
-done &
+) &
+done
 
 wait