#!/usr/bin/perl -T use strict; use warnings; require 5; $|++; use CGI; use CGI::Carp qw(fatalsToBrowser); use Mail::Sendmail; use POSIX qw(strftime); # For safe ENV $ENV{PATH} = '/bin:/usr/bin'; my $to = 'ghenry@perl.me.uk, info@domain.org.uk'; my $from = '"Support Services" '; my $time = localtime; my $date = strftime '%d.%m.%y', localtime; my $hostname = 'server'; my $page = new CGI; my $speed = '32'; my $imagefile = "/server-$date.iso"; my $imagedir = '/opt/backups'; my $defaultdir = '/files'; my $contentsfile = 'contents.txt'; my $device = '/dev/hda'; my $logdir = '/opt/logs'; print $page->header(), $page->start_html( -title=>'Backup Progress....', -author=>'Gavin Henry', -style=>'/bottom.css', ); print '

CD/DVD Backup starting....

Please be patient, as this will take a long time.

';

# Move into the backup dir
chdir "$imagedir" 
	or die "Either that directory doesn't exist or I can't get into it: $!\n";

# Create a "table of contents" file.
!system "sudo ls -lRF $defaultdir > $defaultdir/$contentsfile" 
	or die "I don't have write permission on $defaultdir,so I can't create $contentsfile!\n\n Moving on....\n";

# The "l" option gives a "long" file listing.
# The "R" option makes the listing recursive.
# The "F" option marks the file types (directories get a trailing /).

print "Creating ISO9660 file system image ($imagefile).\n";
!system "sudo mkisofs -J -l -R -V srv-$date -o $imagefile $defaultdir"
	or die "I can't create the image.\n";

# Wipe the inserted CDRW
# echo "Blanking current CDRW"
# cdrecord blank=fast dev=$DEVICE

# Burn the CDRW.
if (-s "$imagefile" < 700_000_000 and -e "$imagefile") {
	print '';
      	print "\n\nISO not too big and exists. \nBurning the CDRW.\n\n"	
	     ."\nPlease be patient, this will take a while...\n\n";
      	!system "sudo cdrecord -v -eject dev=$device $imagefile" 		or die "There must be something wrong with the CD/DVD or there is no CD in the drive.\n \n\nBackup failed.";

	print '';

        # create success logfile
        open LOG, ">>$logdir/cdbackup-success-$date.log"
          or die "Cannot create logfile: $!";
        print LOG
        "CD/DVD backup successful on $time for $hostname.\n\n"
		close LOG;

	#send success e-mail
	my %mail_success = (
	    To      => "$to",
	    From    => "$from",
	    Subject => "CD/DVD backup complete on $hostname on $time",
	    Message => "CD/DVD backup has been a success on $hostname on $time\n\n"
                ."Backup Solution brought to you by Gavin Henry.\n\n"

	# Delete created image
    unlink $imagefile
	or die "I can't delete the CD/DVD image: $!\n";
	);
	sendmail(%mail_success);
		  
	print "\CD/DVD Backup complete on $time for $hostname.\<\/h3\>"
		  ."\E-mail sent with details.\<\/p\>"
          ."\Logfile also created in $logdir on $time.\<\/p\>"
          ."\<\/div\>";

	$page->end_html();
	exit 0

} else {
	# print messages to screen
	print "Oops! The ISO file is way too big. You need a DVD disc, not a CDRW: $!\n";	

	# create failed logfile
	open LOG, ">>$logdir/cdbackup-failure-$date.log"
	  or die "Cannot create logfile: $!";
	print LOG
        "CD/DVD backup has failed on $time for $hostname.\n\n"
         ."There must be something wrong with the CD/DVD media: $!\n"
	close LOG;

	#send failure e-mail
        my %mail_failure = (
            To      => "$to",
            From    => "$from",
            Subject => "CD/DVD backup failed on $hostname on $time",
            Message => "CD/DVD backup has failed on $hostname on $time\n\n"
        );
        sendmail(%mail_failure);

	# Failure message
	print "\CD/DVD Backup failed on $time for $hostname.\<\/h3\>"
      ."\E-mail sent with details.\<\/p\>"
      ."\Logfile also created in $logdir on $time.\<\/p\>"
      ."\<\/div\>";

	$page->end_html() ;
	exit 0
}