# [BkBurnWithCdrtools.bsh] bash script to backup directories 2013-03-11
# Backup G:\bks\*.zip to DVD
# using Cdrtools mkisofs and cdrecord under Cygwin
# Be careful, some options have a lead -, others do not.
# This script will run unattended.
# The work is done is two stages:
# 1. build a 5 Gigabyte image of the DVD on C: with mkisofs
# 2. burn the image onto the DVD with cdrecord
# You will need to customise the temp work drive and the bks directory.

# temp file to store the ISO DVD image: H:\temp\temp.dvd
ISOIMAGE="/cygdrive/h/temp/temp.dvd"

# where the zips are that backuptozip created, G:\bks
LOCATION="/cygdrive/g/bks"

# the physical address of my DVD burner, determined by cdrecord -scanbus, was 0,0,0, new drive is SATA
DEV="0,0,0"

# optionally make a noise
# honk in a Windows utility that I wrote that works fine under Cygwin
honk SystemExclamation

# A blank DVD had better be in place.

# Build an image on disk in the current directory of what the DVD will look like.
# -iso-level 4 -> use most advanced version of the basic ISO-9660 format
# -J -> Joliet extensions for Microsoft
# -R -> RockRidge extensions for Unix/Linux
# -V "CMP BK" -> volume label for DVD (ideally put date in too)
# -root bks -> don't put files in root of DVD, but inside bks directory of DVD
# -o /cygdrive/d/temp/temp.dvd -> output image file D:\temp\temp.dvd
#     on D: where there is at least 5GB free working space
# /cygdrive/g/bks -> G:\bks in Cygwin, directory tree to burn.
# burnfree refers to hardware in some DVD drives to deal gracefully with underrun,
# the hard disk not providing data fast enough to keep up with the DVD.
# Files in bks will go to root of dvd.
# There is no way to specify the max size of the created image.
# Don't override the encodings. Let Windows do things its way or it won't be able to read the discs.
# Even one wrong innocuous-looking option will make your backup fail. Start with this:
mkisofs -J  -V "CMP BK"  -root bks  -o $ISOIMAGE  $LOCATION

ISOIMAGESIZE=$(stat -c%s $ISOIMAGE)
echo "DVD iso image size: $ISOIMAGESIZE (max allowed: 4,524,038,144)"

if [  $ISOIMAGESIZE -gt 4524038144 ]
  then
 read -p "ISO Image is too big to fit on a DVD.  max 4,524,038,144 will fit."
fi

# optionally let user know the image is built and the burn is starting.
honk "cygwin/c/Windows/Meda/Windows Hardware Remove.wav"

# Burn the iso image to DVD.
# dev=0,1,0 -> the physical address of my DVD burner, determined by cdrecord -scanbus
# driveropts=noburnfree -> disable underrun avoidance hardware. Not all DVD drives support this.
# speed=4 -> slow it down for more reliability. It defaults to 8 on my old hardware, 16 on my new. Limited by 16x disc blanks.
# gracetime=10 -> wait ten seconds before starting write to allow manual abort, or last minute blank disc insert
# -dao -> disk at once
# -data -> data as opposed to -audio
# -eject-> eject disk after burn
# There is no option to verify.
# Ideally we should run cdrecord at top priority, but I don't know how to do that from the command line.
# In Unix there is RR real time scheduling.
# There is no progress indication during the burn.
# In email, the author warned of possible "purple smoke" as a result of forcing the choice of driver.
# Let it decide for itself.
# UNFORTUNATELY, CDRECORD WILL NOT GIVE ANY PROGRESS MESSAGES AS IT WRITES THE DVD.
cdrecord  dev=$DEV  gracetime=10  -dao  -data  -eject  $ISOIMAGE

# delete the intermediate 5 Gb iso image
rm $ISOIMAGE

# optionally celebrate completion
honk SystemAsterisk

# quit bash, back to calling Windows bat script
exit

# -30-