[PLUG] cdrecord from cronjob

Robert McIntosh mcintoshrt at gmail.com
Sat May 17 06:28:31 UTC 2008


If it's not a path problem, could you bump up the error reporting level 
and post what's going on?

I used to run cdrecord religiously from cron usually without incident 
(when the scsi trickery didn't shuffle the dev entry).  Something like 
this worked well as encrypted back-ups with some error checking that 
sends a success/fail to the administrator:

===
#!/bin/sh

TODAY=`(set \`date\`;echo $1)`
ISOMAXSIZE=665600
mkdir /var/cdtemp > /dev/null 2>&1
CDTEMPDIR=/var/cdtemp
rm -f -R /var/cdtemp
mkdir /var/cdtemp
chown root::root /var/cdtemp
chmod 700 /var/cdtemp

#back-up databases
SOURCE=/var/lib/mysql
USERNAME=yourusername
PASSWORD=yourpassword
OUTPUT=/var/dbbackups
LOGFILE=/var/log/mysqllogback.txt
DATABASES=`mysql -B -u $USERNAME --password=$PASSWORD -e 'show 
databases' | grep -v Database`
 for DB in $DATABASES ; do
    EMPTYDB=`mysql -B -u $USERNAME --password=$PASSWORD -e 'show tables' 
$DB | wc -l`
    if [ $EMPTYDB -ge 1 ] ; then
        echo `date`": Starting backup of database $DB" 2>>$LOGFILE 
1>>$LOGFILE
        ( time /usr/bin/mysqlhotcopy $DB --allowold -u $USERNAME -p 
$PASSWORD $OUTPUT ) 2>>$LOGFILE 1>>$LOGFILE
        [ $? != 0 ] && echo "mysqlhotcopy of database $DB failed on 
"`hostname -f`"."
        echo `date`": Finished backup of database $DB" 2>>$LOGFILE 
1>>$LOGFILE
    else
       echo `date`": Database $DB empty - no backup is done" 2>>$LOGFILE 
1>>$LOGFILE
    fi
 done
 
 unset USERNAME
 unset PASSWORD
 unset OUTPUT
#First creat the archives
#echo "Creating /root archive"
tar cfz $CDTEMPDIR/root.tar.gz /root > /dev/null 2>&1
#echo "Creating /home archive"
tar cfz $CDTEMPDIR/robertcron.tar.gz /home/robert/cron > /dev/null 2>&1
#
tar cfz $CDTEMPDIR/etc.tar.gz /etc > /dev/null 2>&1
#echo "Creating cron back-ups"
tar cfz $CDTEMPDIR/cron.tar.gz /var/cron > /dev/null 2>&1
#echo "Creating web server back-ups"
tar cfz $CDTEMPDIR/www.tar.gz /var/www > /dev/null 2>&1
#echo "Creating mysql back-ups
tar cfz $CDTEMPDIR/mysql.tar.gz /var/dbbackups > /dev/null 2>&1

/bin/echo yourpasswordhere | /usr/bin/gpg --no-tty -c --yes 
--passphrase-fd 0 $CDTEMPDIR/root.tar.gz > /dev/null 2>&1
/bin/echo yourpasswordhere | /usr/bin/gpg --no-tty -c --yes 
--passphrase-fd 0 $CDTEMPDIR/etc.tar.gz > /dev/null 2>&1
/bin/echo yourpasswordhere | /usr/bin/gpg --no-tty -c --yes 
--passphrase-fd 0 $CDTEMPDIR/cron.tar.gz > /dev/null 2>&1
/bin/echo yourpasswordhere | /usr/bin/gpg --no-tty -c --yes 
--passphrase-fd 0 $CDTEMPDIR/www.tar.gz > /dev/null 2>&1
/bin/echo yourpasswordhere | /usr/bin/gpg --no-tty -c --yes 
--passphrase-fd 0 $CDTEMPDIR/mysql.tar.gz > /dev/null 2>&1
#Create the ISO Image of the archives
#/bin/echo "Creating iso image"
/usr/bin/mkisofs -o $CDTEMPDIR/backup.iso -l -J -R -A -V 
$CDTEMPDIR/*.tar.gz.gpg > /dev/null 2>&1

BACKUPISOSIZE=`du $CDTEMPDIR/backup.iso | cut -f 1`
BURNEDSIZE=`du /mnt/dvd | cut -f 1`
if [ "$BACKUPISOSIZE" -gt "$ISOMAXSIZE" ]
then
echo "ISO filesize too large to burn to CD-RW." > /root/cron/toobigiso
mail -s "Back-Up Image Too Large!" root < /root/cron/toobigiso
else
#Burn the ISO Image to CD-RW
#echo "Burning CD"

###!!!Find out what your --dev entry should be with cdrecord --dev-help
/usr/bin/cdrecord --dev=/dev/cdrom blank=fast > /var/log/burncd.log 2>&1
sleep 10
/usr/bin/cdrecord --dev=/dev/cdrom speed=16 $CDTEMPDIR/backup.iso >> 
/var/log/burncd.log 2>&1
mount /dev/hda /mnt/dvd >> /var/log/burncd.log 2>&1
umount -f /mnt/dvd

#Search the log for error messages
#grep -i warning /var/log/burncd.log >> /var/log/burncderror.log
grep -i error /var/log/burncd.log >> /var/log/burncderror.log
grep -i busy /var/log/burncd.log >> /var/log/burncderror.log
grep -i notice /var/log/burncd.log >> /var/log/burncderror.log
grep -i timeout /var/log/burncd.log >> /var/log/burncderror.log

#If we find errors, report them to the specified back-up administrator"
if test -s /var/log/burncderror.log
then
/bin/echo "$HOSTNAME generated errors during the back-up." 
 >>/var/log/bcderror
/bin/echo "The back-up may not have completed successfully." >> 
/var/log/bcderror
/bin/echo "Please review the following error messages:" >> /var/log/bcderror
/bin/echo "" >> /var/log/bcderror
/bin/echo "-----" >> /var/log/bcderror
cat /var/log/burncderror.log >> /var/log/bcderror
mail -s "$HOSTNAME Back-Up Error" root < /var/log/bcderror
rm -f /var/log/burncd.log
rm -f /var/log/bcderror.log
rm -f /var/log/bcderror
rm -f /var/log/burncderror.log
rm -f /var/ftp/pub/backup/*.*
rm -f /var/ftp/pub/backup/*.*.*
rm -f /var/ftp/pub/backup.iso

mount /mnt/dvd
if [$BURNEDSIZE = 0]
then
/bin/echo "$HOSTNAME generated errors during the bak-up." >> 
/var/log/bcderror
/bin/echo "The back-up may not have completed succesfully." >> 
/var/log/bcderror
/bin/echo "Please review the disc for errors."  >> /var/log/bcderror
/bin/echo "" >> /var/log/bcderror
/bin/echo "-----" >>/var/log/bcderror
mail -s "$HOSTNAME Back-Up Error" root < /var/log/bcderror
rm -f /var/log/burncd.log
rm -f /var/log/bcderror.log
rm -f /var/log/bcderror
rm -f /var/log/burncderror.log
rm -f $CDTEMPDIR/*
fi


#E-mail the administrator 'Success!'
/bin/echo "Back-up for $TODAY completed successfully." >> /root/jobdone
echo "" >> /root/jobdone
echo "The following directories were backed-up:" >> /root/jobdone
echo "/etc (system configuration files)" >> /root/jobdone
echo "/usr/local/etc (server application file configuration" >> 
/root/jobdone
echo "/root (System Adminstration files)" >> /root/jobdone
echo "/usr/local/www (Website)" >> /root/jobdone
echo "The size of the back-up volume is:" >> /root/jobdone
mount /dev/hdc /media/dvd > /dev/null 2>&1
du -h /media/dvd | cut -f 1 >> /root/jobdone
umount -f /media/dvd
echo "When the size is about 650 MB, you'll need to trim some files 
or">>/root/jobdone
echo "new backup solution." >> /root/jobdone  
mail -s "Backup Completed Succesfully" root < /root/jobdone
#rm /root/jobdone
rm -f -R /var/log/bcderror.log/
#rm -f /var/log/burncd.log
fi
fi
rm -f $CDTEMPDIR/*

Roderick A. Anderson wrote:
> I've run into an interesting situation and am looking for some insight.
>
> The scripts that copies some selected directories, creates and iso 
> image, blanks the CD-RW, then burns the image to the CD works fine from 
> the command line but when called aa a cronjob cdrecord seems to get it 
> wrong.  Some times the blanking doesn't work and sometimes it does and 
> then the iso burn works but when trying to mount the drive it tosses all 
> kinds of fits about not finding a valid file system.
>
> Since it works from the command line I'm assuming I've got all the 
> pieces write but it pukes from cron.
>
> Clue-stick anyone?
>
>
> TIA,
> Rod
>   



More information about the PLUG mailing list