Autobackup.sh
Note: The correct title of this article is autobackup.sh. It appears incorrectly here due to technical restrictions.
Author | Phil Brady |
Description | Daily database backup at end of day |
Supports | ![]() ![]() |
Readers may be interested in an approach to automating database backups of a mythtv system.
This mechanism is used with a dedicated mythtv frontend/backend system with Mythwelcome and ACPI Wakeup.
It has been validated with version 0.25 of Mythtv and Mythbuntu 12.04.
AIMS
It was thought that database backups should be taken:
- daily
- on days when recording had been made
- at a time when the system was otherwise idle
- preferably at the end of the day
- if a recording should straddle midnight, the backup should be after that had finished, so 'end of day' needs defining as 4am.
This is achieved by a bash script which calls mythconverg_backup.pl as appropriate. It is called itself from the recording start system event and from within /usr/bin/setwakeup.sh
See Database Backup and Restore for full details of mythconverg_backup.pl.
The script can be placed in /usr/bin/autobackup.sh
#!/bin/bash # Control database backups # Do a backup after last recording of the day. # Needs calls planted in start recording event and in setwakeup.sh # logging if required (remove or write protect the logfile to stop logging) MYLOGDIR=/var/log/mythtv MYLOGF=autobackup.log MYLOG=$MYLOGDIR/$MYLOGF if [ -w $MYLOG ]; then if [ $(find $MYLOGDIR -name $MYLOGF -size +20M) ]; then #tidy log if [ -w $MYLOG.1 ]; then cp $MYLOG $MYLOG.1 ; fi cp /dev/null $MYLOG fi else MYLOG=/dev/null fi echo "$(date +%F" "%T) $(whoami) called $0 $1 $2" >> $MYLOG #database backup directory [ -r $HOME/.mythtv/backuprc ] &&\ source $HOME/.mythtv/backuprc BACKUPDIR=${DBBackupDirectory:-/var/lib/mythtv/db_backups} #file to indicate recording has been made MYRECFILE="${BACKUPDIR}/RecordingDone" #file showing time of last backup MYLASTBACKUP="${BACKUPDIR}/BackupDone" #recordings backup directory #MYRECDIR=/var/lib/mythtv2/recordings if [ $# -eq 0 ]; then echo " need parameter">>$MYLOG echo "Needs parameter: try $0 --help" exit elif [ $1 == "--recording" ]; then #start of recording if [ -e $MYRECFILE ]; then echo " previous recording noted">>$MYLOG else touch $MYRECFILE chmod 666 $MYRECFILE echo " $MYRECFILE created">>$MYLOG fi elif [ $1 == "--setwake" ]; then #System about to close down - see if backup needed first echo " this wakeup time is $(date +%F" "%T --date=@${2})">>$MYLOG if [ ! -e $MYRECFILE ]; then echo " no recordings to backup">>$MYLOG exit fi TODAY=$(($(date +%s) - 14400)) #offset by 4 hours TODAY=$((TODAY/86400)) BACKUPDAY=0 if [ -e $MYLASTBACKUP ]; then BACKUPDAY=$(($(date --reference=${MYLASTBACKUP} +%s)- 14400)) BACKUPDAY=$((BACKUPDAY/86400)) fi if [ $BACKUPDAY -eq $TODAY ]; then echo " backup already done today">>$MYLOG exit fi WAKEDAY=$((${2}-14400)) WAKEDAY=$((WAKEDAY/86400)) if [ $WAKEDAY -eq $TODAY ]; then echo " Another recording expected today">>$MYLOG exit fi RECDAY=$(($(date --reference=${MYRECFILE} +%s)- 14400)) RECDAY=$((RECDAY/86400)) if [ $((WAKEDAY-RECDAY)) -ge 1 ];then # 1 is interval in days - change if necessary #update marker file then do a db backup touch ${MYLASTBACKUP} /usr/share/mythtv/mythconverg_backup.pl --directory ${BACKUPDIR} --rotate 10 >>$MYLOG 2>&1 #did it work? if [ $(find $BACKUPDIR -name 'mythconverg*.sql.gz' -size +5000k -newer $MYLASTBACKUP | wc -l) -gt 0 ]; then echo " db backup finished at $(date +%F" "%T)" >> $MYLOG else echo " DATABASE BACKUP FAILED at $(date +%F" "%T)" >> $MYLOG fi rm $MYRECFILE if [ -x /usr/bin/autobackup.pl ]; then echo " Starting backup of recordings">>$MYLOG /usr/bin/autobackup.pl >> $MYLOG 2>&1 echo " Recordings backup finished at $(date +%F" "%T)" >> $MYLOG fi else echo " Backup not due yet">>$MYLOG fi else #assume help needed echo "" echo "Do backups of database at end of day provided a recording has taken place" echo "" echo " 1. Make this script available and executable in /usr/bin" echo " 2. Create backup directory and permissions and set BACKUPDIR in" echo " this script. Default is ${BACKUPDIR}" echo " 3. Create log file with chmod 666 and set MYLOG." echo " default is /var/log/mythtv/autobackup.log" echo " no logging will take place if this file is not writable" echo " 4. Ditto /var/log/mythtv/autobackup.log.1" echo " 5. Copy the link ~/.mythtv/config.xml to /home/mythtv/.mythtv/config.xml" echo " 6. Plant /usr/bin/autobackup.sh --recording in recording start event" echo " 7. plant /usr/bin/autobackup.sh --setwake \$1 in /usr/bin/setwakeup.sh" echo ' ($1 is wakeuptime in secs since 1970)' echo "" echo "To also backup 'best' recordings:" echo " 8. Create backup partition and directory," echo " 9. place autobackup.pl in /usr/bin and make it executable." echo "10. check parameters, here and in perl script." echo " see autobackup.pl --help" echo "" echo "" echo "Calling parameters to this script are:" echo " --setwake <wakeuptime>" echo " --recording" exit fi #database restore reminder: eg # /usr/share/mythtv/mythconverg_restore.pl\ # --drop_database --create_database\ # --directory /var/lib/mythtv/db_backups\ # --filename mythconverg-1214-20080626150513.sql.gz
Execute permissions need to be set; two log files need creating and a directory creating for the backups:
#make script executable sudo chmod 755 /usr/bin/autobackup.sh #create log files sudo touch /var/log/mythtv/autobackup.log sudo touch /var/log/mythtv/autobackup.log.1 sudo chmod 666 /var/log/mythtv/autobackup.* #create backup directory sudo mkdir /var/lib/mythtv/db_backups sudo chmod 777 /var/lib/mythtv/db_backups
The user mythtv will be performing the backups. If the file (or link) /home/mythtv/.mythtv/config.xml does not exist then it will need creating so the backup system can access the database. However, if the file or link already exists then omit this step.
sudo cp ~/.mythtv/config.xml /home/mythtv/.mythtv/config.xml
The system needs to be triggered by the ‘Recording Start’ system event (frontend, setup, system events) which needs to be set to
/usr/bin/autobackup.sh --recording
It is also triggered in the /usr/bin/setwakeup.sh script which passes next wakeup time to the script and allows it to detect whether it is the last closedown of the 4am to 4am day and perform the database backup.
The standard setwakeup script is modified as follows:
#!/bin/sh #$1 is the first argument to the script. It is the time in seconds since 1970 #this is defined in mythtv-setup with the time_t argument echo 0 > /sys/class/rtc/rtc0/wakealarm #this clears your alarm. echo $1 > /sys/class/rtc/rtc0/wakealarm #this writes your alarm #do database backup if appropriate if [ -x /usr/bin/autobackup.sh ]; then /usr/bin/autobackup.sh --setwake $1 fi
Logging
The script placed logging information in /var/log/mythtv/autobackup.log and rotates it as necessary to /var/log/mythtv/autobackup.log.1 If logging is not required, simply remove these log files.
Backing up Recordings
If the file /usr/bin/autobackup.pl is present, then the script will also trigger a selective backup of recordings. The 'best set' which fits within the available space will be chosen and transferred. See autobackup.pl for details.