4 LOCKFILE=/home/backup/backuprun.lockfile
6 # Initially, we consider everything to be just fine.
9 # Die gracefully (ie. remove lockfile)
11 echo `date`": Something nasty happened, and since I fork a lot:"
12 echo `date`": I CANNOT CLEAN UP THE MESS MYSELF."
13 echo `date`": You need to get rid of lost process named stuff like $0, tar and ssh."
18 trap die SIGINT SIGTERM
20 # Don't start if we're already running
21 if [ -e $LOCKFILE ]; then
22 echo `date`": $LOCKFILE exists, exiting."
28 confdir=/home/backup/conf/ #configuration files
29 storagedir=/backup #mountpoint of huge disc
33 exclude=$confdir/exclude
34 [ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude
37 #syntax of remotestat:
39 remotestatf=$confdir/remotestat
40 [ -f $remotestatf ] && remotestat=`cat $remotestatf`
43 PATH=/local/bin:$PATH:/store/bin
47 echo `date`": Backup run starting" >&2
51 # The computers we want to back up
52 unixcomputers=`cat $confdir/computers.unix \
56 # Backup only one computer, from command line?
61 #disse bør kunne varieres fra fs til fs?
62 maksantallfulle=3 #hvor mange fulle vi tar vare på
63 dagermellomfulle=30 #antall dager før det er på tide med ny full
68 DATE=`date "+%Y%m%d%H%M"` #format: touch
69 DATEs=`date "+%Y-%m-%d %H:%M"` #format: tar
73 # krever at noen variabler er satt
74 # krever at vi er i rett katalog
79 printf " $computer: %-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" >&2
81 if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ]
84 lastcmd="--newer='$lastd'"
89 #if this client has a special tar
90 #we need to find a better solution to this conf-issue
91 if [ -f $confdir/tar.$computer ] ; then
92 tar=`cat $confdir/tar.$computer`
97 #expand the exclude-path for use with tar
98 exf=`ssh root@$computer "ls ~/.backup/exclude"`
100 #We try to run tar on the remote computer
103 # C change to directory first
104 # - output to stdout (we pipe to gzip, then to dd)
105 # . where to start taring (see C)
106 # $lastcmd only files newer than this
107 # --exclude-from file to get exclusion pattern from
108 # pipe to gzip, which in turn pipes over the ssh-stream
109 # ..to dd, to output to a file. We surpress messages from dd.
110 # And at last, redirect stderr to stdout, to get output logged.
112 TARCMD="ssh root@$computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \
113 --exclude-from=$exf\" > $TARFILE"
114 echo `date`" $computer: Running $TARCMD" >&2
116 if eval $TARCMD; then
117 mv $TARFILE $DATE.tgz
119 #update the datefile if the filelist is ok.
120 tar tvfz $DATE.tgz > $DATE.idx 2>&1 &&
121 echo $DATEs > ../.date &&
122 touch -t $DATE ../.date
124 #make a sortet filelist
125 grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx
128 chmod 600 *tgz #only for us
129 chmod 644 *sdx *idx 2>/dev/null #everyone can read
134 echo `date`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
136 # We don't want to return 0
142 #Løper gjennom listen av unixmaskiner som vi skal ta backup av
143 for computer in $unixcomputers
146 echo `date`" $computer: Backing up $computer" >&2
148 # Try to SSH to the computer without entering a password.
149 if ! `ssh -n -o NumberOfPasswordPrompts=0 root@$computer /bin/true`; then
150 echo `date`" $computer: Could not use passwordless SSH."
152 # We don't want to return 0
158 #tømmer variabelen for sikkerhets skyld
161 #Sjekker nest siste felt i fstab. Om det er 0 tar vi ikke backup
162 filesystems=`ssh -n root@$computer "cat /etc/fstab" \
166 | awk '{ if ( $(NF-1) != "0" ) print $2}' `
168 #clean up our dir at this client
169 if ! ssh root@$computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then
170 echo `date`" $computer: Could not create backup staging area at $computer:~/.backup - skipping backup of $computer"
171 # We don't want to return 0
176 #try to copy $exclude to $computer
177 if ! scp $exclude root@$computer:~/.backup/exclude > /dev/null; then
178 echo `date`" $computer: Could not copy exclude.txt to $computer - skipping backup of $computer"
179 # We don't want to return 0
184 #try to copy preeexec and postexec if they exist
185 # TODO: Gah, clean this mess!
186 [ -f $confdir/preexec.$computer ] && (
187 scp $confdir/preexec.$computer root@$computer:~/.backup/preexec >&2 ||
188 ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec - skipping backup of $computer"
193 [ -f $confdir/postexec.$computer ] && (
194 scp $confdir/postexec.$computer root@$computer:~/.backup/postexec >&2 ||
195 ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec - skipping backup of $computer"
201 #try to run preexec if it exist
202 if ! ssh root@$computer "[ ! -f ~/.backup/preexec ] || /bin/bash -x ~/.backup/preexec" >&2; then
203 echo `date`" $computer: Could not run $computer:~/.backup/preexec - skipping backup of $computer"
209 for filesystem in $filesystems
211 #lager en variant uten tegnet "/" eller $ (gjelder NT)
212 sfilesystem=`echo $filesystem | tr '\/\$' '__'`
214 #lager det som trengs av kataloger
215 mkdir -m 755 -p $storagedir/$computer/$sfilesystem/{full,daglig} 2>/dev/null
217 echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name
218 chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name
220 #set default backuplevel
223 if [ ! -f $storagedir/$computer/$sfilesystem/.date ]
225 #take the first full backup of this filesystem on this computer
227 echo $DATEs > $storagedir/$computer/$sfilesystem/.date
230 #sjekker om det er på tide med en full
231 if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$dagermellomfulle`" ]; then
235 #gå ned i rett katalog, eller dø
236 # TODO bør sende mail om dette skjer!
237 cd $storagedir/$computer/$sfilesystem/$backuplevel || die
239 #perform the actual backup
242 # Sjekk om det skal være et annet antall fulle backuper av en boks
243 if [ -f $confdir/maksfulle.$computer ] ; then
244 mf=$((`cat $confdir/maksfulle.$computer`+1))
246 mf=$(($maksantallfulle+1))
249 #delete complete backups
250 for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf`
252 prefix=`echo $full | sed "s/\.[^.]*$//"`
253 echo `date`": $computer:$filesystem sletter full $prefix (for mange)" >&2
257 #delete incremental backups older than the oldest complete backup
258 oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz | tail -1`
260 $storagedir/$computer/$sfilesystem/daglig \
266 #try to run postexec if it exist
267 if ! ssh root@$computer "[ ! -f ~/.backup/postexec ] || /bin/bash -x ~/.backup/postexec" >&2; then
268 echo `date`" $computer: Could not run $computer:~/.backup/postexec"
280 # Did anything go wrong?
281 # (IMPORTANT NOTE: The R-business does not work as expected, since this script
284 echo `date`": Backup run ended with errors, check logs."
287 echo `date`": Backup run ended" >&2