forked from hans/lwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_restore.php
executable file
·184 lines (162 loc) · 7.41 KB
/
backup_restore.php
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
183
184
<?php
/**************************************************************
"Learning with Texts" (LWT) is free and unencumbered software
released into the PUBLIC DOMAIN.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a
compiled binary, for any purpose, commercial or non-commercial,
and by any means.
In jurisdictions that recognize copyright laws, the author or
authors of this software dedicate any and all copyright
interest in the software to the public domain. We make this
dedication for the benefit of the public at large and to the
detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity
of all present and future rights to this software under
copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
For more information, please refer to [http://unlicense.org/].
***************************************************************/
/**************************************************************
Call: backup_restore.php?....
... restore=xxx ... do restore
... backup=xxx ... do backup
... empty=xxx ... do truncate
Backup/Restore/Empty LWT Database
***************************************************************/
require_once( 'settings.inc.php' );
require_once( 'connect.inc.php' );
require_once( 'dbutils.inc.php' );
require_once( 'utilities.inc.php' );
$message = '';
if ($tbpref == '')
$pref = "";
else
$pref = substr($tbpref,0,-1) . "-";
// RESTORE
if (isset($_REQUEST['restore'])) {
if ( isset($_FILES["thefile"]) && $_FILES["thefile"]["tmp_name"] != "" && $_FILES["thefile"]["error"] == 0 ) {
$handle = gzopen ($_FILES["thefile"]["tmp_name"], "r");
if ($handle === FALSE) {
$message = "Error: Restore file could not be opened";
} // $handle not OK
else { // $handle OK
$message = restore_file($handle, "Database");
} // $handle OK
} // restore file specified
else {
$message = "Error: No Restore file specified";
}
}
// BACKUP
elseif (isset($_REQUEST['backup'])) {
$tables = array('archivedtexts', 'archtexttags', 'languages', 'sentences', 'settings', 'tags', 'tags2', 'textitems', 'texts', 'texttags', 'words', 'wordtags');
$fname = "lwt-backup-" . $pref . date('Y-m-d-H-i-s') . ".sql.gz";
$out = "-- " . $fname . "\n";
foreach($tables as $table) { // foreach table
$result = do_mysqli_query('SELECT * FROM ' . $tbpref . $table);
$num_fields = mysqli_num_fields($result);
$out .= "\nDROP TABLE IF EXISTS " . $table . ";\n";
$row2 = mysqli_fetch_row(do_mysqli_query('SHOW CREATE TABLE ' . $tbpref . $table));
$out .= str_replace($tbpref . $table, $table, str_replace("\n"," ",$row2[1])) . ";\n";
if ($table !== 'sentences' && $table !== 'textitems') {
while ($row = mysqli_fetch_row($result)) { // foreach record
$return = 'INSERT INTO ' . $table . ' VALUES(';
for ($j=0; $j < $num_fields; $j++) { // foreach field
if (isset($row[$j])) {
$return .= "'" . mysqli_real_escape_string($GLOBALS['DBCONNECTION'], $row[$j]) . "'";
} else {
$return .= 'NULL';
}
if ($j < ($num_fields-1)) $return .= ',';
} // foreach field
$out .= $return . ");\n";
} // foreach record
} // if
} // foreach table
header('Content-type: application/x-gzip');
header("Content-disposition: attachment; filename=" . $fname);
echo gzencode($out,9);
exit();
}
// EMPTY
elseif (isset($_REQUEST['empty'])) {
$dummy = runsql('TRUNCATE ' . $tbpref . 'archivedtexts','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'archtexttags','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'languages','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'sentences','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'tags','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'tags2','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'textitems','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'texts','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'texttags','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'words','');
$dummy = runsql('TRUNCATE ' . $tbpref . 'wordtags', '');
$dummy = runsql('DELETE FROM ' . $tbpref . 'settings where StKey = \'currenttext\'', '');
optimizedb();
get_tags($refresh = 1);
get_texttags($refresh = 1);
$message = "Database content has been deleted (but settings have been kept)";
}
pagestart('Backup/Restore/Empty Database',true);
echo error_message_with_hide($message,1);
if ($tbpref == '')
$prefinfo = "(Default Table Set)";
else
$prefinfo = "(Table Set: <i>" . tohtml(substr($tbpref,0,-1)) . "</i>)";
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return confirm('Are you sure?');">
<table class="tab1" cellspacing="0" cellpadding="5">
<tr>
<th class="th1 center">Backup</th>
<td class="td1">
<p class="smallgray2">
The database <i><?php echo tohtml($dbname); ?></i> <?php echo $prefinfo; ?> will be exported to a gzipped SQL file.<br />Please keep this file in a safe place.<br />If necessary, you can recreate the database via the Restore function below.<br />Important: If the backup file is too large, the restore may not be possible (see limits below).</p>
<p class="right"> <br /><input type="submit" name="backup" value="Download LWT Backup" /></p>
</td>
</tr>
<tr>
<th class="th1 center">Restore</th>
<td class="td1">
<p class="smallgray2">
The database <i><?php echo tohtml($dbname); ?></i> <?php echo $prefinfo; ?> will be <b>replaced</b> by the data in the specified backup file<br />(gzipped or normal SQL file, created above).<br /><br /><span class="smallgray">Important: If the backup file is too large, the restore may not be possible.<br />Upload limits (in bytes): <b>post_max_size = <?php echo ini_get('post_max_size'); ?> / upload_max_filesize = <?php echo ini_get('upload_max_filesize'); ?></b><br />
If needed, increase in "<?php echo tohtml(php_ini_loaded_file()); ?>" and restart server.<br /> </span></p>
<p><input name="thefile" type="file" /></p>
<p class="right"> <br /><span class="red2">YOU MAY LOSE DATA - BE CAREFUL: </span>
<input type="submit" name="restore" value="Restore from LWT Backup" /></p>
</td>
</tr>
<tr>
<th class="th1 center">Install<br />LWT<br />Demo</th>
<td class="td1">
<p class="smallgray2">
The database <i><?php echo tohtml($dbname); ?></i> <?php echo $prefinfo; ?> will be <b>replaced</b> by the LWT demo database.</p>
<p class="right"> <br />
<input type="button" value="Install LWT Demo Database" onclick="location.href='install_demo.php';" />
</td>
</tr>
<tr>
<th class="th1 center">Empty<br />Database</th>
<td class="td1">
<p class="smallgray2">
Empty (= <b>delete</b> the contents of) all tables - except the Settings - of your database <i><?php echo tohtml($dbname); ?></i> <?php echo $prefinfo; ?>.</p>
<p class="right"> <br /><span class="red2">YOU MAY LOSE DATA - BE CAREFUL: </span>
<input type="submit" name="empty" value="Empty LWT Database" />
</td>
</tr>
<tr>
<td class="td1 right" colspan="2">
<input type="button" value="<< Back" onclick="location.href='index.php';" /></td>
</tr>
</table>
</form>
<?php
pageend();
?>