Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Nightly autobackup to multisession CD
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Gandalf_Grey_
Apprentice
Apprentice


Joined: 19 Apr 2003
Posts: 151

PostPosted: Wed Jan 14, 2004 2:21 am    Post subject: Nightly autobackup to multisession CD Reply with quote

When I got a 120gb hard drive for christmas it coincided with tthe completion of my fathers web page. I decided to make my desktop machine into a server running a 240 gigabyte raid 0 array. The only reservations I have is that this server was hosting several hundred hours worth of PHP coding as well as the mysql database for 2 websites. If even one of the three hard drives crashed all of this would be lost. I decided to write a shell script to automatically back up the contents of certain directories to a multisession CD at night. I would leave the CD in the CD RW drive and it would automatically open a new session every night and burn it. This is what I came up with. Oh yeah, and my script is called autobaka. To use it just chmod +x autobaka and move it to the roots PATH

EDITED (Already) - I forgot to remove the temp files and also took out some debugging output

Requirements

sed
cdrecord
bash (no idea if it works on any other shells)
tar

Any normal gentoo install should have all of the requirements except cdrecord.

And here is the source to my script

Code:

#!/bin/bash
printhelp () {

   echo "Usage: autobaka [OPTION]"
   echo
   echo "   --help   Print this message"
   echo "   --makenew   Create a new iso"
   echo "   --speed   The speed at which to write"
   echo "   --device   The writing device (in /dev/hdx format)"
   echo "   --directory   One of the directories you wanted backed up (can be repeated)"
   echo "   --message   An arbitrary message that you want put in the holder file"
   echo "         (cannot have spaces at the moment)"
   echo
   echo "Examples:"
   echo "autobaka --speed=32 --device=/dev/hdd --makenew --message=This_is_my_porn_backup"
   echo "autobaka --speed=32 --device=/dev/hdd --directory=/porn --directory=/more\ porn"
   echo "And ... well ... you get the idea"

   exit 0
}

makenew () {

   if [ "$1" != "" ]
   then
      echo "$3" > /tmp/message
   else
      echo "This was generated by Autobaka version $version" > /tmp/message
   fi

   mkisofs -o /tmp/init.iso -J -r -v -V Created_By_Autobaka /tmp/message
   cdrecord -v speed=$1 dev=$2 -multi /tmp/init.iso
   rm /tmp/init.iso
   rm /tmp/message
}

version=.01

if [ ! -n "$1" ]
then
   printhelp
fi

if [ -n "$1" ]
then

   currentdate=`date '+%d-%B-%Y--%H.%M.%S'`
   command="tar -zcvpf /tmp/backup-$currentdate.tar.gz"

   opmakenew=0

   for arg in "$@"

      do

         if [ "$arg" == "--help" ]
         then
            printhelp


         elif [ "$arg" == "--makenew" ]
         then
            opmakenew=1

         elif [ "$(echo $arg | cut -b -9)" == "--message" ]
         then
            message=$(echo $arg | cut -b 11-)

         elif [ "$(echo $arg | cut -b -7)" == "--speed" ]
         then
            speed=$(echo $arg | cut -b 9-)

         elif [ "$(echo $arg | cut -b -8)" == "--device" ]
         then
            device=$(echo $arg | cut -b 10-)

         elif [ "$(echo $arg | cut -b -11)"  == "--directory" ]
         then
            arg=$(echo $(echo $arg | sed "s' '\\\\ 'g") | cut -b 13-)
            command="$command -C . $arg"
         else
            help

         fi

         let "index+=1"

      done

   if [ "$opmakenew" == "1" ]
   then
      if [ "$device" != "" ]
      then

         if [ "$speed" == "" ]
         then
            echo "No speed value entered!"
            echo "Falling back on 1x"
            sleep 5
            speed=1
         fi
         makenew $speed $device $message
      else
         echo "You must enter at least a device for me to write to"
      fi
   else
      if [ "$speed" == "" ]
      then
         echo "No speed value entered!"
         echo "Falling back on 1x"
         sleep 5
         speed=1
      fi

      #do the writing bit
      #the reason I have to eject it is because if I don't then the newest session won't be recognised. I eject it twice for good measue ;)
      eject -r $device
      eject -tr $device
      eval $command
      echo $command
      mkisofs -o /tmp/$currentdate.iso -J -r -V $currentdate -C `cdrecord dev=$device -msinfo` -M $device /tmp/backup-$currentdate.tar.gz
      cdrecord -v speed=$speed dev=$device -multi /tmp/$currentdate.iso
      eject -r $device
      eject -tr $device
      rm /tmp/backup-$currentdate.tar.gz
      rm /tmp/$currentdate.iso
   fi
