X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=backup.sh;h=5bbf1378b09c83cf736881e59926fd752b5d1f02;hb=744c43e41f7db3dbf8cc9a2b8a6bee8b6fe84ee4;hp=36738bb09fbd752c9e336a4b51f40521ab964197;hpb=fb777aa73a7cb7b31217c6f0e44c181b151b6259;p=backup.sh diff --git a/backup.sh b/backup.sh index 36738bb..5bbf137 100755 --- a/backup.sh +++ b/backup.sh @@ -1,8 +1,31 @@ -#!/bin/bash +#!/bin/sh + +# Locking +LOCKFILE=/home/backup/backuprun.lockfile + +# Initially, we consider everything to be just fine. +R=0 + +# Die gracefully (ie. remove lockfile) +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." + exit 1 +fi; +touch $LOCKFILE #dirs confdir=/home/backup/conf/ #configuration files -webdir=/home/backup/public_html #web-pages storagedir=/backup #mountpoint of huge disc ### @@ -21,23 +44,25 @@ PATH=/local/bin:$PATH:/store/bin export PATH #start the logfile -date -echo +echo `date`": Backup run starting" echo "Exclude pattern:" cat $exclude -echo -echo +echo "End exclude pattern" umask 027 # The computers we want to back up -unixcomputers=`cat $confdir/computers.unix` -#ntcomputers=`cat $confdir/computers.nt` -### +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? -#disse bør flyttes ut i egen fil som kan editeres fra web kanskje? -maksantallfulle=2 #hvor mange fulle vi tar vare på +maksantallfulle=3 #hvor mange fulle vi tar vare på dagermellomfulle=30 #antall dager før det er på tide med ny full logw=40 ### @@ -57,22 +82,43 @@ backup() if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ] then - forrige=`cat ../.date` - forrigecmd="--newer=\"$forrige\"" + lastd=`cat ../.date` + lastcmd="--newer='$lastd'" else - forrigecmd="" + lastcmd="" fi - #Prøver å ta backup med tar - if `ssh $computer "cd $filesystem ;\ - tar cfl - . $forrigecmd \ - --exclude-from=/tmp/.backup/exclude \ - | gzip" \ - | dd of=$DATE.tmp 2>/dev/null` - then - #perhaps it did work - mv $DATE.tmp $DATE.tgz - #make a filelist and update the datefile + #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 + # 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 $computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \ + --exclude-from=$exf\" > $TARFILE" + echo "cmdline: $TARCMD" + + if eval $TARCMD; then + 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 @@ -81,13 +127,16 @@ backup() grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx #fix perm - chmod 600 *tgz - chmod 644 *sdx *idx + chmod 600 *tgz #only for us + chmod 644 *sdx *idx 2>/dev/null #everyone can read else #it did not work - rm $DATE.tmp 2>/dev/null - echo "$backuplevel backup of $computer:$filesystem failed" + rm $TARFILE + echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" + + # We don't want to return 0 + R=1 fi } @@ -95,6 +144,19 @@ backup() #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." + + # We don't want to return 0 + R=1 + break; + fi #tømmer variabelen for sikkerhets skyld filesystems="" @@ -102,25 +164,47 @@ do #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 "^#" \ + | grep -v "^$" \ | awk '{ if ( $(NF-1) != "0" ) print $2}' ` + echo "Filesystems to backup on $computer: $filesystems" + #clean up our dir at this client - ssh $computer "rm -rf /tmp/.backup ; mkdir -m 700 /tmp/.backup" + 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 - scp $exclude $computer:/tmp/.backup/exclude || - echo "Vi klarte ikke kopiere over exclude.txt til $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 - [ -f $confdir/preexec.$computer ] && - scp $confdir/preexec.$computer $computer:/tmp/.backup/preexec - [ -f $confdir/postexec.$computer ] && - scp $confdir/postexec.$computer $computer:/tmp/.backup/postexec +# 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"; + 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 + R=1 + ) + ) #try to run preexec if it exist - ssh $computer "[ -f /tmp/.backup/preexec ] && - /bin/bash -x /tmp/.backup/preexec" + ssh $computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec" for filesystem in $filesystems do @@ -130,9 +214,8 @@ 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 + echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name + chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name #set default backuplevel backuplevel=daglig @@ -145,23 +228,28 @@ 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 - cd $storagedir/$computer/$sfilesystem/$backuplevel + #gå ned i rett katalog, eller dø + # TODO bør sende mail om dette skjer! + cd $storagedir/$computer/$sfilesystem/$backuplevel || die #perform the actual backup backup + # Sjekk om det skal være et annet antall fulle backuper av en boks + if [ -f $confdir/maksfulle.$computer ] ; then + mf=$((`cat $confdir/maksfulle.$computer`+1)) + else + mf=$(($maksantallfulle+1)) + fi + #delete complete backups - mf=$(($maksantallfulle+1)) for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf` do - prefix=`echo $full|cut -d . -f 1` + prefix=`echo $full | sed "s/\.[^.]*$//"` echo "$computer:$filesystem sletter full $prefix (for mange)" rm $prefix* done @@ -170,22 +258,30 @@ do oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz | tail -1` find \ $storagedir/$computer/$sfilesystem/daglig \ + -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 /tmp/.backup/postexec ] && - /bin/bash -x /tmp/.backup/postexec" + ssh $computer "[ -f ~/.backup/postexec ] && /bin/bash -x ~/.backup/postexec" -done +) +done & -#create report -/home/backup/report.sh $storagedir > /tmp/report.txt -[ ! -z $remotestat ] && scp /tmp/report.txt $remotestat -### +wait + +# print diskusage to logfile +echo "Current disk usage:" +df -k $storagedir + +# Remove lockfile +rm $LOCKFILE -date +# 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