-
- printf "%-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup"
-
- if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
- then
- lastd=`cat ../.date`
- lastcmd="--newer='$lastd'"
- else
- lastcmd=""
- fi
-
- #if this client has a special tar
- #we need to find a better solution to this conf-issue
- if [ -f $confdir/tar.$computer ] ; then
- tar=`cat $confdir/tar.$computer`
- else
- tar=tar
- fi
-
- #expand the exclude-path for use with tar
- exf=`ssh $computer "ls ~/.backup/exclude"`
-
- #We try to run tar on the remote computer
- # 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 $TARFILE $DATE.tgz
- #make a filelist.
- #update the datefile if the filelist is ok.
- tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
- echo $DATEs > ../.date &&
- touch -t $DATE ../.date
-
- #make a sortet filelist
- grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx
-
- #fix perm
- chmod 600 *tgz #only for us
- chmod 644 *sdx *idx 2>/dev/null #everyone can read
-
- else
- #it did not work
- rm $TARFILE
- echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
- fi
-
-}
-
-#Løper gjennom listen av unixmaskiner som vi skal ta backup av
-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."
- break;
- 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" \
- | grep -v nfs \
- | grep -v "^#" \
- | 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"
- 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"
- break;
- fi
-
- #try to copy preeexec and postexec if they exist
- [ -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"
-
- for filesystem in $filesystems
- do
- #lager en variant uten tegnet "/" eller $ (gjelder NT)
- sfilesystem=`echo $filesystem | tr '\/\$' '__'`
-
- #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
-
- #set default backuplevel
- backuplevel=daglig
-
- if [ ! -f $storagedir/$computer/$sfilesystem/.date ]
- then
- #take the first full backup of this filesystem on this computer
- backuplevel=full
- echo $DATEs > $storagedir/$computer/$sfilesystem/.date
- fi
-
- #sjekker om det er på tide med en full
- if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]; then
- backuplevel=full
- fi
+ echo -n `date` >&2
+ printf " $computer: $computer:$filesystem $backuplevel backup\n" >&2
+
+ SNARFILE="${storagedir}/${computer}/${sfilesystem}/.incremental.snar"
+ incrementalsnar="/root/.backup/${sfilesystem}.snar"
+ if [ "$backuplevel" = "daglig" ]
+ then
+ # If incremental backup, we need to copy the incremental status to $computer.
+ # If it does not exist, whine a bit and then run date-based instead.
+ if [ -s "$SNARFILE" ]; then
+ if ! scp $SNARFILE root@$computer:$incrementalsnar; then
+ diemsg "Could not copy .incremental.snar to $computer"
+ fi
+ lastcmd="--listed-incremental=$incrementalsnar --no-check-device"
+ else
+ echo `date`" $computer: Missing incremental.snar for $filesystem, doing date-based backup instead until next full backup" >&2
+ incrementalsnar=""
+ lastd=`cat ../.date`
+ lastcmd="--newer='$lastd'"
+ fi
+ else
+ lastcmd="--listed-incremental=$incrementalsnar"
+ fi
+
+ # We try to run tar on the remote computer
+ # c create archive
+ # C change to directory first
+ # . where to start taring (see C)
+ # $lastcmd only files newer than this
+ # --one-file-system don't traverse file systems
+ # --use-compress-program compress it using gzip or pigz
+ # --exclude-from file to get exclusion pattern from
+ # Pipe the stuff over ssh to ourselves, run pee to cat the contents to a
+ # file and build a file list, respectivly.
+ TARFILE=$DATE.tmp
+ TARCMD="ssh root@$computer \"nice -n 19 ionice -c3 tar --one-file-system --use-compress-program $compressor -cf - -C $filesystem . $lastcmd \
+ --exclude-from=/root/.backup/exclude\" | pee \"cat > $TARFILE\" \"tar tzvf -\""
+ echo `date`" $computer: Running $TARCMD" >&2
+ eval $TARCMD > $DATE.idx
+
+ if [ $? -eq 0 ] && [ -s $TARFILE ]; then
+ # File is >0 in size and neither cat or tar tzvf failed; we assume it worked.