#!/usr/bin/perl -T use strict; # avoid stupid bugs use warnings; # Let us know what is bad require 5; # for following modules $|++; # send stuff to output asap. use CGI; # Use CGI stuff for web output use CGI::Carp qw(fatalsToBrowser); # For better error messages use Mail::Sendmail; # For sending the e-mails use POSIX qw(strftime); # For the date format # For safe ENV $ENV{PATH} = '/bin:/usr/bin'; my $to = 'me@me.com'; my $from = 'me@me.com'; my $time = localtime; my $date = strftime '%d.%m.%y', localtime; my $hostname = 'servername'; my $page = new CGI; my $speed = '32'; my $imagefile = "servername-$date.iso"; my $imagedir = '/opt/backups'; my $defaultdir = '/files'; my $contentsfile = 'contents.txt'; my $device = '/dev/hda'; my $logdir = '/var/logs'; print $page->header(), $page->start_html( -title=>'Backup Progress....', -author=>'me.', -style=>'/style.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 warn "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 srvnme-$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";                 
}

( $? == 0 ) ? &success($date, $time, $hostname, $logdir, $to, $from) :
&failure($date, $time, $hostname, $logdir, $to, $from);

##########################
sub success {
##########################
        my ($date, $time, $hostname, $logdir, $to, $from) = @_;
        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";               
        );
        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();
}

##########################
sub failure {
##########################
        my ($date, $time, $hostname, $logdir, $to, $from) = @_;
        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();
}