X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=backup.sh;h=a5cc91570a4455e7f90a258a481f761295070fdb;hb=ebd6ca30a863bc9d9d073ab2ea5760f248d2034e;hp=e39cade4655560d67dd23dcd0eb8350004d333d2;hpb=a65ec8e403c92194c28bb6e3474778f5b0696ce0;p=backup.sh diff --git a/backup.sh b/backup.sh index e39cade..a5cc915 100755 --- a/backup.sh +++ b/backup.sh @@ -20,6 +20,7 @@ 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 @@ -44,10 +45,7 @@ PATH=/local/bin:$PATH:/store/bin export PATH #start the logfile -echo `date`": Backup run starting" -echo "Exclude pattern:" -cat $exclude -echo "End exclude pattern" +echo `date`": Backup run starting" >&2 umask 027 @@ -78,7 +76,8 @@ 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 @@ -97,9 +96,10 @@ 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 + # z gzip it # c create archive # C change to directory first # - output to stdout (we pipe to gzip, then to dd) @@ -110,18 +110,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" - 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. + 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 - 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. @@ -130,7 +123,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 @@ -139,7 +132,7 @@ backup() else #it did not work rm $TARFILE - echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" + echo `date`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" # We don't want to return 0 R=1 @@ -151,66 +144,68 @@ backup() for computer in $unixcomputers do ( - echo `date`": Backing up $computer" + echo `date`" $computer: Backing up $computer" >&2 # 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." + 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 - break; + # 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}' ` - 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" + 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 - break; + continue; fi #try to copy $exclude to $computer - if ! scp $exclude $computer:~/.backup/exclude > /dev/null; then - 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 - break; + continue; 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"; + 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 - break + continue ) ) [ -f $confdir/postexec.$computer ] && ( - scp $confdir/postexec.$computer $computer:~/.backup/postexec || - ( echo "Could not copy postexec.$computer to $computer:~/.backup/postexec" - break + 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 @@ -253,10 +248,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 @@ -270,24 +265,25 @@ do 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 & wait -# print diskusage to logfile -echo "Current disk usage:" -df -k $storagedir - # 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" + echo `date`": Backup run ended" >&2 fi