Difference between revisions of "Autobackup.sh"
m (tabs problem) |
(Fix because it stopped working. See discussion.) |
||
Line 31: | Line 31: | ||
<pre> | <pre> | ||
+ | |||
#!/bin/bash | #!/bin/bash | ||
Line 43: | Line 44: | ||
if [ -w $MYLOG ]; then | if [ -w $MYLOG ]; then | ||
− | if [ $(find $MYLOGDIR -name $MYLOGF -size + | + | if [ $(find $MYLOGDIR -name $MYLOGF -size +20M) ]; then |
#tidy log | #tidy log | ||
if [ -w $MYLOG.1 ]; then cp $MYLOG $MYLOG.1 ; fi | if [ -w $MYLOG.1 ]; then cp $MYLOG $MYLOG.1 ; fi | ||
Line 64: | Line 65: | ||
#recordings backup directory | #recordings backup directory | ||
− | MYRECDIR=/var/lib/mythtv2/recordings | + | #MYRECDIR=/var/lib/mythtv2/recordings |
if [ $# -eq 0 ]; then | if [ $# -eq 0 ]; then | ||
Line 82: | Line 83: | ||
elif [ $1 == "--setwake" ]; then | elif [ $1 == "--setwake" ]; then | ||
+ | |||
#System about to close down - see if backup needed first | #System about to close down - see if backup needed first | ||
echo " this wakeup time is $(date +%F" "%T --date=@${2})">>$MYLOG | echo " this wakeup time is $(date +%F" "%T --date=@${2})">>$MYLOG | ||
Line 116: | Line 118: | ||
if [ $((WAKEDAY-RECDAY)) -ge 1 ];then # 1 is interval in days - change if necessary | 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 | + | #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 | rm $MYRECFILE | ||
− | |||
− | |||
if [ -x /usr/bin/autobackup.pl ]; then | if [ -x /usr/bin/autobackup.pl ]; then | ||
echo " Starting backup of recordings">>$MYLOG | echo " Starting backup of recordings">>$MYLOG | ||
− | /usr/bin/autobackup.pl | + | /usr/bin/autobackup.pl >> $MYLOG 2>&1 |
− | + | echo " Recordings backup finished at $(date +%F" "%T)" >> $MYLOG | |
fi | fi | ||
else | else | ||
Line 137: | Line 146: | ||
echo "" | echo "" | ||
echo " 1. Make this script available and executable in /usr/bin" | echo " 1. Make this script available and executable in /usr/bin" | ||
− | echo " 2. Create backup directory and permissions and set BACKUPDIR in | + | echo " 2. Create backup directory and permissions and set BACKUPDIR in" |
− | echo " | + | echo " this script. Default is /var/lib/mythtv/db_backups" |
echo " 3. Create log file with chmod 666 and set MYLOG." | echo " 3. Create log file with chmod 666 and set MYLOG." | ||
echo " default is /var/log/mythtv/autobackup.log" | echo " default is /var/log/mythtv/autobackup.log" | ||
echo " no logging will take place if this file is not writable" | echo " no logging will take place if this file is not writable" | ||
echo " 4. Ditto /var/log/mythtv/autobackup.log.1" | echo " 4. Ditto /var/log/mythtv/autobackup.log.1" | ||
− | echo " 5. Plant /usr/bin/autobackup.sh --recording in recording start event" | + | echo " 5. Copy the link ~/.mythtv/config.xml to /home/mythtv/.mythtv/config.xml" |
− | echo " | + | 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 ' ($1 is wakeuptime in secs since 1970)' | ||
− | |||
echo "" | echo "" | ||
echo "To also backup 'best' recordings:" | echo "To also backup 'best' recordings:" | ||
Line 163: | Line 172: | ||
#database restore reminder: eg | #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 | + | # /usr/share/mythtv/mythconverg_restore.pl\ |
+ | # --drop_database --create_database\ | ||
+ | # --directory /var/lib/mythtv/db_backups\ | ||
+ | # --filename mythconverg-1214-20080626150513.sql.gz | ||
</pre> | </pre> | ||
Line 184: | Line 196: | ||
</pre> | </pre> | ||
− | + | The user ''mythtv'' will be performing the backups so a link needs creating in ''/home/mythtv/.mythtv'' so the backup system can access the database. | |
+ | |||
+ | <pre> | ||
+ | sudo cp ~/.mythtv/config.xml /home/mythtv/.mythtv/config.xml | ||
+ | </pre> | ||
+ | |||
+ | The system needs to be triggered by the ‘Recording Start’ system event (frontend, setup, system events) which needs to be set to | ||
<pre> | <pre> |
Revision as of 17:23, 17 January 2013
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 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" 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 /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/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 so a link needs creating in /home/mythtv/.mythtv so the backup system can access the database.
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.
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.