From MythTV Official Wiki
Revision as of 20:44, 25 August 2014 by Dekarl (talk | contribs) (adjust style in an attempt to make mythwikiscripts happy)

Jump to: navigation, search

Important.png Note: The correct title of this article is It appears incorrectly here due to technical restrictions.

Author Phil Brady
Description Daily database backup at end of day
Supports Version25.png  

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.


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 as appropriate. It is called itself from the recording start system event and from within /usr/bin/

See Database Backup and Restore for full details of

The script can be placed in /usr/bin/



# Control database backups
# Do a backup after last recording of the day.
# Needs calls planted in start recording event and in

# logging if required (remove or write protect the logfile to stop logging)

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

echo "$(date +%F" "%T) $(whoami) called $0 $1 $2" >> $MYLOG

#database backup directory

#file to indicate recording has been made

#file showing time of last backup

#recordings backup directory

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
		touch $MYRECFILE
		chmod 666 $MYRECFILE
		echo "    $MYRECFILE created">>$MYLOG    

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

	TODAY=$(($(date +%s) - 14400))   #offset by 4 hours

	if [ -e $MYLASTBACKUP ]; then
		BACKUPDAY=$(($(date --reference=${MYLASTBACKUP} +%s)- 14400))

	if [ $BACKUPDAY -eq $TODAY ]; then
		echo "    backup already done today">>$MYLOG


	if [ $WAKEDAY -eq $TODAY ]; then
		echo "    Another recording expected today">>$MYLOG

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

	if [ $((WAKEDAY-RECDAY)) -ge 1 ];then    # 1 is interval in days - change if necessary

		#update marker file then do a db backup
		/usr/share/mythtv/ --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
			echo "    DATABASE BACKUP FAILED at $(date +%F" "%T)" >> $MYLOG

		if [ -x /usr/bin/ ]; then
			echo "    Starting backup of recordings">>$MYLOG
			/usr/bin/ >> $MYLOG 2>&1
			echo "    Recordings backup finished at $(date +%F" "%T)" >> $MYLOG
		echo "    Backup not due yet">>$MYLOG

	#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 /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.  Copy the link ~/.mythtv/config.xml to /home/mythtv/.mythtv/config.xml"
	echo " 6.  Plant /usr/bin/ --recording in recording start event"
	echo " 7.  plant /usr/bin/ --setwake \$1 in /usr/bin/"
	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 in /usr/bin and make it executable."
	echo "10. check parameters, here and in perl script."
	echo "    see --help" 
	echo ""
	echo ""
	echo "Calling parameters to this script are:"
	echo "  --setwake  <wakeuptime>"
	echo "  --recording"

#database restore reminder:  eg
#    /usr/share/mythtv/\
#	 --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/
#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/ --recording

See MythTV System Events

It is also triggered in the /usr/bin/ 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:

#$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/ ]; then
	/usr/bin/ --setwake $1


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/ 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 for details.