summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/lh_system_restore_job
blob: eb3bd51e7b59643ffab985c2ea854df97b3bb2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/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