4 LOCKFILE=/home/backup/backuprun.lockfile
6 # Die gracefully (ie. remove lockfile)
12 trap die SIGINT SIGTERM
14 if [ -e $LOCKFILE ]; then
15 echo `date`": $LOCKFILE exists, exiting."
21 confdir=/home/backup/conf/ #configuration files
22 storagedir=/backup #mountpoint of huge disc
26 exclude=$confdir/exclude
27 [ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude
30 #syntax of remotestat:
32 remotestatf=$confdir/remotestat
33 [ -f $remotestatf ] && remotestat=`cat $remotestatf`
36 PATH=/local/bin:$PATH:/store/bin
40 echo `date`": Backup run starting"
41 echo "Exclude pattern:"
43 echo "End exclude pattern"
47 # The computers we want to back up
48 unixcomputers=`cat $confdir/computers.unix \
58 #disse bør kunne varieres fra fs til fs?
59 maksantallfulle=3 #hvor mange fulle vi tar vare på
60 dagermellomfulle=30 #antall dager før det er på tide med ny full
65 DATE=`date "+%Y%m%d%H%M"` #format: touch
66 DATEs=`date "+%Y-%m-%d %H:%M"` #format: tar
70 # krever at noen variabler er satt
71 # krever at vi er i rett katalog
75 printf "%-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup"
77 if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
80 lastcmd="--newer='$lastd'"
85 #if this client has a special tar
86 #we need to find a better solution to this conf-issue
87 if [ -f $confdir/tar.$computer ] ; then
88 tar=`cat $confdir/tar.$computer`
93 #expand the exclude-path for use with tar
94 exf=`ssh $computer "ls ~/.backup/exclude"`
96 #We try to run tar on the remote computer
98 # C change to directory first
99 # - output to stdout (we pipe to gzip, then to dd)
100 # . where to start taring (see C)
101 # $lastcmd only files newer than this
102 # --exclude-from file to get exclusion pattern from
103 # pipe to gzip, which in turn pipes over the ssh-stream
104 # ..to dd, to output to a file. We surpress messages from dd.
105 # And at last, redirect stderr to stdout, to get output logged.
107 TARCMD="ssh $computer \"$tar --one-file-system -cf - -C $filesystem . $lastcmd \
108 --exclude-from=$exf | gzip\" | (dd of=$TARFILE 2> /dev/null) 2>&1"
109 echo "cmdline: $TARCMD"
112 # Ideally, we should check wether the tar command returned 0 or not, but it
113 # seems a pipe in bash returns the value of the last command in the pipe.
114 # Instead, we check wether the resulting file has zero size, in which case we
115 # consider it an error.
116 if [ -s $TARFILE ]; then
117 echo `date`": command probably ran without errors."
119 mv $TARFILE $DATE.tgz
121 #update the datefile if the filelist is ok.
122 tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
123 echo $DATEs > ../.date &&
124 touch -t $DATE ../.date
126 #make a sortet filelist
127 grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx
130 chmod 600 *tgz #only for us
131 chmod 644 *sdx *idx 2>/dev/null #everyone can read
136 echo `date`": $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
141 #Løper gjennom listen av unixmaskiner som vi skal ta backup av
142 for computer in $unixcomputers
145 echo `date`": Backing up $computer"
147 # Try to SSH to the computer without entering a password.
148 if `ssh -n -o NumberOfPasswordPrompts=0 $computer /bin/true`; then
149 echo "Passwordless SSH to $computer works."
151 echo "Could not use passwordless SSH to $computer."
155 #tømmer variabelen for sikkerhets skyld
158 #Sjekker nest siste felt i fstab. Om det er 0 tar vi ikke backup
159 filesystems=`ssh -n $computer "cat /etc/fstab" \
163 | awk '{ if ( $(NF-1) != "0" ) print $2}' `
165 echo "Filesystems to backup on $computer: $filesystems"
167 #clean up our dir at this client
168 if ! ssh $computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then
169 echo "Could not create backup staging area at $computer:~/.backup"
173 #try to copy $exclude to $computer
174 if ! scp $exclude $computer:~/.backup/exclude > /dev/null; then
175 echo "Could not copy exclude.txt to $computer"
179 #try to copy preeexec and postexec if they exist
180 [ -f $confdir/preexec.$computer ] && (
181 scp $confdir/preexec.$computer $computer:~/.backup/preexec ||
182 ( echo "Could not copy preexec.$computer to $computer:~/.backup/preexec"; break )
184 [ -f $confdir/postexec.$computer ] && (
185 scp $confdir/postexec.$computer $computer:~/.backup/postexec ||
186 ( echo "Could not copy postexec.$computer to $computer:~/.backup/postexec"; break )
189 #try to run preexec if it exist
190 ssh $computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec"
192 for filesystem in $filesystems
194 #lager en variant uten tegnet "/" eller $ (gjelder NT)
195 sfilesystem=`echo $filesystem | tr '\/\$' '__'`
197 #lager det som trengs av kataloger
198 mkdir -m 755 -p $storagedir/$computer/$sfilesystem/{full,daglig} 2>/dev/null
200 echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name
201 chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name
203 #set default backuplevel
206 if [ ! -f $storagedir/$computer/$sfilesystem/.date ]
208 #take the first full backup of this filesystem on this computer
210 echo $DATEs > $storagedir/$computer/$sfilesystem/.date
213 #sjekker om det er på tide med en full
214 if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]; then
218 #gå ned i rett katalog, eller dø
219 # TODO bør sende mail om dette skjer!
220 cd $storagedir/$computer/$sfilesystem/$backuplevel || die
222 #perform the actual backup
225 # Sjekk om det skal være et annet antall fulle backuper av en boks
226 if [ -f $confdir/maksfulle.$computer ] ; then
227 mf=$((`cat $confdir/maksfulle.$computer`+1))
229 mf=$(($maksantallfulle+1))
232 #delete complete backups
233 for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf`
235 prefix=`echo $full | sed "s/\.[^.]*$//"`
236 echo "$computer:$filesystem sletter full $prefix (for mange)"
240 #delete incremental backups older than the oldest complete backup
241 oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz | tail -1`
243 $storagedir/$computer/$sfilesystem/daglig \
247 # #denne funker bare med gnu find
248 # #-printf "$computer:$filesystem sletter daglig %f (for gammel)\n"
251 #try to run postexec if it exist
252 ssh $computer "[ -f ~/.backup/postexec ] && /bin/bash -x ~/.backup/postexec"
259 # print diskusage to logfile
260 echo "Current disk usage:"
263 echo `date`": Backup run ended"