#!/bin/bash ########################################################################################## # bashup.sh # alotlikeyesterday@gmail.com # # A simple bash script to backup scattered files throughout a linux box # into a gzipped tar archive with option to send to cd burner. # # Todo: # -make TYPE=2 - add trailing "/" so files written to original locations # upon untarring, as opposed to TYPE=0, where directory structure is # maintaned, but written in PWD # -fix nameing scheme of archive when written to cd # # Updated: 5/14/2005 # ############ DEFINE ###################################################################### TYPE=1 #0-create archive keeping original path of files, from cwd #1-create archive in dated directory DATA=( /home/dir1/fileA.txt /var/log/ /home/userdir/ /etc/httpd.conf ) # name the compressed archive date=$(date +"%m%d%y") ARCHIVENAME="backup_$date.tar" fullname=$ARCHIVENAME.gz # location of backup directory BACKUPDIR=/backups ############Check if archive already exists#### if [ -e $BACKUPDIR/$fullname ] then echo -n "->$fullname already exists, overwrite? [y,n]: " read overwrite if [ "$overwrite" == "y" ] then rm -rf $BACKUPDIR/$fullname echo " ->$fullname deleted." else echo "Goodbye" exit 0 fi fi ######### Count the number of Data elements ##### element_count=${#DATA[@]} echo "->There are $element_count elements to be archived..." ############Check if each Data element exists#### echo "->Checking data..." index=0 while [ "$index" -lt "$element_count" ] do if [ -e ${DATA[$index]} ] then echo " ->${DATA[$index]}..............exists" else echo " ->${DATA[$index]}..............does not exist, please check your desired data" exit 0 fi let "index = $index +1" done #################################################### echo "->Adding Data to the archive..." ######### CREATE TYPE 0 ARCHIVE #################### if [ "$TYPE" == 0 ] then #create archive,starting with first element tar -cvf $ARCHIVENAME ${DATA[0]} > /dev/null index=1 #add the rest of the elements to the archive while [ "$index" -lt "$element_count" ] do echo " ->Adding ${DATA[$index]}" tar -r --file=$ARCHIVENAME ${DATA[$index]} > /dev/null let "index = $index +1" done gzip $ARCHIVENAME ARCHIVENAME=$ARCHIVENAME.gz fi ######### CREATE TYPE 1 ARCHIVE ##################### if [ "$TYPE" == 1 ] then # create compressed archive, copy to backup directory tmpdir=/backup_$date sudo mkdir -m 777 $tmpdir index=0 while [ "$index" -lt "$element_count" ] do echo " ->Adding ${DATA[$index]}" cp -r ${DATA[$index]} $tmpdir let "index = $index +1" done tar -cvf $ARCHIVENAME $tmpdir > /dev/null gzip $ARCHIVENAME ARCHIVENAME=$ARCHIVENAME.gz fi ###### CHECK THE SIZE OF THE ARCHIVE ########## size=`du -k $ARCHIVENAME |sed s/$ARCHIVENAME//` echo "->The size of the archive is $size kilobytes" ##### MOVE TO BACKUP AREA, CLEAN UP ########### cp $ARCHIVENAME $BACKUPDIR sudo mv $ARCHIVENAME /backups if [ "$TYPE" == 1 ] then sudo rm -rf $tmpdir fi ###### CHECK THAT THE BACKUP WAS CREATED ###### if [ -e $BACKUPDIR/$ARCHIVENAME ] then success=1 echo "->Backup was successful, moved to $BACKUPDIR" else echo "->Backup was unsuccessful, time to troubleshoot" exit 0 fi ########## BURN TO CD ######################### # see http://mirror.centos.org/centos/4/docs/html/rhel-sbs-en-4/s1-disks-cdrw.html # also http://www-structmed.cimr.cam.ac.uk/Local/CD.html if [ "$size" -lt "700000" ] then echo -n "->Do you want to send the archive to CD? [y,n]: " read burn if [ "$burn" == "y" ] && [ "$success" == "1" ] then echo "->Burning to disc..." mkisofs -r -o backup.iso $BACKUPDIR/$ARCHIVENAME cdrecord -v -eject dev=/dev/hdc backup.iso rm -rf backup.iso echo "->Disc is burned, please check." else echo "->OK, archive is in $BACKUPDIR" fi else echo "The archive is too big ($size kb) to write to CD" fi