summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/bin/mythrestore
blob: 447b0b809f91f2e9c544b7e9c06e6ea5fd961b00 (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
#!/bin/bash

#----------------------------------------------------------------------------
. /usr/LH/bin/backupcommon || {
    echo 1>&2 "Can not load common settings!"
    exit 1
}

. /etc/osd_cat.cfg || {
	color=yellow
	outline=2
	outlinecolour=black
	shadow=0
	shadowcolour=black
	font="-adobe-helvetica-bold-*-*-*-34-*-*-*-*-*-*-*"
}
#----------------------------------------------------------------------------

# Function to display status onscreen
osd()
# Arg_1 = display text Arg_2 = delay time Arg_3 = line num
{
    TEXT=$1
    killall -9 osd_cat
    if [ $3 -gt 1 ]; then
        for ((i=2; i <= $3 ; i++))
        do
            TEXT="\n"$TEXT
        done
    fi
    printf "$TEXT" | osd_cat --lines=6 --pos=middle --align=center --delay=$2 --color=$color --outline=$outline --outlinecolour=$outlinecolour --shadow=$shadow --shadowcolour=$shadowcolour --font=$font &
}

do_file_updates() {  # A function because we need to do this in two places
    echo "Doing any needed file updates..."
    [ -n "$UPDATE_FILES" -a -f "$UPDATE_FILES" -a -x "$UPDATE_FILES" ] &&
        "$UPDATE_FILES"
}

do_db_updates() {
    # We need to redo this since we just restored the old settings...
    lock_myth
    # This is gross, but makes sure that the lock count has a sane value...
    mysql_cmd "update settings set data = '1' where value = 'MythShutdownLock'"

    # Clean up ambiguous schema version settings...
    for val in $(mysql_cmd "select distinct value from settings where value like '%SchemaVer'") ; do
        # Find the numeric maximum version for this schema
        max_ver=$(mysql_cmd "select max(0 + ifnull(data, 0)) from settings where value = '$val'")
        # Wipe out all the existing ones
        mysql_cmd "delete from settings where value = '$val'"
        # Insert a nice clean unique one.
        mysql_cmd "insert into settings set value = '$val', data = '$max_ver'"
    done

    # This table causes problems with mythweb on upgrades...
    mysql_cmd "update settings set data = '0' where value = 'WebDBSchemaVer'"
    mysql_cmd "drop table mythweb_sessions" >/dev/null 2>&1
}

# Let's prevent mythshutdown from shutting down the system.
lock_myth

# Play a sound to let you know I'm starting.
play_sound restore.wav
export DISPLAY=:0.0

# Doing this while the backend is active could be BAD.
stop_mythbackend

# If the standard backup file exists we try to restore the files based
# on our restore list.
if compression=$(compression_type "$BACKUP_TAR") ; then
    osd "Restoring files..." 0 1
    echo "Starting the restore of files..."
    cd /
    expand -c $BACKUP_TAR$compression |
        $TAR xpvf - $RESTORE_LIST $EXCLUSION 2>&1 |
        $SED -e '/Error exit delayed from previous errors/d'
    echo "Completed the restore of files."
fi

# If the db backup file exists start the DB restore and upgrade
if compression=$(compression_type "$BACKUP_SQL") ; then
    echo "Starting the DB restore, this can take a while..."
    echo "Clearing out the existing skeleton..."
    #mysql_stdin < $DROP_SQL
    osd "Recreating database..." 0 2
    echo "Recreating the db..."
    sleep 1
    $MYSQLADMIN -u root create $DATABASE
    osd "Restoring database..." 0 3
    echo "Restoring the data (long)..."
    expand -c $BACKUP_SQL$compression | mysql_stdin
    echo "Doing any needed db updates..."
    [ -n "$UPDATE_SQL" -a -f "$UPDATE_SQL" ] &&
        mysql_stdin < $UPDATE_SQL
    echo "Completed the DB restore."
fi

osd "Testing restore..." 0 4
echo "Sanity checking your restore..."
play_sound vr.wav
# Play a sound to let you know the outcome.
if check_files_and_tables $RESTORE_LIST ; then
    osd "Restore completed successfully." 5 5
    echo "Restore passes all checks."
    COMPLETE_MSG="Last restore completed on `date '+%D @ %-I:%M %p'`"
    play_sound restored.wav
    STATUS=0
else
    osd "Restore FAILED." 5 5
    echo "The restore failed or was already modified!"
    COMPLETE_MSG="Last restore FAILED on `date '+%D @ %-I:%M %p'`"
    play_sound rf.wav
    STATUS=1
fi

# Add Last restore status to menu item
if grep "<description>" /usr/share/mythtv/themes/defaultmenu/mythrestore.xml >/dev/null 2>&1
then
    sed -i "s_\<description\>.*\<description\>_description\>$COMPLETE_MSG\<\/description_" /usr/share/mythtv/themes/defaultmenu/mythrestore.xml
else
    sed -i.orig " /NONE/ i\     \<description\>$COMPLETE_MSG\<\/description\>" /usr/share/mythtv/themes/defaultmenu/mythrestore.xml
fi

# Make any updates _after_ we verify the backup...
do_db_updates
do_file_updates

#post restore fixup for sshd
/usr/MythVantage/bin/systemconfig.py -m user

# Now it's more or less safe to restart the backend.
start_mythbackend

exit $STATUS