Du bist hier: Snippet-Verzeichnis » UNIX Admin (199)
Sprache:

Backup on CD-R or RW

Sprache: English
Programmiersprache: Unix Shell
Veröffentlicht von: auriocus [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 971


Beschreibung

This is simple shellscript very similar to my multimount/multiumount scripts. It is suitable for doing incremental backup on directories holding not too much so it fits on a single CD-R. If you set the Variables in the beginning correctly, you can just run ./backup.sh /some/directory and then insert a blank CD-R into the writer. The first time you run it, the shell script just does a full backup of the directory. There is one extra feature: If you include a file named "EXCLUDELIST" in any of the subdirectories, that can contain shellscript patterns ( separated by newline ), all files under this subdirectory that match the pattern are not written. This is for example useful if you want to exclude all object files, because they can easily be recompiled, just put "*.o" into "EXCLUDELIST" in the toplevel directory.When you run it a second time on the same tree and have inserted the same CD-R, the script checks what files have changed ( according to their modification time ) or if you have added new files, and writes them as the next session on the CD-R. So it gives you incremental backup, but with easy access, because the multisesion logic takes care about that you get the newest version always if you mount the CD-R regularly. Limits:- doesn't check if there is enough space- relys on that the clock is right- doesn't automatically blank a CD-RW if this would free space- quite strange program, slow- writes an image instead of using a pipe from mkisofs to cdrecord- needs bash and the GNU-shell tools, otherwise it will not work, because it relys on some specific behaviour

Code

1 #!/bin/bash 2 # 3 # DOes incremental backup of a directory on CD-R or CD-RW 4 5 DEVICE="/dev/scd0" 6 RAWDEVICE="/dev/sg0" 7 MOUNTPOINT="/cdwriter/"; 8 SHADOWDIR="/tmp/backupshadow" 9 TMPIMAGE="/tmp/backup.img" 10 11 12 13 echo "Please insert a blank disc or an already created backup into $DEVICE" 14 read 15 echo "Getting disc info" 16 MULTI_INFO=`cdrecord -msinfo dev=$RAWDEVICE 2>/dev/null` 17 MULTISESSION=`echo $MULTI_INFO | wc -w` 18 # if it is a multisession fixed CD, we got only one word 19 # if it is a blank disk, or not multisession fixed, we got more than one word 20 21 echo "Shadowing your files to " $SHADOWDIR 22 mkdir $SHADOWDIR 23 for i in $*; do 24 if [ -f $i ]; then 25 ln -s $i $SHADOWDIR 26 echo $i 27 # lndir does the echoing itself... 28 else 29 lndir $i $SHADOWDIR 30 fi 31 done 32 33 echo "Deleting the shadows you don't want to be backed up" 34 # this allows to put a file in the toplevel directory 35 # that contains *.o 36 # and no object-files are backed up any longer 37 for DIR in $(find $SHADOWDIR -type d); do 38 # if the file EXCLUDELIST is present 39 # then remove it's contents 40 if [ -f $DIR/EXCLUDELIST ]; then 41 while read EXCLUDEEXPR; do 42 echo find $DIR -depth -name "$EXCLUDEEXPR" -exec rm -rf {} \; 43 find $DIR -depth -name "$EXCLUDEEXPR" -exec rm -rf {} \; 44 done < $DIR/EXCLUDELIST 45 fi 46 done 47 48 date > $SHADOWDIR/DATE 49 # creating the reference file for later time-comparing 50 51 52 if [ $MULTISESSION == 1 ]; then 53 echo "Mounting the old filesystem on CD to get the time of the last backup" 54 mount $DEVICE 55 if ! [ -f $MOUNTPOINT/DATE ]; then 56 echo "HELP !!!" 57 echo " The DATE file is missing, which means that this is not a CD that I created. Fix this" 58 echo "Leaving the disc untouched"; 59 rm -r $SHADOWDIR 60 umount $DEVICE 61 exit 1; 62 fi 63 64 find $SHADOWDIR -follow -type f -and -not -newer $MOUNTPOINT/DATE | 65 while read not_newer_file; do 66 PUREFILENAME=`echo $not_newer_file | sed "s|^$SHADOWDIR||g"` 67 if [ -e "$MOUNTPOINT/$PUREFILENAME" ]; then 68 # this file is already on the CD 69 # don't write it a second time --> remove the shadow link 70 rm "$not_newer_file" 71 else 72 echo "Adding " $PUREFILENAME 73 fi 74 done; 75 76 umount $DEVICE 77 78 echo Writing files: 79 #find $SHADOWDIR 80 81 echo " If I should write them now, press Enter, else Ctrl-C" 82 read 83 84 mkisofs -R -T -f -o $TMPIMAGE -C $MULTI_INFO -M $DEVICE $SHADOWDIR 85 # now burn it to disc 86 cdrecord dev=$RAWDEVICE -multi -eject -v speed=4 $TMPIMAGE 87 rm $TMPIMAGE 88 89 rm -rf $SHADOWDIR 90 91 else 92 echo "Warning: No multisession disc. I assume you have inserted a blank disc" 93 echo "If I should do a full backup now, hit enter, otherwise Ctrl-C" 94 read 95 96 97 mkisofs -R -T -f -o $TMPIMAGE $SHADOWDIR 98 cdrecord dev=$RAWDEVICE -multi -eject -v speed=4 $TMPIMAGE 99 rm $TMPIMAGE 100 rm -r $SHADOWDIR 101 fi

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS