Autobackup.sh

From MythTV Official Wiki
Revision as of 22:11, 27 November 2012 by PhilB (Talk | contribs)

Jump to: navigation, search

Important.png 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 Version25.png  


THIS PAGE IS UNDER CONSTRUCTION. PLEASE IGNORE IT FOR NOW!

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 is placed in /usr/bin/autobackup.sh and which calls mythconverg_backup.pl as appropriate. It is called itself from the recording start system event and from within setwakeup.sh

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 +100k) ]; 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
BACKUPDIR=/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"

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

	RECDAY=$(($(date --reference=${MYRECFILE} +%s)- 14400))
	RECDAY=$((RECDAY/86400))

	WAKEDAY=$((${2}-14400))
	WAKEDAY=$((WAKEDAY/86400))

	if [ $((WAKEDAY-RECDAY)) -ge 1 ];then    # 1 is interval in days - change if necessary
		#do a backup
		/usr/share/mythtv/mythconverg_backup.pl --directory ${BACKUPDIR} --rotate 10
		rm $MYRECFILE
		touch ${MYLASTBACKUP}
		echo "    Database backup completed" >>$MYLOG
		if [ -x /usr/bin/autobackup.pl ]; then
			echo "    Starting backup of recordings">>$MYLOG
			/usr/bin/autobackup.pl 1>> $MYLOG 2>&1
		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 this script"
	echo "         default is /var/lib/mythtv/db_backups"
	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.  Plant /usr/bin/autobackup.sh --recording in recording start event"
	echo " 6.  plant /usr/bin/autobackup.sh --setwake \$1 in /usr/bin/setwakeup.sh"
	echo '      ($1 is wakeuptime in secs since 1970)'
	echo " 7.  When log file no longer needed, simply delete it." 
	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


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

It 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

and 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 looging id not required, simply remove these log files.

Recording backups

If the file /usr/bin/autobackup.pl is present, then the script will also trigger a selective backup of recordings. See autobackup.pl for details.