#!/bin/bash # 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 BACKUPDIR=/data/storage/disk0/backup/system_backups backupfile=$1 function usage(){ echo "------------------------------------------------------" echo "This program will restore the database from a system backup." echo "Files are expected to be in the $BACKUPDIR" echo "usage:" echo "" echo "lh_system_restore_job \$filename [cleanup partial]" echo "" echo "example: lh_system_restore_job backup.2011-12-22_15-34.tgz " echo "" echo "" echo "If the script is called with cleanup, it will cleanup the the restore dir." echo "If the script is called with restore, it will restore the dir, and then copy back some settings." echo " It's intended to be used with restoring R7 databases only" echo "------------------------------------------------------" exit 1 } function run_cleanup(){ echo "Cleaning up the restore dir" rm -rf $RESTOREDIR/$DIR 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" mkdir -p $RESTOREDIR cp $BACKUPDIR/$backupfile $RESTOREDIR cd $RESTOREDIR && tar -xf $backupfile && cd $DIR if [ -f mythconverg ] then #drop the db $MYSQL -e "$DSQL" #create the db $MYSQL -e "$CSQL" #restore the database_backup echo "Restoring database $DIR" $MYSQL mythconverg < mythconverg if [ $? = 0 ] then echo "Done" else echo "An error occured" fi else echo "Couldn't file a file to restore" fi } function run_prestore(){ echo "Partial restore of database" echo "---------------------------------" 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 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:" echo "* storage groups definitions" echo "* service menu settings" } function run_full_restore(){ echo "Full restore of database" echo "---------------------------------" echo #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 #start mythbackend echo " Starting mythbackend" lh_backend_control.sh start echo echo "Restore complete" } #-----------------------Main program -------------------------- if [ x$backupfile = x ] then usage fi MYTH_RUN_STATUS="1" . /etc/profile prestore="false" cleanup="false" for i in $*; do #echo $i if [ $i = "cleanup" ] then cleanup="true" fi if [ $i = "partial" ] then prestore="true" fi done RESTOREDIR=$BACKUPDIR/restore mkdir -p $RESTOREDIR if [ $prestore = "true" ] then run_prestore else run_full_restore fi if [ $cleanup = "true" ] then run_cleanup fi