From 6eddeb0330d50d2afaf3ae1458b0e61a0f626bbe Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Sat, 18 Feb 2023 00:56:05 -0500
Subject: linhes-system: move myth_mtc to lh_mtc and rework

---
 linhes/linhes-system/PKGBUILD             |  19 +-
 linhes/linhes-system/lh_mtc.cron          |  80 ++++++++
 linhes/linhes-system/lh_mtc.py            | 153 ++++++++++++++
 linhes/linhes-system/lh_system_backup     |  29 +++
 linhes/linhes-system/lh_system_backup_job | 330 ++++++++++++++++++++++++++++++
 linhes/linhes-system/lh_system_start.sh   |   7 +-
 linhes/linhes-system/myth_mtc.cron        |  79 -------
 linhes/linhes-system/myth_mtc.py          | 155 --------------
 8 files changed, 605 insertions(+), 247 deletions(-)
 create mode 100644 linhes/linhes-system/lh_mtc.cron
 create mode 100755 linhes/linhes-system/lh_mtc.py
 create mode 100755 linhes/linhes-system/lh_system_backup
 create mode 100755 linhes/linhes-system/lh_system_backup_job
 delete mode 100644 linhes/linhes-system/myth_mtc.cron
 delete mode 100755 linhes/linhes-system/myth_mtc.py

diff --git a/linhes/linhes-system/PKGBUILD b/linhes/linhes-system/PKGBUILD
index ab75da9..62bee0b 100755
--- a/linhes/linhes-system/PKGBUILD
+++ b/linhes/linhes-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=linhes-system
 pkgver=9.0.0
-pkgrel=46
+pkgrel=47
 arch=('x86_64')
 #install=$pkgname.install
 pkgdesc="Everything that makes LinHES a system"
@@ -11,14 +11,15 @@ depends=('cronie' 'dbus-python' 'dvb-firmware' 'expect' 'flatpak' 'firefox' 'gla
          'intel-media-driver' 'libva-intel-driver' 'libva-vdpau-driver')
 binfiles="add_storage.py balance_storage_groups.py empty_storage_groups.py remove_storage.py
           checkXFSfrag.sh enableIRWake.sh idle.py lh_system_start.sh lh_notify-send
-          lh_home_check.sh lh_myth_status.py jobqueue_helper.py gen_lib_xml.py
-          diskspace.sh find_orphans.py optimize_mythdb.py myth_mtc.py
+          lh_home_check.sh lh_myth_status.py lh_system_backup lh_system_backup_job
+          jobqueue_helper.py gen_lib_xml.py
+          diskspace.sh find_orphans.py optimize_mythdb.py lh_mtc.py
           misc_recent_recordings.pl misc_status_config.py misc_status_info.sh
           misc_upcoming_recordings.pl misc_which_recorder.pl
           create_media_dirs.sh be_check.py
           myth2mkv myth2mp3 myth2videos udev_link.sh"
 source=($binfiles
-    'myth_mtc.cron' 'paccache.cron' 'flatpak_update.cron' 'xfs_defrag.cron'
+    'lh_mtc.cron' 'paccache.cron' 'flatpak_update.cron' 'xfs_defrag.cron'
     'readme_is_xml' 'add_storage.readme' 'LinHES-release' 'lh_log_care.cron'
     '79-cronie.hook' 'fstrim.hook' 'openssh.hook' 'plex_lib.conf' 'rsyslog.hook'
     '10-monitor.conf' 'x11vnc.override.conf' 'lh_lighttpd.conf' 'lh_php.ini'
@@ -31,16 +32,18 @@ sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             'c7a2b2de44645e3a905b39d7411e5b8b27b60bd2944533fd4655c3b175755da1'
             'ae34515e144830f424d3bd3f6b1b446892d62beed20bca6f0fb19b0bbb779f27'
             '5e6d128f879b0fe7c1a190cccd75d4e5d00afc161f3bc9e92ffa2d87242cc9df'
-            '7667518308cbb2ff7fbc52e6e0ba918b11ee04e5e26cee85d98297df4929da41'
+            '7d4c5397326565b517799aef4e6301d12b672a5995e4de03cf6817f5de05855d'
             '6d4fb0ed1a5ed961b3a3884dce093118e50c2981a9cd5837d20abc5a6d4fd8aa'
             '87875d9e5f5ce18208f419698ce69b6bcbcd08955a57a4a13940e715af58b787'
             '93d664f4a46fda05d0f754d19df40cbda120e325b80c07092345b14763993833'
+            '8d6d09cedc85e49712144699ccb905be6ef663e9ab0bc01abd434370ab12fede'
+            'e5ffd470e43b4993b5212b128c2e92aa55d18538b130de671fcf56c0d0a5e9d4'
             '91bdec992bb2c933e15625c181f2195c402060b879168ebf35944cb064c904b9'
             '5cacfdd02833e5a3130d765573e772e6bd5030336ba86239c5e4db5ffa36fc69'
             'ebdb3ee0212e0cc72526bb5e50a032573e1894acb7bf75617243b0b49aa1f8f2'
             '4d006f0fe3b13e67de1b961d611e81911905a30d140849dfdb8e5c0dc4da2f7c'
             'e371c6a289c68fe200d7da856c20a8c579efa23178f4d62235f7359d7f6e49a1'
-            'c9b52a5be54f920ca5fb16423977cb685c9b2db8b39fe9b1c743abf12228a700'
+            '048a978774c4f05a5a9482f0aab32e00d2c21217d6faf8c578ed6b26cb5de67d'
             'd2d69b2bf6315bd37ff5f5b2f0cde8ab2fb89bae18f8796dc5208ffc1a9d743e'
             'a3f8ba840853e4a189dd52520a6958f4030e1cc3391200a6aeef055fb469f0b4'
             '1819085bd2c9106482c5f243b95fddf3dae69212330ab76cb493add5c26a45a4'
@@ -52,7 +55,7 @@ sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             'a961cfdc6f02b12fb445777dd2c144fed96306ca2f430cc8853ae307c759c1ad'
             'd8574104b75c6d41284488612ec5583c50a8dab438492fa42c47231add4cfc54'
             '6bdbf593d3e1348d1a8f7c4c17cb2e893f7e18ae355daf978173e669cfe3be80'
-            '9e97b4d68c9e8988daacadd40f1de9f0b5945d870eba596a2ceb5e0c023fa9c0'
+            'b3f02ec3f8cedc98c74b3169049b7b6aa78bd79d558a8bc98d00e064b983965c'
             '186203d3c0520bb3d611da99d33a7713e9c1563814285f1f101097234f214b2f'
             'd8d36a501928d0cc505957d392291fad317b1e895ff99847d90643cf5f622a89'
             'cdfc0c836b8194f631f4a9e022c232ff75a13ff1a161a1a011858578bea5f930'
@@ -126,7 +129,7 @@ package() {
     #cron files
     install -Dm755 $srcdir/paccache.cron $pkgdir/etc/cron.weekly/paccache
     install -Dm755 $srcdir/xfs_defrag.cron $pkgdir/etc/cron.weekly/xfs_defrag
-    install -Dm755 $srcdir/myth_mtc.cron $pkgdir/etc/cron.hourly/myth_mtc
+    install -Dm755 $srcdir/lh_mtc.cron $pkgdir/etc/cron.hourly/lh_mtc
     install -Dm755 $srcdir/flatpak_update.cron $pkgdir/etc/cron.daily/flatpak_update
     install -Dm755 $srcdir/lh_log_care.cron $pkgdir/etc/cron.daily/lh_log_care
 
diff --git a/linhes/linhes-system/lh_mtc.cron b/linhes/linhes-system/lh_mtc.cron
new file mode 100644
index 0000000..b556d60
--- /dev/null
+++ b/linhes/linhes-system/lh_mtc.cron
@@ -0,0 +1,80 @@
+#!/bin/bash
+MYTH_RUN_STATUS=1
+. /etc/systemconfig
+. /etc/profile
+
+date=`date +%Y-%m-%d`
+timestamp=`date +'%Y-%m-%d %H:%M'`
+hostname=`/usr/bin/hostnamectl hostname`
+logFile="/var/log/${date}/${hostname}_lh_mtc.log"
+log="logger -t lh_mtc -p local6.info"
+
+if [ ! -f $logFile ]; then
+    touch $logFile
+    echo "" | $log
+fi
+
+#check logfile for Finished and if not run lh_mtc.py
+if ! grep -q "Finished Maintenance" $logFile
+then
+
+    if ! grep -q "Finished checking size of MythTV home" $logFile
+    then
+        sudo -u mythtv bash -c "MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --check_home | $log"
+        if [ $? = 0 ]
+        then
+            echo "" | $log
+        else
+            echo "Time Exceeded" | $log
+            exit
+        fi
+    fi
+
+    if ! grep -q "Finished Optimize" $logFile
+    then
+        if [ $SystemType = FrontendOnly ]
+        then
+            echo "Will not run Optimize on Frontend Only systems." | $log
+            echo "Finished Optimize" | $log
+        else
+            sudo -u mythtv bash -c "MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --optimize | $log"
+            if [ $? = 0 ]
+            then
+                echo "" | $log
+            else
+                echo "Time Exceeded" | $log
+                exit
+            fi
+        fi
+    fi
+
+    if ! grep -q "Finished Backup" $logFile && grep -q "Finished Optimize" $logFile
+    then
+        sudo -u mythtv bash -c "MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --backup | $log"
+        if [ $? = 0 ]
+        then
+            echo "" | $log
+        else
+            echo "Time Exceeded" | $log
+            exit
+        fi
+    fi
+
+#    if ! grep -q "Finished Update" $logFile
+#    then
+#        sudo -u mythtv bash -c "MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --update | $log"
+#        if [ $? = 0 ]
+#        then
+#            echo "" | $log
+#        else
+#            echo "Time Exceeded" | $log
+#            exit
+#        fi
+#    fi
+
+#    if grep -q "Finished checking size of MythTV home" $logFile && grep -q "Finished Optimize" $logFile && grep -q "Finished Backup" $logFile && grep -q "Finished Update" $logFile
+    if grep -q "Finished checking size of MythTV home" $logFile && grep -q "Finished Optimize" $logFile && grep -q "Finished Backup" $logFile
+    then
+        echo "Finished Maintenance" | $log
+    fi
+fi
diff --git a/linhes/linhes-system/lh_mtc.py b/linhes/linhes-system/lh_mtc.py
new file mode 100755
index 0000000..f79314e
--- /dev/null
+++ b/linhes/linhes-system/lh_mtc.py
@@ -0,0 +1,153 @@
+#!/usr/bin/python
+import sys, subprocess
+import re
+import socket
+import os
+import datetime,time
+import shlex
+sys.dont_write_bytecode = True
+
+try:
+    from MythTV import MythBE
+    mythtv = MythBE()
+except:
+    mythtv = None
+
+#print mythtv.db.getSetting( 'Theme', socket.gethostname())
+
+def get_timestamp():
+    now = datetime.datetime.now()
+    date = (now.strftime('%Y-%m-%d %H:%M'))
+    return date
+
+def getFreePercentForDir(dir):
+    stats = os.statvfs(dir)
+    total = (stats.f_blocks)
+    avail = (stats.f_bavail)
+    return (total - avail) / float(total)
+
+def check_home():
+    #get the mythtv home dir
+    MYTHHOME=subprocess.check_output("lh_home_check.sh").decode('utf-8').strip()
+
+    freePcent = getFreePercentForDir(MYTHHOME)
+    print("  Home directory percent used: " + str(freePcent * 100) + "%")
+    if float(freePcent) > .9:
+        print("    Home directory is greater than 90% used. Clearing caches...")
+        cmd = "/usr/bin/rm -rf " + MYTHHOME + "/.mythtv/{*cache,Cache-*,tmp/*,MythMusic/AlbumArt/*}"
+        subprocess.call(["sh", "-c", cmd])
+        print("    Restarting mythfrontend...")
+        subprocess.call(["killall", "mythfrontend"])
+        cmd = "/usr/bin/rm -rf " + MYTHHOME + "/.cache/*"
+        subprocess.call(["sh", "-c", cmd])
+        cmd = "/usr/bin/rm -rf " + MYTHHOME + "/.plexht/userdata/Thumbnails/*"
+        subprocess.call(["sh", "-c", cmd])
+        cmd = "/usr/bin/rm -rf " + MYTHHOME + "/.plexht/userdata/ThemeMusicCache/*"
+        subprocess.call(["sh", "-c", cmd])
+        freePcent = getFreePercentForDir(MYTHHOME)
+        print("  Home directory percent used: " + str(freePcent * 100) + "%")
+    else:
+        print("    Home directory is less than 90% used. Not clearing caches.")
+    return 0
+
+def optimize():
+    try:
+        cursor = mythtv.db.cursor()
+        cursor.execute("SHOW tables")
+        result = cursor.fetchall()
+    except:
+        print("\n%s Problem getting tables from the database" %(get_timestamp()))
+        return 1
+    ops=["REPAIR","OPTIMIZE","ANALYZE"]
+    for row in result:
+        ctable=row[0]
+        for op in ops:
+            print("    %s %s" %(op,ctable))
+            cmd= "%s  table %s" %(op,ctable)
+            cursor.execute(cmd)
+    return 0
+
+def cleanup_inuseprograms():
+    fourHoursAgo=datetime.datetime.today() - datetime.timedelta(hours=4)
+    cmd="DELETE FROM inuseprograms WHERE lastupdatetime < '%s';" %fourHoursAgo
+    try:
+        cursor = mythtv.db.cursor()
+        cursor.execute(cmd)
+    except:
+        print("\n%s Problem cleaning inuseprograms in database" %(get_timestamp()))
+
+
+def bail_if_another_is_running():
+    cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
+    pids = subprocess.check_output(cmd).decode('utf-8').strip().split('\n')
+    if len(pids) > 1:
+        pids.remove("{}".format(os.getpid()))
+        print("Exiting! Found {} is already running (pids): {}".format(
+            __file__, " ".join(pids)))
+        raise SystemExit(1)
+
+def run_stuff():
+    print("\n%s" %get_timestamp())
+
+    if (len(sys.argv) == 1) or ("--noidlecheck" in sys.argv) and (len(sys.argv) == 2):
+        runall = True
+    else:
+        runall = False
+
+    if ("--noidlecheck" in sys.argv):
+        print("No system idle check will be done.")
+        idle = 0
+    else:
+        idle = subprocess.call(["/usr/bin/python", "/usr/bin/idle.py"])
+
+    if not idle:
+        if ("--check_home" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Checking size of MythTV home" %(get_timestamp()))
+            if not check_home():
+                print("\nFinished checking size of MythTV home")
+            else:
+                return True
+
+        if ("--optimize" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Running Optimize" %(get_timestamp()))
+            if not optimize():
+                print("\nFinished Optimize")
+            else:
+                return True
+
+        if ("--backup" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Running Backup" %(get_timestamp()))
+            if not os.system('/usr/bin/lh_system_backup_job'):
+                print("\nFinished Backup")
+
+        if ("--update" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Running System Update" %(get_timestamp()))
+            if not os.system('/usr/bin/lh_system_host_update'):
+                print("\nFinished Update")
+
+        print("\n#######################################")
+        continue_loop=False
+    else:
+        continue_loop=True
+    return continue_loop
+
+#---------------------------------
+bail_if_another_is_running()
+starttime=time.time()
+ctin=True
+while ctin:
+    cleanup_inuseprograms()
+    ctin=run_stuff()
+    if ctin:
+        print("\n%s Waiting 10 minutes before trying again." %(get_timestamp()))
+        time.sleep(600)
+
+    current_time=time.time()
+    if (current_time - starttime) > 3000 :
+        ctin = False
+        print("\n%s Time Exceeded 50 minutes. Quitting.)" %(get_timestamp()))
+        exit(1)
diff --git a/linhes/linhes-system/lh_system_backup b/linhes/linhes-system/lh_system_backup
new file mode 100755
index 0000000..f970f4f
--- /dev/null
+++ b/linhes/linhes-system/lh_system_backup
@@ -0,0 +1,29 @@
+#!/bin/bash
+. /etc/systemconfig
+lh_notify-send "Starting Backup..."
+#alert user the database will not be backed up
+if [ $SystemType != MasterBackend -a $SystemType != Standalone ]
+then
+    lh_notify-send "This is not the Master backend.\n Skipping backup of database."
+fi
+
+if [ $SystemType = MasterBackend -o $SystemType = Standalone ]
+then
+    backend_control.sh stop 127.0.0.1
+fi
+
+#do the backup
+lh_system_backup_job  2>&1 > /var/run/backup.log
+rc=$?
+if [ $SystemType = MasterBackend -o  $SystemType = Standalone ]
+then
+    backend_control.sh start 127.0.0.1
+fi
+
+if [ $rc = 0 ]
+then
+    complete_message="Backup completed successfully."
+else
+    complete_message="Backup failed."
+fi
+lh_notify-send "$complete_message"
diff --git a/linhes/linhes-system/lh_system_backup_job b/linhes/linhes-system/lh_system_backup_job
new file mode 100755
index 0000000..257bb87
--- /dev/null
+++ b/linhes/linhes-system/lh_system_backup_job
@@ -0,0 +1,330 @@
+#!/bin/bash
+#process that uses this system backup script
+# - lh_mtc.py
+
+MYTH_RUN_STATUS="1"
+. /etc/profile
+. /etc/systemconfig
+BACKUPDIR=/data/storage/disk0/backup/system_backups
+SECBACKUPLINK=/data/storage/disk1
+KeepBackups=14
+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
+    echo "Starting Backup"
+    mkdir -p $BACKUPDIR/$DATE
+
+    #backup database
+    if [ $SystemType = Master_backend -o  $SystemType = Standalone ]
+    then
+        echo
+        echo "Backup mysql databases"
+        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
+
+    echo "Backup saved settings"
+    if [ -e  /usr/MythVantage/templates/settings ]
+    then
+        cp -rp  /usr/MythVantage/templates/settings $BACKUPDIR/$DATE/settings
+        backup_status_check $?
+    fi
+
+    echo "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
+
+    echo "Backup func keys"
+    cp -rp /etc/pki $BACKUPDIR/$DATE/pki
+
+    echo "Backup Plex Media Server databases and preferences"
+    if [ -e  /data/storage/disk0/media/plex/Plex\ Media\ Server/Plug-in\ Support ]
+    then
+        mkdir $BACKUPDIR/$DATE/plex
+        backup_status_check $?
+    fi
+    if [ -e  /data/storage/disk0/media/plex/Plex\ Media\ Server/Plug-in\ Support/Databases ]
+    then
+        cp -rp /data/storage/disk0/media/plex/Plex\ Media\ Server/Plug-in\ Support/Databases $BACKUPDIR/$DATE/plex/Databases
+        backup_status_check $?
+    fi
+    if [ -e  /data/storage/disk0/media/plex/Plex\ Media\ Server/Plug-in\ Support/Preferences ]
+    then
+        cp -rp /data/storage/disk0/media/plex/Plex\ Media\ Server/Plug-in\ Support/Preferences $BACKUPDIR/$DATE/plex/Preferences
+        backup_status_check $?
+    fi
+
+    #create default backup_exclude.txt
+    if [ ! -f $MYTHHOME/backup_config/backup_exclude.txt ]
+    then
+        mkdir -p $MYTHHOME/backup_config/
+        touch $MYTHHOME/backup_config/backup_exclude.txt
+        chmod 777 $MYTHHOME/backup_config/backup_exclude.txt
+    fi
+
+    for i in ".mythtv/cache" ".mythtv/themecache" ".mythtv/remotecache" ".mythtv/Cache-myth*" ".cache" "tmp" ".vnc/*log" ".vnc/*pid" ".plexht/userdata/Thumbnails" ".plexht/userdata/ThemeMusicCache" ".kodi/userdata/Thumbnails"
+    do
+        grep -qF "$i" $MYTHHOME/backup_config/backup_exclude.txt
+        rc=$?
+        if [ $rc != 0 ]
+        then
+            echo "$i" >> $MYTHHOME/backup_config/backup_exclude.txt
+        fi
+    done
+
+    echo "Backup home dirs"
+    HOMEDIRS=""
+    tar -zcf $BACKUPDIR/$DATE/home_dir.tar.gz -X $MYTHHOME/backup_config/backup_exclude.txt  $MYTHHOME $HOMEDIRS
+
+    if [ -f $MYTHHOME/backup_config/backup_include.txt ]
+    then
+        echo
+        echo "Backup items from $MYTHHOME/backup_config/backup_include.txt"
+        tar -zcf $BACKUPDIR/$DATE/other.tar.gz -T $MYTHHOME/backup_config/backup_include.txt
+        backup_status_check $?
+    fi
+
+    echo "Compress backup file"
+    cd $BACKUPDIR
+    tar -zcf $BACKUPDIR/backup.$DATE.tgz  $DATE
+    backup_status_check $?
+    if [ -d $BACKUPDIR/$DATE ]
+    then
+        rm -rf $BACKUPDIR/$DATE
+    fi
+
+    if [ $backup_status -eq 0 ]
+    then
+        BACKUPPATH=$BACKUPDIR/backup.$DATE.tgz
+    else
+        echo "Backup had an error"
+        mkdir $BACKUPDIR/errored_backups
+        mv $BACKUPDIR/backup.$DATE.tgz $BACKUPDIR/errored_backups/backup.$DATE.tgz
+        BACKUPPATH=$BACKUPDIR/errored_backups/backup.$DATE.tgz
+    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 backup file:"
+    echo "    $BACKUPPATH"
+}
+
+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 "<description>" $xmlfile >/dev/null
+    desc_check=$?
+
+    if [ $desc_check = 0 ]
+    then
+        sed -i "0,/<description\>.*\<description\>/s||\<description\>$COMPLETE_MSG<\/description|" $xmlfile
+        #sed -i "0,/\<description\>.*\<description\>/s//\<description\>$COMPLETE_MSG\<\/description/" $xmlfile
+    else
+        sed -i " /NONE/ i\     \<description\>$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 another drive on this system."
+        SECBACKUPDISK=`readlink $SECBACKUPLINK`
+
+        if [ -z $SECBACKUPDISK ]
+        then
+            for dir in /data/storage/disk*
+            do
+                if [ $dir == /data/storage/disk0 ]
+                then
+                    continue
+                fi
+                TESTDIR=`readlink $dir`
+                if [ -n $TESTDIR ]
+                then
+                    SECBACKUPDISK=$TESTDIR
+                    break
+                fi
+            done
+        fi
+
+        if [ -n "$SECBACKUPDISK" ]; then
+            SECBACKUP=$SECBACKUPDISK/backup
+            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 "    Could not find another drive on this system."
+        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
+    backup
+    update_backup_status
+    #only remove old backups if there was no problems
+    if [ $backup_status -eq 0 ]
+    then
+        remove_old_backups
+    fi
+    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
diff --git a/linhes/linhes-system/lh_system_start.sh b/linhes/linhes-system/lh_system_start.sh
index 5e84bc6..eab8594 100755
--- a/linhes/linhes-system/lh_system_start.sh
+++ b/linhes/linhes-system/lh_system_start.sh
@@ -66,7 +66,7 @@ function install_lh_apps(){
     #install programs that are not needed on the iso
     #check network connection
     netwait=0
-    while ! nc -zw1 1.1.1.1 443; do
+    while [ ! nc -zw1 1.1.1.1 443 ]; do
         [ $netwait -gt 12 ] && msg "Could not install apps. Check internet connection. Cancelling Setup." && exit 1
         msg "Waiting for internet connectivity..."
         ((netwait++))
@@ -74,12 +74,8 @@ function install_lh_apps(){
     done
     msg "Installing apps."
     konsole -e /bin/bash -i -c "sudo pacman -Syyy --noconfirm archlinux-keyring && sudo pacman -Syyy --noconfirm mythtv mythplugins-mytharchive mythplugins-mythmusic mythplugins-mythweb && flatpak install tv.plex.PlexHTPC --noninteractive --assumeyes && sudo flatpak override tv.plex.PlexHTPC --filesystem=/run/lirc/lircd"
-    #sudo pacman -Syyy --noconfirm archlinux-keyring
-    #sudo pacman -Syyy --noconfirm flatpak firefox glances mlocate mythtv mythplugins-mytharchive mythplugins-mythmusic mythplugins-mythweb ncdu x11vnc
-    #sudo pacman -Syyy --noconfirm mythtv mythplugins-mytharchive mythplugins-mythmusic mythplugins-mythweb
     status=$?
     [ $status -eq 1 ] && msg "Could not install apps. Check internet connection. Cancelling Setup." && exit 1
-    #flatpak install tv.plex.PlexHTPC --noninteractive --assumeyes
     gen_lib_xml.py
 }
 
@@ -122,6 +118,7 @@ function first_configure(){
             # create media directory structure
             sudo mkdir -p /data/storage/disk0
             sudo create_media_dirs.sh /data/storage/disk0
+            sudo ln -s /data/storage/disk0/media/ /myth
             # setup DB
             sql_setup
             # run mythtv-setup
diff --git a/linhes/linhes-system/myth_mtc.cron b/linhes/linhes-system/myth_mtc.cron
deleted file mode 100644
index 6d70058..0000000
--- a/linhes/linhes-system/myth_mtc.cron
+++ /dev/null
@@ -1,79 +0,0 @@
-#!/bin/bash
-MYTH_RUN_STATUS=1
-. /etc/systemconfig
-. /etc/profile
-
-date=`date +%Y-%m-%d`
-timestamp=`date +'%Y-%m-%d %H:%M'`
-hostname=`hostname`
-logFile="/var/log/${date}/${hostname}_myth_mtc.log"
-log="logger -t myth_mtc -p local6.info"
-
-if [ ! -f $logFile ]; then
-    touch $logFile
-    echo "" | $log
-fi
-
-#check logfile for Finished and if not run myth_mtc.py
-if ! grep -q "Finished Maintenance" $logFile
-then
-
-    if ! grep -q "Finished checking size of MythTV home" $logFile
-    then
-        MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --check_home | $log
-        if [ $? = 0 ]
-        then
-            echo "" | $log
-        else
-            echo "Time Exceeded" | $log
-            exit
-        fi
-    fi
-
-    if ! grep -q "Finished Optimize" $logFile
-    then
-        if [ $SystemType = Frontend_only ]
-        then
-            echo "Will not run Optimize on Frontend Only systems." | $log
-            echo "Finished Optimize" | $log
-        else
-            MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --optimize | $log
-            if [ $? = 0 ]
-            then
-                echo "" | $log
-            else
-                echo "Time Exceeded" | $log
-                exit
-            fi
-        fi
-    fi
-
-    if ! grep -q "Finished Backup" $logFile && grep -q "Finished Optimize" $logFile
-    then
-        MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --backup | $log
-        if [ $? = 0 ]
-        then
-            echo "" | $log
-        else
-            echo "Time Exceeded" | $log
-            exit
-        fi
-    fi
-
-    if ! grep -q "Finished Update" $logFile
-    then
-        MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --update | $log
-        if [ $? = 0 ]
-        then
-            echo "" | $log
-        else
-            echo "Time Exceeded" | $log
-            exit
-        fi
-    fi
-
-    if grep -q "Finished checking size of MythTV home" $logFile && grep -q "Finished Optimize" $logFile && grep -q "Finished Backup" $logFile && grep -q "Finished Update" $logFile
-    then
-        echo "Finished Maintenance" | $log
-    fi
-fi
diff --git a/linhes/linhes-system/myth_mtc.py b/linhes/linhes-system/myth_mtc.py
deleted file mode 100755
index 1a71aa9..0000000
--- a/linhes/linhes-system/myth_mtc.py
+++ /dev/null
@@ -1,155 +0,0 @@
-#!/usr/bin/python
-import sys, subprocess
-import re
-import socket
-import os
-import datetime,time
-import shlex
-sys.dont_write_bytecode = True
-
-try:
-    from MythTV import MythBE
-    mythtv = MythBE()
-except:
-    mythtv = None
-
-#print mythtv.db.getSetting( 'Theme', socket.gethostname())
-
-def get_timestamp():
-    now = datetime.datetime.now()
-    date = (now.strftime('%Y-%m-%d %H:%M'))
-    return date
-
-def getFreePercentForDir(dir):
-    stats = os.statvfs(dir)
-    total = (stats.f_blocks)
-    avail = (stats.f_bavail)
-    return (total - avail) / float(total)
-
-def check_home():
-    #get the mythtv home dir
-    sys.path.append('/usr/MythVantage/bin/')
-    config_file = "mv_config"
-    data_config = __import__(config_file,  globals(),  locals(),  [])
-
-    freePcent = getFreePercentForDir(data_config.MYTHHOME)
-    print("  Home directory percent used: " + str(freePcent * 100) + "%")
-    if float(freePcent) > .9:
-        print("    Home directory is greater than 90% used. Clearing caches...")
-        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.mythtv/{*cache,Cache-*,tmp/*,MythMusic/AlbumArt/*}"
-        subprocess.call(["sh", "-c", cmd])
-        print("    Restarting mythfrontend...")
-        subprocess.call(["killall", "mythfrontend"])
-        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.cache/*"
-        subprocess.call(["sh", "-c", cmd])
-        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.plexht/userdata/Thumbnails/*"
-        subprocess.call(["sh", "-c", cmd])
-        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.plexht/userdata/ThemeMusicCache/*"
-        subprocess.call(["sh", "-c", cmd])
-        freePcent = getFreePercentForDir(data_config.MYTHHOME)
-        print("  Home directory percent used: " + str(freePcent * 100) + "%")
-    else:
-        print("    Home directory is less than 90% used. Not clearing caches.")
-    return 0
-
-def optimize():
-    try:
-        cursor = mythtv.db.cursor()
-        cursor.execute("SHOW tables")
-        result = cursor.fetchall()
-    except:
-        print("\n%s Problem getting tables from the database" %(get_timestamp()))
-        return 1
-    ops=["REPAIR","OPTIMIZE","ANALYZE"]
-    for row in result:
-        ctable=row[0]
-        for op in ops:
-            print("    %s %s" %(op,ctable))
-            cmd= "%s  table %s" %(op,ctable)
-            cursor.execute(cmd)
-    return 0
-
-def cleanup_inuseprograms():
-    fourHoursAgo=datetime.datetime.today() - datetime.timedelta(hours=4)
-    cmd="DELETE FROM inuseprograms WHERE lastupdatetime < '%s';" %fourHoursAgo
-    try:
-        cursor = mythtv.db.cursor()
-        cursor.execute(cmd)
-    except:
-        print("\n%s Problem cleaning inuseprograms in database" %(get_timestamp()))
-
-
-def bail_if_another_is_running():
-    cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
-    pids = subprocess.check_output(cmd).decode('utf-8').strip().split('\n')
-    if len(pids) > 1:
-        pids.remove("{}".format(os.getpid()))
-        print("Exiting! Found {} is already running (pids): {}".format(
-            __file__, " ".join(pids)))
-        raise SystemExit(1)
-
-def run_stuff():
-    print("\n%s" %get_timestamp())
-
-    if (len(sys.argv) == 1) or ("--noidlecheck" in sys.argv) and (len(sys.argv) == 2):
-        runall = True
-    else:
-        runall = False
-
-    if ("--noidlecheck" in sys.argv):
-        print("No system idle check will be done.")
-        idle = 0
-    else:
-        idle = subprocess.call(["/usr/bin/python", "/usr/bin/idle.py"])
-
-    if not idle:
-        if ("--check_home" in sys.argv) or runall:
-            print("\n#######################################")
-            print("\n%s Checking size of MythTV home" %(get_timestamp()))
-            if not check_home():
-                print("\nFinished checking size of MythTV home")
-            else:
-                return True
-
-        if ("--optimize" in sys.argv) or runall:
-            print("\n#######################################")
-            print("\n%s Running Optimize" %(get_timestamp()))
-            if not optimize():
-                print("\nFinished Optimize")
-            else:
-                return True
-
-        if ("--backup" in sys.argv) or runall:
-            print("\n#######################################")
-            print("\n%s Running Backup" %(get_timestamp()))
-            if not os.system('/usr/bin/lh_system_backup_job'):
-                print("\nFinished Backup")
-
-        if ("--update" in sys.argv) or runall:
-            print("\n#######################################")
-            print("\n%s Running System Update" %(get_timestamp()))
-            if not os.system('/usr/bin/lh_system_host_update'):
-                print("\nFinished Update")
-
-        print("\n#######################################")
-        continue_loop=False
-    else:
-        continue_loop=True
-    return continue_loop
-
-#---------------------------------
-bail_if_another_is_running()
-starttime=time.time()
-ctin=True
-while ctin:
-    cleanup_inuseprograms()
-    ctin=run_stuff()
-    if ctin:
-        print("\n%s Waiting 10 minutes before trying again." %(get_timestamp()))
-        time.sleep(600)
-
-    current_time=time.time()
-    if (current_time - starttime) > 3000 :
-        ctin = False
-        print("\n%s Time Exceeded 50 minutes. Quitting.)" %(get_timestamp()))
-        exit(1)
-- 
cgit v0.12

