#!/bin/bash #process that uses this system backup script # - myth_mtc.py # - supplemental web, process.py backup linhes_backup_wrapper MYTH_RUN_STATUS="1" . /etc/profile . /etc/systemconfig BACKUPDIR=/data/storage/disk0/backup/system_backups SECBACKUPLINK=/data/storage/disk1 KeepBackups=13 DATE=`date +%F_%H-%M` backup_status=0 # MYTHSHUTDOWN="/usr/bin/mythshutdown" function lock_myth(){ $MYTHSHUTDOWN --lock } function unlock_myth(){ $MYTHSHUTDOWN --unlock } function backup_status_check(){ if [ $1 -ne 0 ] then backup_status=1 fi } function backup(){ echo "Starting Backup" mkdir -p $BACKUPDIR/$DATE #backup database if [ $SystemType = Master_backend -o $SystemType = Standalone ] then pacman -Q mysql 2>/dev/null if [ $? = 0 ] then echo " mythconverg (mythtv database)" mysqldump -x mythconverg > $BACKUPDIR/$DATE/mythconverg backup_status_check $? echo " ncid (caller id)" mysqldump -x ncid > $BACKUPDIR/$DATE/ncid 2>/dev/null #this is all the users echo " users" mysqldump -x mysql > $BACKUPDIR/$DATE/mysql_table #this is everything echo " All databases in one file" mysqldump -x --all-databases > $BACKUPDIR/$DATE/all_databases backup_status_check $? fi fi #backup saved settings if [ -e /usr/MythVantage/templates/settings ] then cp -rp /usr/MythVantage/templates/settings $BACKUPDIR/$DATE/settings backup_status_check $? fi #backup etc cp -rp /etc $BACKUPDIR/$DATE/etc if [ -e /var/lib/oss ] then cp -rp /var/lib/oss $BACKUPDIR/$DATE/oss backup_status_check $? fi #backup func keys cp -rp /etc/pki $BACKUPDIR/$DATE/pki if [ ! -f $MYTHHOME/backup_config/backup_exclude.txt ] then mkdir -p $MYTHHOME/backup_config/ echo ".mythtv/themecache" > $MYTHHOME/backup_config/backup_exclude.txt echo "tmp" >> $MYTHHOME/backup_config/backup_exclude.txt echo ".vnc/*log" >> $MYTHHOME/backup_config/backup_exclude.txt echo ".vnc/*pid" >> $MYTHHOME/backup_config/backup_exclude.txt chmod 777 $MYTHHOME/backup_config/backup_exclude.txt fi tar -zcf $BACKUPDIR/$DATE/home_dir.tar.gz -X $MYTHHOME/backup_config/backup_exclude.txt $MYTHHOME /home/vncsvc /home/xymon if [ -f $MYTHHOME/backup_config/backup_include.txt ] then tar -zcf $BACKUPDIR/$DATE/other.tar.gz -T $MYTHHOME/backup_config/backup_include.txt backup_status_check $? fi #make_zip file cd $BACKUPDIR tar -zcvf $BACKUPDIR/backup.$DATE.tgz $DATE backup_status_check $? if [ -d $BACKUPDIR/$DATE ] then rm -rf $BACKUPDIR/$DATE fi if [ -f /home/xymon/server/ext/hbnotes.py ] then /home/xymon/server/ext/hbnotes.py chown nobody:nobody /data/srv/httpd/htdocs/hobbit/notes/* 2> /dev/null >/dev/null fi echo echo "Created file:" echo " $BACKUPDIR/backup.$DATE.tgz" } function update_backup_status(){ echo # Add Last backup status to menu item #if description not in the backup xml file, add it if [ $rc=0 ] then COMPLETE_MSG="Last backup completed `date '+%D %-I:%M %p'`" else COMPLETE_MSG="Last backup FAILED `date '+%D %-I:%M %p'`" fi echo "Updating menu with:" echo " $COMPLETE_MSG" xmlfile="/usr/share/mythtv/themes/defaultmenu/mythbackup.xml" grep -q "" $xmlfile >/dev/null desc_check=$? if [ $desc_check = 0 ] then sed -i "0,/.*\/s||\$COMPLETE_MSG<\/description|" $xmlfile #sed -i "0,/\.*\/s//\$COMPLETE_MSG\<\/description/" $xmlfile else sed -i " /NONE/ i\ \$COMPLETE_MSG\<\/description\>" $xmlfile fi } function remove_old_backups(){ #remove old backups NumBackups=`ls $BACKUPDIR/backup*.tgz|wc -l` if [[ $NumBackups -gt $KeepBackups ]]; then numdel=$(($NumBackups-$KeepBackups)) rm -f `ls $BACKUPDIR/backup*.tgz -tr1|head -$numdel` fi } function remote_backup(){ echo echo "Remote backup" #Remote copy if [ x$RemoteBackup = x1 ] then localRemoteCheck=`echo $RemoteBackupDir | cut -d: -f1` if [ x$localRemoteCheck = xdir ] then localRemotedir=`echo $RemoteBackupDir | cut -d: -f2` echo " copying $BACKUPDIR/backup.$DATE.tgz to $localRemotedir " cp $BACKUPDIR/backup.$DATE.tgz $localRemotedir else /usr/bin/func ${RemoteBackupDir} ping| grep -q "FAILED" rc=$? if [ $rc = 0 ] then #this is here to mark failed copy of the backup. #There is a cron.hourly job that will attempt to retransfer the file echo " Remote backup failed to ${RemoteBackupDir}" echo backup.$DATE.tgz >> $BACKUPDIR/remote_backup_failed.txt else echo " copying $BACKUPDIR/backup.$DATE.tgz to ${RemoteBackupDir}:$BACKUPDIR/MBE_$DATE.tgz" /usr/bin/func ${RemoteBackupDir} copyfile -f $BACKUPDIR/backup.$DATE.tgz --remotepath $BACKUPDIR/MBE_$DATE.tgz fi fi else #do local copy to SECBACKUPLINK echo " Remote backup is not enabled, copying backup to $SECBACKUPLINK if it exists" SECBACKUPDISK=`readlink $SECBACKUPLINK` SECBACKUP=$SECBACKUPDISK/backup if [ -n "$SECBACKUPDISK" ]; then if [ `mountpoint -q -d $SECBACKUPDISK` ]; then if [ ! -d "$SECBACKUP" ]; then mkdir -p -m 775 $SECBACKUP echo " Created $SECBACKUP" chown mythtv:users $SECBACKUP fi echo " Copying system backups to $SECBACKUP" rsync -au --delete $BACKUPDIR $SECBACKUP else echo " $SECBACKUPDISK isn't mounted." fi else echo " Link $SECBACKUPLINK doesn't exist." fi fi } function remote_transfer(){ transfer_file=${1} echo $transfer_file /usr/bin/func ${RemoteBackupDir} ping| grep -q "FAILED" rc=$? if [ $rc = 0 ] then #this is here to mark a failed copy of the backup. #There is a cron.hourly job that will attempt to retransfer the file echo " Remote backup failed to ${RemoteBackupDir}" echo $transfer_file >> $BACKUPDIR/remote_backup_failed.txt else echo " copying $BACKUPDIR/$transfer_file to ${RemoteBackupDir}:$BACKUPDIR/MBE_$transfer_file" /usr/bin/func ${RemoteBackupDir} copyfile -f $BACKUPDIR/$transfer_file --remotepath $BACKUPDIR/MBE_$transfer_file fi } function add_link(){ if [ -f $BACKUPDIR/remote_backup_failed.txt ] then RETRYFILE="/etc/cron.hourly/lh_backup_retry.sh" echo "#!/bin/bash" > $RETRYFILE echo "#This file was autogenerated and will be removed by lh_system_backup_job" >> $RETRYFILE echo "MYTH_RUN_STATUS=1">> $RETRYFILE echo ". /etc/profile">> $RETRYFILE echo "lh_system_backup_job retry">> $RETRYFILE chmod 755 $RETRYFILE fi } function remove_link(){ RETRYFILE="/etc/cron.hourly/lh_backup_retry.sh" if [ ! -f $BACKUPDIR/remote_backup_failed.txt ] then rm -f $RETRYFILE fi } #------------------------------------ lock_myth if [ "x$1" = "x" ] then remove_old_backups backup update_backup_status remote_backup add_link else #this is where we attempt to transfer again as part of the cronjob if [ -f $BACKUPDIR/remote_backup_failed.txt ] then mv -f $BACKUPDIR/remote_backup_failed.txt /tmp while read line do echo $line remote_transfer $line done < /tmp/remote_backup_failed.txt rm -f /tmp/remote_backup_failed.txt fi remove_link fi unlock_myth echo $backup_status > /var/run/systembackup.rc exit $backup_status