3 # TODO(sesse, 2007-12-16):
4 # * Fiks så parallelliseringen fungerer igjen
5 # * Bruk tee i stedet for en separat tar ztvf-prosess
6 # * Sjekk hvorfor .idx-filene til UD er blitt 0 for siste fullbackuper
9 LOCKFILE=/home/backup/backuprun.lockfile
11 # Initially, we consider everything to be just fine.
14 # Die gracefully (ie. remove lockfile)
16 echo `date`": Something nasty happened, and since I fork a lot:"
17 echo `date`": I CANNOT CLEAN UP THE MESS MYSELF."
18 echo `date`": You need to get rid of lost process named stuff like $0, tar and ssh."
23 trap die SIGINT SIGTERM
25 # Don't start if we're already running
26 if [ -e $LOCKFILE ]; then
27 echo `date`": $LOCKFILE exists, exiting."
28 echo `date`": $LOCKFILE exists, exiting." >&2
34 confdir=/home/backup/conf/ #configuration files
35 storagedir=/backup #mountpoint of huge disc
39 exclude=$confdir/exclude
40 [ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude
43 #syntax of remotestat:
45 remotestatf=$confdir/remotestat
46 [ -f $remotestatf ] && remotestat=`cat $remotestatf`
49 PATH=/local/bin:$PATH:/store/bin
53 echo `date`": Backup run starting" >&2
57 # The computers we want to back up
58 unixcomputers=`cat $confdir/computers.unix \
62 # Backup only one computer, from command line?
67 #disse bør kunne varieres fra fs til fs?
68 maksantallfulle=3 #hvor mange fulle vi tar vare på
69 dagermellomfulle=30 #antall dager før det er på tide med ny full
74 DATE=`date "+%Y%m%d%H%M"` #format: touch
75 DATEs=`date "+%Y-%m-%d %H:%M"` #format: tar
79 # krever at noen variabler er satt
80 # krever at vi er i rett katalog
85 printf " $computer: %-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" >&2
87 if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
90 lastcmd="--newer='$lastd'"
95 #if this client has a special tar
96 #we need to find a better solution to this conf-issue
97 if [ -f $confdir/tar.$computer ] ; then
98 tar=`cat $confdir/tar.$computer`
103 #expand the exclude-path for use with tar
104 exf=`ssh root@$computer "ls ~/.backup/exclude"`
106 #We try to run tar on the remote computer
109 # C change to directory first
110 # - output to stdout (we pipe to gzip, then to dd)
111 # . where to start taring (see C)
112 # $lastcmd only files newer than this
113 # --exclude-from file to get exclusion pattern from
114 # pipe to gzip, which in turn pipes over the ssh-stream
115 # ..to dd, to output to a file. We surpress messages from dd.
116 # And at last, redirect stderr to stdout, to get output logged.
118 TARCMD="ssh root@$computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \
119 --exclude-from=$exf\" > $TARFILE"
120 echo `date`" $computer: Running $TARCMD" >&2
122 if [ -s $TARFILE ]; then
123 mv $TARFILE $DATE.tgz
125 #update the datefile if the filelist is ok.
126 tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
127 echo $DATEs > ../.date &&
128 touch -t $DATE ../.date
130 #make a sortet filelist
131 grep -v ^d $DATE.idx | sort -n -r -k 2 > $DATE.sdx
134 chmod 600 *tgz #only for us
135 chmod 644 *sdx *idx 2>/dev/null #everyone can read
140 echo `date`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
142 # We don't want to return 0
148 #Løper gjennom listen av unixmaskiner som vi skal ta backup av
149 for computer in $unixcomputers
152 # Check that the target filesystem is mounted
153 # (actually check that it's not the root filesystem)
154 rootfilesystem=`df -P /`
155 targetfilesystem=`df -P "$storagedir/$computer"`
156 if [ "$rootfilesystem" == "$targetfilesystem" ]; then
157 echo `date`" $computer: Target filesystem ($storagedir/$computer) was mounted on /. Aborting"
161 echo `date`" $computer: Backing up $computer" >&2
163 # Try to SSH to the computer without entering a password.
164 if ! `ssh -n -o NumberOfPasswordPrompts=0 root@$computer /bin/true`; then
165 echo `date`" $computer: Could not use passwordless SSH."
167 # We don't want to return 0
173 #tømmer variabelen for sikkerhets skyld
176 #Sjekker nest siste felt i fstab. Om det er 0 tar vi ikke backup
177 filesystems=`ssh -n root@$computer "cat /etc/fstab" \
181 | awk '{ if ( $(NF-1) != "0" ) print $2}' `
183 #clean up our dir at this client
184 if ! ssh root@$computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then
185 echo `date`" $computer: Could not create backup staging area at $computer:~/.backup - skipping backup of $computer"
186 # We don't want to return 0
191 #try to copy $exclude to $computer
192 if ! scp $exclude root@$computer:~/.backup/exclude > /dev/null; then
193 echo `date`" $computer: Could not copy exclude.txt to $computer - skipping backup of $computer"
194 # We don't want to return 0
199 #try to copy preeexec and postexec if they exist
200 # TODO: Gah, clean this mess!
201 [ -f $confdir/preexec.$computer ] && (
202 scp $confdir/preexec.$computer root@$computer:~/.backup/preexec >&2 ||
203 ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec - skipping backup of $computer"
208 [ -f $confdir/postexec.$computer ] && (
209 scp $confdir/postexec.$computer root@$computer:~/.backup/postexec >&2 ||
210 ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec - skipping backup of $computer"
216 #try to run preexec if it exist
217 if ! ssh root@$computer "[ ! -f ~/.backup/preexec ] || /bin/bash -x ~/.backup/preexec" >&2; then
218 echo `date`" $computer: Could not run $computer:~/.backup/preexec - skipping backup of $computer"
224 for filesystem in $filesystems
226 #lager en variant uten tegnet "/" eller $ (gjelder NT)
227 sfilesystem=`echo $filesystem | tr '\/\$' '__'`
229 #lager det som trengs av kataloger
230 mkdir -m 755 -p $storagedir/$computer/$sfilesystem/{full,daglig} 2>/dev/null
232 echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name
233 chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name
235 #set default backuplevel
238 if [ ! -f $storagedir/$computer/$sfilesystem/.date ]
240 #take the first full backup of this filesystem on this computer
242 echo $DATEs > $storagedir/$computer/$sfilesystem/.date
245 #sjekker om det er på tide med en full
246 if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]; then
250 #gå ned i rett katalog, eller dø
251 # TODO bør sende mail om dette skjer!
252 cd $storagedir/$computer/$sfilesystem/$backuplevel || die
254 #perform the actual backup
257 # Sjekk om det skal være et annet antall fulle backuper av en boks
258 if [ -f $confdir/maksfulle.$computer ] ; then
259 mf=$((`cat $confdir/maksfulle.$computer`+1))
261 mf=$(($maksantallfulle+1))
264 #delete complete backups
265 for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf`
267 prefix=`echo $full | sed "s/\.[^.]*$//"`
268 echo `date`": $computer:$filesystem sletter full $prefix (for mange)" >&2
272 #delete incremental backups older than the oldest complete backup
273 oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz | tail -1`
275 $storagedir/$computer/$sfilesystem/daglig \
281 #try to run postexec if it exist
282 if ! ssh root@$computer "[ ! -f ~/.backup/postexec ] || /bin/bash -x ~/.backup/postexec" >&2; then
283 echo `date`" $computer: Could not run $computer:~/.backup/postexec"
295 # Did anything go wrong?
296 # (IMPORTANT NOTE: The R-business does not work as expected, since this script
299 echo `date`": Backup run ended with errors, check logs."
302 echo `date`": Backup run ended" >&2