LinHES Forums
http://forums.linhes.org/

Auto mythbackup script
http://forums.linhes.org/viewtopic.php?f=5&t=15873
Page 1 of 1

Author:  Gnarl [ Fri Jul 13, 2007 9:57 pm ]
Post subject:  Auto mythbackup script

This is a little diddy I threw together that needs testing. It's an automatic mythbackup script that checks to see if the backend is busy before running, and if it is, it reschedules itself to run an hour later again and again until it succeeds, then resets itself to run again at the original time (every monday at 2AM)

WARNING!!! THIS IS BETA CODE!!

Prerequisites: Add the following line to your /etc/crontab
Code:
0 2 * * 1       root    /myth/data/backup/scripts/autobackup.sh


Things to Note:
The script fails to detect whether or not the upcoming recordings are actually going to be recorded.
If your system is busy all day and you run out of Monday it won't run because there are not 25 hours in the day. You will manually have to fix your cron entry
It is also very rudimentary, if you have other things scheduled at that time it will change those entries also.

Code:
#!/bin/sh
# initalize variables
db='mythconverg'
user='root'
password=''
SCRIPTDIR=/myth/data/backup/scripts

data0=`mysql $db -u $user -e "select count(*) from inuseprograms where recusage like 'player'" -sN`
data1=`mysql $db -u $user -e "select count(*) from inuseprograms where recusage like 'recorder'" -sN`
data2=`mysql $db -u $user -e "select count(*) from record
                                inner join recordmatch on record.recordid=recordmatch.recordid
                                where timediff(recordmatch.starttime,sysdate())<'00:10:00' and
                                recordmatch.starttime > sysdate() and
                                inactive=0 and (type!=0 or type!=8);" -sN`

CRONENTRY=$(grep autobackup /etc/crontab|gawk -F'[ ]' '{print $1,$2,"\\"$3,"\\"$4,$5}')
HOUR=$(grep autobackup /etc/crontab|gawk -F'[ ]' '{print $2}')
NEWCRON="0 $(expr $HOUR + 1) \* \* 1"

if [ $data0 -ge 1 -o $data1 -ge 1 -o $data2 -ge 1 ]; then
        cat /etc/crontab | sed "s/$CRONENTRY/$NEWCRON/g" > /etc/crontab
else
        cat /etc/crontab | sed "s/$CRONENTRY/0 2 \* \* 1/g" > /etc/crontab
        mythbackup
fi

exit 0


If you find any flaws or ways to augment it, please post. Comments are appreciated.

Author:  tjc [ Fri Jul 13, 2007 10:45 pm ]
Post subject: 

Rather than rescheduling it'd probably be simpler to just hang around and wait... It avoids the problem of stepping on other crontab entries or leaving a bunch of extra ones around... Make sure there's only one copy of the job running and loop until you get a break, sleeping for a half an hour at a pop takes up no CPU to speak of...

Author:  Gnarl [ Mon Jul 16, 2007 2:27 pm ]
Post subject: 

Another version for those who don't like modifying their crontab

Also needs testing

Code:
#!/bin/sh
# initalize variables
db='mythconverg'
user='root'
password=''
SCRIPTDIR=/myth/data/backup/scripts
BACKEDUP="false"

while [ $BACKEDUP = "false" ]
do
   data0=`mysql $db -u $user -e "select count(*) from inuseprograms where recusage like 'player'" -sN`
   data1=`mysql $db -u $user -e "select count(*) from inuseprograms where recusage like 'recorder'" -sN`
   data2=`mysql $db -u $user -e "select count(*) from record
                                inner join recordmatch on record.recordid=recordmatch.recordid
                                where timediff(recordmatch.starttime,sysdate())<'00:10:00' and
                                recordmatch.starttime > sysdate() and
                                inactive=0 and (type!=0 or type!=8);" -sN`

   if [ $data0 -ge 1 -o $data1 -ge 1 -o $data2 -ge 1 ]; then
        BACKEDUP="false"
        sleep 1800
   else
        BACKEDUP="true"
        mythbackup
   fi
done

exit 0

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/