fi

exit 0


Stuff I intend to add


    Allow spaces in the message
    Have the program automatically mail root when the CD is getting low on space


In order to make in nightly just add save it in roots $PATH and add it as a cron job
_________________
Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect.
-- Linus Torvalds
Back to top
View user's profile Send private message
axxackall
l33t
l33t


Joined: 06 Nov 2002
Posts: 651
Location: Toronto, Ontario, 3rd Rock From Sun

PostPosted: Wed Jan 14, 2004 11:10 am    Post subject: Re: Nightly autobackup to multisession CD Reply with quote

Gandalf_Grey_ wrote:
Have the program automatically mail root when the CD is getting low on space


Perhaps my script may help you. I burns CD with a backup, but at first it checks if CD image can fit:

Code:

#!/bin/sh
                                                                                                                                             do_burn() {
                                                                                                                                             
cd ${BACKUP_WRK_DIR}
                                                                                                                                             MKISOCMD="-quiet -allow-lowercase -allow-multidot -l -relaxed-filenames -o backup.image.iso"
MKISOWHAT="${DIR_TO_BACKUP}"
                                                                                                                                             
HUGESIZE=`mkisofs --print-size ${MKISOCMD} ${MKISOWHAT} 2>/dev/null`
echo "HUGESIZE = ${HUGESIZE} of 2K blocks (317000 is the limit)"
                                                                                                                                             
if [ ${HUGESIZE} -gt 317000 ]
 then
  MKISOWHAT="-m *tmp* ${DIR_TO_BACKUP}"
  SMALLSIZE=`mkisofs --print-size ${MKISOCMD} ${MKISOWHAT} 2>/dev/null`
  MSG="Failed CD-archive size was: ${HUGESIZE} , writing ${SMALLSIZE}"
  echo ${MSG}
  echo ${MSG} | mail root -s "[Alert] CD-backup w/o tmp files (failed size: ${HUGESIZE})"
fi
                                                                                                                                             
mkisofs ${MKISOCMD} ${MKISOWHAT}
                                                                                                                                             cdrecord -eject  speed=4 dev=${CDR_DEV} backup.image.iso
                                                                                                                                             
}
                                                                                                                                             echo `date` " *** CD backup begin ***" >> /var/log/cd.backup.log 2>&1
                                                                                                                                             do_burn >> /var/log/cd.backup.log 2>&1
                                                                                                                                             echo `date` " *** CD backup is done ***" >> /var/log/cd.backup.log 2>&1


I use -m *tmp* trying to avoid huge archives of tmp files to be written. Another regexps and/or can be used.

I use space limit of 317000 of 2K blocks as I create new CD again. But with multiple sessions the available free space size has to be determined dynamycally.
_________________
"Lisp is a programmable programming language." - John Foderaro, CACM, September 1991
Back to top
View user's profile Send private message
panserg
Apprentice
Apprentice


Joined: 16 Apr 2003
Posts: 188

PostPosted: Wed Jan 14, 2004 11:12 am    Post subject: Reply with quote

Is it possible to delete previous sessions on CD-RW?
_________________
Less is more!
Back to top
View user's profile Send private message
Gandalf_Grey_
Apprentice
Apprentice


Joined: 19 Apr 2003
Posts: 151

PostPosted: Wed Jan 14, 2004 3:27 pm    Post subject: Reply with quote

panserg wrote:
Is it possible to delete previous sessions on CD-RW?


That was also one of the things that I was considering, although to do it you would have to first backup everything on the CD, blank it, then write back everything except the record you want to delete.
_________________
Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect.
-- Linus Torvalds
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum