From bf9991d8258086515c1350d8719622c659924f37 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Thu, 31 Jan 2013 20:01:24 -0600
Subject: LinHES-system:   add lh_system_restore and lh_system_backup.  These
 scripts are called from the mythmenu. refs #900

---
 abs/core/LinHES-system/PKGBUILD              |  24 ++---
 abs/core/LinHES-system/lh_system_backup      |  30 ++++++
 abs/core/LinHES-system/lh_system_backup_job  | 104 ++++++++++++++++++---
 abs/core/LinHES-system/lh_system_restore     |  32 +++++++
 abs/core/LinHES-system/lh_system_restore_job | 134 +++++++++++++++++++--------
 5 files changed, 261 insertions(+), 63 deletions(-)
 create mode 100755 abs/core/LinHES-system/lh_system_backup
 create mode 100755 abs/core/LinHES-system/lh_system_restore

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 273a17d..2365063 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=2
-pkgrel=141
+pkgrel=145
 arch=('i686' 'x86_64')
 MVDIR=$startdir/pkg/usr/LH
 BINDIR=$startdir/pkg/usr/bin
@@ -15,6 +15,7 @@ binfiles="LinHES-start optimize_mythdb.py
  mythfrontend-start set_windowmanager.sh
  myth_status.py myth_status.sh
  install_supplemental_service.sh
+ lh_system_backup
  lh_system_backup_job
  lh_system_restore_job
  lh_system_host_update
@@ -52,13 +53,13 @@ build() {
         	echo "installing $item to $MVDIR"
         done
 
-        cd $startdir/bin
-        for i in *
-        do
-        	item=$i
-        	install -m755 -D $item $MVDIR/bin/$item
-        	echo "bin stuff: $item"
-        done
+#         cd $startdir/bin
+#         for i in *
+#         do
+#         	item=$i
+#         	install -m755 -D $item $MVDIR/bin/$item
+#         	echo "bin stuff: $item"
+#         done
 
         install -m644 -D $startdir/src/alsa-base $startdir/pkg/etc/modprobe.d/alsa-base.conf
         install -m644 -D $startdir/src/readme_is_xml $startdir/pkg/etc/gen_is_xml.d/readme_is_xml
@@ -95,8 +96,9 @@ md5sums=('d1f60851cb319921f3b7ad044d6f6332'
          '692563448cca1d49f45e7d1c8abcaa0c'
          '962a3e9eaba2d1466251b7ab0956705d'
          '1758aed160de64abfafb28a3a8f3390e'
-         'a19d010af99a3b0a9d826e6fd6a5b45f'
-         '6b2640772d89195cd6fa3db134142951'
+         '3edef50a49a47694bf8add39cc160add'
+         '5e17e0786afd891550a660f646076308'
+         'bc69a520add58ede9b060c73e67ace13'
          '1be1d3dfc83f828eede93055713882ae'
          '47e093e8cfe4b5b96602358e1f540832'
          'a8e486f5d23913242b6edb862bf44c0e'
@@ -106,7 +108,7 @@ md5sums=('d1f60851cb319921f3b7ad044d6f6332'
          '4a3cd8f9b33b2b86fdba47a8f1fa2859'
          '16f079dab35dde6efd55bf6cf4e2fb2f'
          '0e76cd8f320a852a162931450a1fb8b7'
-         'a53de3c723a071eb2800a4f966f2d4dd'
+         '645297f5905e87eb99999d5a0f3f069a'
          'ea315f41dcd6c978e546c95fc05546cf'
          'ac61cc460d9e97ba1f5ef69e92cdfbe5'
          'f3502bb7c665750da0ecdf6918f7c838'
diff --git a/abs/core/LinHES-system/lh_system_backup b/abs/core/LinHES-system/lh_system_backup
new file mode 100755
index 0000000..61a0ec3
--- /dev/null
+++ b/abs/core/LinHES-system/lh_system_backup
@@ -0,0 +1,30 @@
+#!/bin/bash
+. /etc/systemconfig
+msg_client.py --msg "Starting Backup"
+#alert user the database will not be backed up
+if [ $SystemType != Master_backend -a  $SystemType != Standalone ]
+then
+    msg_client.py --msg "This is not the Master backend.\n Skipping backup of database."
+fi
+
+if [ $SystemType = Master_backend -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 = Master_backend -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
+msg_client.py --msg "$complete_message"
+
diff --git a/abs/core/LinHES-system/lh_system_backup_job b/abs/core/LinHES-system/lh_system_backup_job
index 3a13ade..e095a97 100644
--- a/abs/core/LinHES-system/lh_system_backup_job
+++ b/abs/core/LinHES-system/lh_system_backup_job
@@ -1,25 +1,69 @@
 #!/bin/bash
 #process that uses this system backup script
 # - myth_mtc.py
-# - supplemental web, process.py  backup
+# - supplemental web, process.py  backup  linhes_backup_wrapper
+
 MYTH_RUN_STATUS="1"
 . /etc/profile
 . /etc/systemconfig
 BACKUPDIR=/data/storage/disk0/backup/system_backups
 DELETE_DAYS=21
 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 db
-    pacman -Q mysql 2>/dev/null
-    if [ $? = 0 ]
+    #backup database
+    if [ $SystemType = Master_backend -o  $SystemType = Standalone ]
+    then
+        pacman -Q mysql 2>/dev/null
+        if [ $? = 0 ]
+        then
+            mysqldump -x mythconverg > $BACKUPDIR/$DATE/mythconverg
+            backup_status_check $?
+
+            mysqldump -x ncid > $BACKUPDIR/$DATE/ncid
+
+            #this is all the users
+            mysqldump -x mysql > $BACKUPDIR/$DATE/mysql_table
+
+            #this is everything
+            mysqldump -x --all-databases > $BACKUPDIR/$DATE/all_databases
+            backup_status_check $?
+
+
+
+        fi
+    fi
+
+    #backup saved settings
+    if [ -e  /usr/MythVantage/templates/settings ]
     then
-        mysqldump mythconverg > $BACKUPDIR/$DATE/mythconverg
-        mysqldump ncid > $BACKUPDIR/$DATE/ncid
+        cp -rp  /usr/MythVantage/templates/settings $BACKUPDIR/$DATE/settings
+        backup_status_check $?
     fi
 
     #backup etc
@@ -27,6 +71,7 @@ function backup(){
     if [ -e /var/lib/oss ]
     then
         cp -rp /var/lib/oss $BACKUPDIR/$DATE/oss
+        backup_status_check $?
     fi
 
     #backup func keys
@@ -47,16 +92,17 @@ function backup(){
     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
-
-    if [ -d $BACKUPDIR/$DATE ]
-    then
-        rm -rf $BACKUPDIR/$DATE
-    fi
+    backup_status_check $?
+     if [ -d $BACKUPDIR/$DATE ]
+     then
+         rm -rf $BACKUPDIR/$DATE
+     fi
 
     if [ -f /home/xymon/server/ext/hbnotes.py ]
     then
@@ -69,6 +115,29 @@ function backup(){
     echo "########################################"
 }
 
+function update_backup_status(){
+    # 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 on `date '+%D @ %-I:%M %p'`"
+    else
+        COMPLETE_MSG="Last backup FAILED on `date '+%D @ %-I:%M %p'`"
+    fi
+
+    xmlfile="/usr/share/mythtv/themes/defaultmenu/mythbackup.xml"
+
+    grep  -q "<description>" $xmlfile >/dev/null
+    desc_check=$?
+
+    if [ $desc_check = 0 ]
+    then
+        sed -i "s_\<description\>.*\<description\>_description\>$COMPLETE_MSG\<\/description_" $xmlfile
+    else
+        sed -i " /NONE/ i\     \<description\>$COMPLETE_MSG\<\/description\>" $xmlfile
+    fi
+}
+
 function remove_old_backups(){
     #remove old backups
     find $BACKUPDIR/backup*.tgz -type f -mtime +$DELETE_DAYS -delete
@@ -109,7 +178,7 @@ function remote_transfer(){
     rc=$?
     if [ $rc = 0 ]
     then
-        #this is here to mark failed copy of the backup.
+        #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
@@ -124,7 +193,7 @@ function add_link(){
     then
         RETRYFILE="/etc/cron.hourly/lh_backup_retry.sh"
         echo "#!/bin/bash" > $RETRYFILE
-        echo "#This file was autogenerated and removed by lh_system_backup_job" >> $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
@@ -139,11 +208,15 @@ function remove_link(){
         rm -f $RETRYFILE
     fi
 }
+
+
 #------------------------------------
+lock_myth
 if [ "x$1" = "x" ]
 then
     remove_old_backups
     backup
+    update_backup_status
     remote_backup
     add_link
 else
@@ -160,6 +233,7 @@ else
     fi
     remove_link
 fi
-
-
+unlock_myth
+echo $backup_status > /var/run/systembackup.rc
+exit $backup_status
 
diff --git a/abs/core/LinHES-system/lh_system_restore b/abs/core/LinHES-system/lh_system_restore
new file mode 100755
index 0000000..99082ff
--- /dev/null
+++ b/abs/core/LinHES-system/lh_system_restore
@@ -0,0 +1,32 @@
+#!/bin/bash
+. /etc/systemconfig
+BACKUPDIR=/data/storage/disk0/backup/system_backups
+
+
+#find last backup
+cd $BACKUPDIR
+restore_file=`ls -t backup.*|head -1`
+cd -
+
+if [ $SystemType != Master_backend -a  $SystemType != Standalone ]
+then
+    msg_client.py --msg "This is not the Master backend.\n Skipping restore of database."
+fi
+
+#do the backup
+msg_client.py --msg "Restoring from:\n$restore_file"
+lh_system_restore_job $restore_file 2>&1 > /var/run/restore.log
+rc=$?
+
+if [ $rc = 0 ]
+then
+    complete_message="Restore completed successfully"
+else
+    complete_message="Restore failed!"
+fi
+
+msg_client.py --msg "$complete_message"
+
+
+
+#don't try to restore DB if not master
diff --git a/abs/core/LinHES-system/lh_system_restore_job b/abs/core/LinHES-system/lh_system_restore_job
index eb3bd51..da7dbca 100644
--- a/abs/core/LinHES-system/lh_system_restore_job
+++ b/abs/core/LinHES-system/lh_system_restore_job
@@ -2,10 +2,16 @@
 #  This script is used to perform a quick restore of a database that was backed up via the nightly system backup
 #  The restore only does the database it does NOT restore system files (even if they are in the backup file)
 #
-# - supplemental web, process.py 
+# - supplemental web, process.py  , lh_system_restore
 
+. /etc/systemconfig
 BACKUPDIR=/data/storage/disk0/backup/system_backups
+RESTOREDIR=$BACKUPDIR/restore
 backupfile=$1
+restore_status=0
+
+MYTH_RUN_STATUS="1"
+. /etc/profile
 
 function usage(){
     echo "------------------------------------------------------"
@@ -26,7 +32,15 @@ function usage(){
     echo "------------------------------------------------------"
 
     exit 1
-    }
+}
+
+function restore_status_check(){
+    if [ $1 -ne 0 ]
+    then
+        restore_status=1
+    fi
+}
+
 
 function run_cleanup(){
     echo "Cleaning up the restore dir"
@@ -34,18 +48,18 @@ function run_cleanup(){
     rm -f $RESTOREDIR/$backupfile
 }
 
-function restore_db(){
-
-    DIR=`echo $backupfile |cut -d. -f2`
-    CSQL="create database mythconverg;"
-    DSQL="drop database mythconverg; "
-    MYSQL="mysql  -u mythtv -pmythtv"
-
-
-
+function expand_restore_file(){
     mkdir -p $RESTOREDIR
+    DIR=`echo $backupfile |cut -d. -f2`
     cp $BACKUPDIR/$backupfile $RESTOREDIR
     cd $RESTOREDIR && tar -xf  $backupfile && cd $DIR
+    restore_status_check $?
+}
+
+function restore_db(){
+    CSQL="create database mythconverg;"
+    DSQL="drop database mythconverg; "
+    MYSQL="mysql -u mythtv -pmythtv"
 
     if [ -f mythconverg ]
     then
@@ -56,18 +70,52 @@ function restore_db(){
         #restore the database_backup
         echo "Restoring database  $DIR"
         $MYSQL  mythconverg < mythconverg
-        if [ $? = 0 ]
+        rc=$?
+        restore_status_check $rc
+
+        if [ $rc = 0 ]
         then
-            echo "Done"
+            echo "Restore of mythconverg complete"
         else
-            echo "An error occured"
+            echo "An error occurred restoring mythconverg"
         fi
+
      else
-        echo "Couldn't file a file to restore"
+        echo "Couldn't find file mythconverg  to restore"
+        restore_status=1
     fi
 
-    }
+}
 
+function restore_ncid(){
+    CSQL="create database ncid;"
+    DSQL="drop database ncid; "
+    MYSQL="mysql  -u root "
+
+    if [ -f ncid ]
+    then
+        #drop the db
+        $MYSQL -e "$DSQL"
+        #create the db
+        $MYSQL  -e "$CSQL"
+        #restore the database_backup
+        echo "Restoring database  $DIR"
+        $MYSQL  ncid < ncid
+        rc=$?
+        #restore_status_check $rc
+
+        if [ $rc = 0 ]
+        then
+            echo "Restore of ncid complete"
+        else
+            echo "An error occurred restoring ncid"
+        fi
+
+     else
+        echo "Couldn't find file ncid  to restore"
+    fi
+
+}
 
 
 function run_prestore(){
@@ -76,40 +124,32 @@ function run_prestore(){
     echo
     #save settings to group
         echo "    Saving current database settings for selective import"
-        #mythutil --save-settings --groupname pre_db_restore > /tmp/prestore.out
-        #mythutil --export-settings --generic  --outfile $RESTOREDIR/pre_db.export   --tablelist settings 2>&1 >> /tmp/prestore.out
-        #sed -i "s/settings/settings_pre_db/g" $RESTOREDIR/pre_db.export
-
         mythutil --export-settings --outfile $RESTOREDIR/pre_db.exports.xml  --tablelist settings --generic 2> /dev/null >> /tmp/prestore.out
 
-
     #save storage groups
-        #mythutil --export-settings --generic  --outfile /tmp/pre_db_sg.export   --tablelist storagegroup
         mysqldump   mythconverg storagegroup > $RESTOREDIR/sg.sql
 
     #stop mythbackend
         echo "    Stopping mythbackend"
         lh_backend_control.sh stop
 
-    #do backup
-        echo "    Backing up current database"
-        lh_system_backup_job 2>/dev/null >> /tmp/prestore.out
-
     # restore mythconverg
         restore_db
         mysqldump   mythconverg storagegroup > $RESTOREDIR/backupfile_sg.sql
         mysql mythconverg -e "truncate storagegroup;"
 
+        restore_ncid
     # restore exported settings
         echo "    Importing previous settings into settings_pre_db"
         mythutil --import-settings --infile $RESTOREDIR/pre_db.exports.xml --hostname `hostname` 2>/dev/null  >> /tmp/prestore.out
-        #mythutil  --import-settings  --infile $RESTOREDIR/pre_db.export 2>&1 >> /tmp/prestore.out
+
     # restore storage_groups
         mysql mythconverg < $RESTOREDIR/sg.sql
 
     #start mythbackend
         echo "    Starting mythbackend"
         lh_backend_control.sh start
+
     echo
     echo "Partial restore complete."
     echo "All data for `hostname` has been restored except:"
@@ -120,36 +160,54 @@ function run_prestore(){
 }
 
 function run_full_restore(){
-    echo "Full restore of database"
+    echo "Full restore"
     echo "---------------------------------"
     echo
+    cp -f etc/systemconfig /etc/systemconfig
+
+    if [ $SystemType = Master_backend -o  $SystemType = Standalone ]
+    then
     #stop mythbackend
         echo "    Stopping mythbackend"
         lh_backend_control.sh stop
         echo
-    #do backup
-        echo "    Backing up current database"
-        lh_system_backup_job 2>/dev/null >> /tmp/prestore.out
-        echo
+
     # restore mythconverg
         restore_db
         echo
+        restore_ncid
+        echo
+
     #start mythbackend
         echo "    Starting mythbackend"
         lh_backend_control.sh start
         echo
+
+    else
+    # didn't restore the database, so restore FE settings
+        if [ -e settings ]
+        then
+            rsync -arvp settings/* /usr/MythVantage/templates/settings/
+        fi
+
+        if [ -e /usr/MythVantage/templates/settings/syssettings ]
+        then
+            myth_settings_wrapper.sh -c restore -t syssettings
+            restore_status_check $?
+        else
+            echo "Couldn't find syssettings file"
+        fi
+    fi
+    systemconfig.py -m all
     echo "Restore complete"
 }
 
-
 #-----------------------Main program --------------------------
 
 if [ x$backupfile = x ]
 then
     usage
 fi
-MYTH_RUN_STATUS="1"
-. /etc/profile
 
 prestore="false"
 cleanup="false"
@@ -160,13 +218,14 @@ for i in $*; do
     then
         cleanup="true"
     fi
+
     if [ $i = "partial" ]
     then
         prestore="true"
     fi
 done
-RESTOREDIR=$BACKUPDIR/restore
-mkdir -p $RESTOREDIR
+
+expand_restore_file
 
 if  [ $prestore = "true" ]
 then
@@ -180,3 +239,4 @@ then
     run_cleanup
 fi
 
+exit $restore_status
-- 
cgit v0.12

