forked from mkucej/i-librarian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
283 lines (236 loc) · 11.3 KB
/
backup.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
include_once 'data.php';
include_once 'functions.php';
set_time_limit(0);
if ($hosted == true)
die();
function safe_copy($file, $directory) {
$path_to = str_replace(IL_LIBRARY_PATH, $directory, $file);
if (!is_file($path_to) || (is_file($path_to) && filemtime($file) > filemtime($path_to))) {
$fp = fopen($file, "r");
if (flock($fp, LOCK_SH)) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
exec('copy "' . $file . '" "' . $path_to . '" /Y');
} else {
exec(escapeshellcmd('cp -p "' . $file . '" "' . $path_to . '"'));
}
flock($fp, LOCK_UN);
fclose($fp);
} else {
fclose($fp);
}
}
}
if (isset($_SESSION['auth']) && $_SESSION['permissions'] == 'A') {
if (!empty($_GET['backup'])) {
$directory = '';
if (isset($_GET['directory'])) {
$directory = $_GET['directory'];
}
// Convert DIRECTORY_SEPARATOR to forward slashes.
$directory = str_replace(DIRECTORY_SEPARATOR, '/', $directory);
// Remove directory trailing slash.
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
if (empty($directory)) {
/*
* Initial backup form. Calculate required drive space.
*/
$required_space = null;
$f_number = 1;
$lit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(IL_LIBRARY_PATH));
while ($lit->valid()) {
$file = $lit->key();
if (is_file($file)) {
$required_space += filesize($file);
$f_number++;
}
$lit->next();
}
} else {
/*
* Backup.
*/
// Directory must end with /library.
if (substr($directory, -8) !== '/library') {
$directory .= '/library';
}
if (!is_dir($directory)) {
$is_dir = @mkdir($directory);
} else {
$is_dir = true;
}
if ($is_dir && is_writable($directory)) {
database_connect(IL_USER_DATABASE_PATH, 'users');
save_setting($dbHandle, 'backup_dir', $directory);
$dbHandle = null;
// Create folders.
$lit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(IL_LIBRARY_PATH));
while ($lit->valid()) {
$dir = $lit->key();
if (is_dir($dir)) {
$path_to = str_replace(IL_LIBRARY_PATH, $directory, $dir);
if (!is_dir($path_to)) {
mkdir($path_to);
}
}
$lit->next();
}
// Copy files.
$lit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(IL_LIBRARY_PATH));
while ($lit->valid()) {
$file = $lit->key();
if (is_file($file)) {
safe_copy($file, $directory);
}
$lit->next();
}
// DELETE NON-EXISTENT FILES
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while ($it->valid()) {
$backupfile = $it->key();
if (is_file($backupfile)) {
$libfile = str_replace($directory, IL_LIBRARY_PATH, $backupfile);
if (!is_file($libfile)) {
@unlink($backupfile);
}
}
$it->next();
}
die('Done');
} else {
die('Error! Access denied or directory cannot be created.');
}
}
$backup_dir = get_setting('backup_dir');
?>
<table style="width: 100%"><tr><td class="details alternating_row"><b>Backup</b></td></tr></table>
<div class="item-sticker ui-widget-content ui-corner-all" style="margin:auto;margin-top:100px;width:500px">
<div class="ui-dialog-titlebar ui-state-default ui-corner-top" style="border:0;padding-left:1em">
Enter the directory path, where the backup copy should be created:
</div>
<div class="separator" style="margin:0"></div>
<div class="alternating_row items ui-corner-bottom">
<form action="backup.php" method="GET" class="form-backup">
<input type="hidden" name="backup" value="1">
<button class="open-dirs-button" title="Browse directories"><i class="fa fa-folder-open"></i></button>
<input type="text" size="50" style="width:420px" name="directory" value="<?php if (!empty($backup_dir)) print $backup_dir; ?>"><br>
Total library size: <?php
function formatBytes($size, $precision = 1) {
$base = log($size) / log(1024);
$suffixes = array('', 'k', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}
print formatBytes($required_space);
?>B
(<?php print number_format($f_number, '0', '.', ','); ?> files)<br>
Make sure that the destination drive has sufficient free space.<br>
<input type="submit" value="Proceed">
</form>
</div>
</div>
<?php
} elseif (!empty($_GET['restore'])) {
$directory = '';
if (isset($_GET['directory'])) {
$directory = $_GET['directory'];
}
// Convert DIRECTORY_SEPARATOR to forward slashes.
$directory = str_replace(DIRECTORY_SEPARATOR, '/', $directory);
// Remove directory trailing slash.
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
if (!empty($directory)) {
/*
* Restore.
*/
// Directory must end with /library.
if (substr($directory, -8) !== '/library') {
$directory .= '/library';
}
if (!is_dir($directory)) {
$is_dir = false;
} else {
$is_dir = true;
}
if ($is_dir && is_writable(IL_LIBRARY_PATH)) {
if (!is_readable($directory . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'library.sq3'))
die('Error! Access denied or directory does not exist.');
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
exec("del /q \"" . IL_LIBRARY_PATH . "\"");
exec("rmdir \"" . IL_DATABASE_PATH . "\" /s/q");
exec("rmdir \"" . IL_SUPPLEMENT_PATH . "\" /s/q");
exec("rmdir \"" . IL_IMAGE_PATH . "\" /s/q");
exec("xcopy \"" . $directory . "\" \"" . IL_LIBRARY_PATH . "\" /c /v /q /s /e /h /y", $a);
} else {
exec("rm -f \"" . IL_LIBRARY_PATH . DIRECTORY_SEPARATOR . "*.*\"");
exec("rm -rf \"" . IL_DATABASE_PATH . "\"");
exec("rm -rf \"" . IL_SUPPLEMENT_PATH . "\"");
exec("rm -rf \"" . IL_IMAGE_PATH . "\"");
exec(escapeshellcmd("cp -r \"" . $directory . "\" \"" . IL_LIBRARY_PATH . DIRECTORY_SEPARATOR . "\""));
}
die('Done');
} else {
die('Error! Access denied or directory does not exist.');
}
}
$backup_dir = get_setting('backup_dir');
?>
<table style="width: 100%"><tr><td class="details alternating_row"><b>Restore</b></td></tr></table>
<div class="item-sticker ui-widget-content ui-corner-all" style="margin:auto;margin-top:100px;width:500px">
<div class="ui-dialog-titlebar ui-state-default ui-corner-top" style="border:0;padding-left:1em">
Enter the directory path, where the backup copy is stored:<br>
</div>
<div class="separator" style="margin:0"></div>
<div class="alternating_row items ui-corner-bottom">
<form action="backup.php" method="GET" class="form-backup">
<input type="hidden" name="restore" value="1">
<button class="open-dirs-button" title="Browse directories"><i class="fa fa-folder-open"></i></button>
<input type="text" size="50" style="width:420px" name="directory" value="<?php if (!empty($backup_dir)) print $backup_dir; ?>"><br>
<span class="ui-state-error-text">This action will permanently delete your current library!</span><br>
<input type="submit" value="Proceed">
</form>
</div>
</div>
<?php
} else {
?>
<table style="width: 100%"><tr><td class="details alternating_row"><b>Backup Assistant</b></td></tr></table>
<div style="text-align:center">
<div style="width:250px;margin:auto;margin-top:100px">
<div style="width:250px">
<span id="unlock-restore" class="fa fa-lock" style="float:right;cursor:pointer" title="Unlock restore"></span>
</div>
<div style="clear:both"></div>
<div id="select-backup" class="item-sticker ui-widget-content ui-corner-all" style="margin-left:4px;margin-top:4px;width:100px;float:left;text-align:left;cursor:pointer">
<div class="ui-dialog-titlebar ui-state-default ui-corner-top" style="text-align:center;border:0">Backup</div>
<div class="separator" style="margin:0"></div>
<div class="alternating_row ui-corner-bottom" style="padding:1em;overflow:auto;height:1.4em;text-align:center">
<span class="fa fa-file-o"></span>
<span class="fa fa-arrow-right"></span>
<span class="fa fa-save"></span>
</div>
</div>
<div id="select-restore" class="item-sticker ui-widget-content ui-corner-all ui-state-disabled" style="margin-left:4px;margin-top:4px;width:100px;float:right;text-align:left;cursor:pointer">
<div class="ui-dialog-titlebar ui-state-default ui-corner-top" style="text-align:center;border:0">Restore</div>
<div class="separator" style="margin:0"></div>
<div class="alternating_row ui-corner-bottom" style="padding:1em;overflow:auto;height:1.4em;text-align:center">
<span class="fa fa-save"></span>
<span class="fa fa-arrow-right"></span>
<span class="fa fa-file-o"></span>
</div>
</div>
</div>
<div style="clear:both"></div>
<div><br><br>Make sure that nobody is using the library.</div>
</div>
<?php
}
?>
<?php
} else {
print "<p>Super User permissions required.</p>";
}
?>