Compact css tweak
[openemr.git] / interface / main / backup.php
blob0ee671f921405c168c20fc015ff3af4668762583
1 <?php
2 /* $Id$ */
3 // Copyright (C) 2008-2014, 2016 Rod Roark <rod@sunsetsystems.com>
4 // Adapted for cross-platform operation by Bill Cernansky (www.mi-squared.com)
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This script creates a backup tarball and sends it to the users's
12 // browser for download. The tarball includes:
14 // * an OpenEMR database dump (gzipped)
15 // * a phpGACL database dump (gzipped), if phpGACL is used and has
16 // its own database
17 // * the OpenEMR web directory (.tar.gz)
18 // * the phpGACL web directory (.tar.gz), if phpGACL is used
20 // The OpenEMR web directory is important because it includes config-
21 // uration files, patient documents, and possible customizations, and
22 // also because the database structure is dependent on the installed
23 // OpenEMR version.
25 // This script depends on execution of some external programs:
26 // mysqldump & pg_dump. It has been tested with Debian and Ubuntu
27 // Linux and with Windows XP.
28 // Do not assume that it works for you until you have successfully
29 // tested a restore!
30 set_time_limit(0);
31 require_once("../globals.php");
32 require_once("$srcdir/acl.inc");
33 require_once("$srcdir/log.inc");
35 // Prevent scripts calling Globals from being blocked.
37 session_write_close();
39 if (!extension_loaded('zlib')) {
40 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
43 if (!function_exists('gzopen') && function_exists('gzopen64')) {
44 function gzopen($filename, $mode, $use_include_path = 0)
46 return gzopen64($filename, $mode, $use_include_path);
51 if (!acl_check('admin', 'super')) {
52 die(xl('Not authorized', '', '', '!'));
55 include_once("Archive/Tar.php");
57 // Set up method, which will depend on OS and if pear tar.php is installed
58 if (class_exists('Archive_Tar')) {
59 # pear tar.php is installed so can use os independent method
60 $newBackupMethod = true;
61 } elseif (IS_WINDOWS) {
62 # without the tar.php module, can't run backup in windows
63 die(xl("Error. You need to install the Archive/Tar.php php module."));
64 } else {
65 # without the tar.php module, can run via system commands in non-windows
66 $newBackupMethod = false;
69 $BTN_TEXT_CREATE = xl('Create Backup');
70 $BTN_TEXT_EXPORT = xl('Export Configuration');
71 $BTN_TEXT_IMPORT = xl('Import Configuration');
72 // ViSolve: Create Log Backup button
73 $BTN_TEXT_CREATE_EVENTLOG = xl('Create Eventlog Backup');
75 $form_step = isset($_POST['form_step']) ? trim($_POST['form_step']) : '0';
76 $form_status = isset($_POST['form_status' ]) ? trim($_POST['form_status' ]) : '';
78 if (!empty($_POST['form_export'])) {
79 $form_step = 101;
82 if (!empty($_POST['form_import'])) {
83 $form_step = 201;
86 //ViSolve: Assign Unique Number for the Log Creation
87 if (!empty($_POST['form_backup'])) {
88 $form_step = 301;
91 // When true the current form will submit itself after a brief pause.
92 $auto_continue = false;
94 # set up main paths
95 $backup_file_prefix = "emr_backup";
96 $backup_file_suffix = ".tar";
97 $TMP_BASE = $GLOBALS['temporary_files_dir'] . "/openemr_web_backup";
98 $BACKUP_DIR = $TMP_BASE . "/emr_backup";
99 $TAR_FILE_PATH = $TMP_BASE . DIRECTORY_SEPARATOR . $backup_file_prefix . $backup_file_suffix;
100 $EXPORT_FILE = $GLOBALS['temporary_files_dir'] . "/openemr_config.sql";
101 $MYSQL_PATH = $GLOBALS['mysql_bin_dir'];
102 $PERL_PATH = $GLOBALS['perl_bin_dir'];
104 if ($form_step == 6) {
105 header("Pragma: public");
106 header("Expires: 0");
107 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
108 header("Content-Type: application/force-download");
109 header("Content-Length: " . filesize($TAR_FILE_PATH));
110 header("Content-Disposition: attachment; filename=" . basename($TAR_FILE_PATH));
111 header("Content-Description: File Transfer");
113 if (is_file($TAR_FILE_PATH)) {
114 $chunkSize = 1024 * 1024;
115 $handle = fopen($TAR_FILE_PATH, 'rb');
116 while (!feof($handle)) {
117 $buffer = fread($handle, $chunkSize);
118 echo $buffer;
119 ob_flush();
120 flush();
122 fclose($handle);
123 } else {
124 obliterate_dir($BACKUP_DIR);
125 $dieMsg = xlt("Backup Failed missing generated file");
126 die($dieMsg);
128 unlink($TAR_FILE_PATH);
129 obliterate_dir($BACKUP_DIR);
130 exit(0);
133 if ($form_step == 104) {
134 header("Pragma: public");
135 header("Expires: 0");
136 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
137 header("Content-Type: application/force-download");
138 header("Content-Length: " . filesize($EXPORT_FILE));
139 header("Content-Disposition: attachment; filename=" . basename($EXPORT_FILE));
140 header("Content-Description: File Transfer");
141 readfile($EXPORT_FILE);
142 unlink($EXPORT_FILE);
143 exit(0);
146 <html>
148 <head>
149 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
150 <title><?php xl('Backup', 'e'); ?></title>
151 </head>
153 <body class="body_top">
154 <center>
155 &nbsp;<br />
156 <form method='post' action='backup.php' enctype='multipart/form-data'>
158 <table<?php if ($form_step != 101) {
159 echo " style='width:50em'";
160 } ?>>
161 <tr>
162 <td>
164 <?php
165 $cmd = '';
166 $mysql_cmd = $MYSQL_PATH . DIRECTORY_SEPARATOR . 'mysql';
167 $mysql_dump_cmd = $mysql_cmd . 'dump';
168 $mysql_ssl = '';
169 if (file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-ca")) {
170 // Support for mysql SSL encryption
171 $mysql_ssl = " --ssl-ca=" . $GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-ca ";
172 if (file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-key") &&
173 file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-cert")) {
174 // Support for mysql SSL client based cert authentication
175 $mysql_ssl .= "--ssl-cert=" . $GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-cert ";
176 $mysql_ssl .= "--ssl-key=" . $GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-key ";
180 $file_to_compress = ''; // if named, this iteration's file will be gzipped after it is created
181 $eventlog=0; // Eventlog Flag
183 if ($form_step == 0) {
184 echo "<table>\n";
185 echo " <tr>\n";
186 echo " <td><input type='submit' name='form_create' value='$BTN_TEXT_CREATE' /></td>\n";
187 echo " <td>" . xl('Create and download a full backup') . "</td>\n";
188 echo " </tr>\n";
189 // The config import/export feature is optional.
190 if (!empty($GLOBALS['configuration_import_export'])) {
191 echo " <tr>\n";
192 echo " <td><input type='submit' name='form_export' value='$BTN_TEXT_EXPORT' /></td>\n";
193 echo " <td>" . xl('Download configuration data') . "</td>\n";
194 echo " </tr>\n";
195 echo " <tr>\n";
196 echo " <td><input type='submit' name='form_import' value='$BTN_TEXT_IMPORT' /></td>\n";
197 echo " <td>" . xl('Upload configuration data') . "</td>\n";
198 echo " </tr>\n";
201 // ViSolve : Add ' Create Log table backup Button'
202 echo " <tr>\n";
203 echo " <td><input type='submit' name='form_backup' value='$BTN_TEXT_CREATE_EVENTLOG' /></td>\n";
204 echo " <td>" . xl('Create Eventlog Backup') . "</td>\n";
205 echo " </tr>\n";
206 echo " <tr>\n";
207 echo " <td></td><td class='text'><b>" . xl('Note')."</b>&nbsp;" . xl('Please refer to').'&nbsp;README-Log-Backup.txt&nbsp;'.xl('file in the Documentation directory to learn how to automate the process of creating log backups') . "</td>\n";
208 echo " </tr>\n";
209 echo "</table>\n";
212 if ($form_step == 1) {
213 $form_status .= xl('Dumping OpenEMR database') . "...<br />";
214 echo nl2br($form_status);
215 if (file_exists($TAR_FILE_PATH)) {
216 if (! unlink($TAR_FILE_PATH)) {
217 die(xl("Couldn't remove old backup file:") . " " . $TAR_FILE_PATH);
221 if (! obliterate_dir($TMP_BASE)) {
222 die(xl("Couldn't remove dir:"). " " . $TMP_BASE);
225 if (! mkdir($BACKUP_DIR, 0777, true)) {
226 die(xl("Couldn't create backup dir:") . " " . $BACKUP_DIR);
229 $file_to_compress = "$BACKUP_DIR/openemr.sql"; // gzip this file after creation
231 if ($GLOBALS['include_de_identification']==1) {
232 //include routines during backup when de-identification is enabled
233 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
234 " -p" . escapeshellarg($sqlconf["pass"]) .
235 " -h " . escapeshellarg($sqlconf["host"]) .
236 " --port=".escapeshellarg($sqlconf["port"]) .
237 " --routines".
238 " --opt --quote-names -r $file_to_compress $mysql_ssl " .
239 escapeshellarg($sqlconf["dbase"]);
240 } else {
241 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
242 " -p" . escapeshellarg($sqlconf["pass"]) .
243 " -h " . escapeshellarg($sqlconf["host"]) .
244 " --port=".escapeshellarg($sqlconf["port"]) .
245 " --opt --quote-names -r $file_to_compress $mysql_ssl " .
246 escapeshellarg($sqlconf["dbase"]);
249 $auto_continue = true;
252 if ($form_step == 2) {
253 if (!empty($phpgacl_location) && $gacl_object->_db_name != $sqlconf["dbase"]) {
254 $form_status .= xl('Dumping phpGACL database') . "...<br />";
255 echo nl2br($form_status);
256 $file_to_compress = "$BACKUP_DIR/phpgacl.sql"; // gzip this file after creation
257 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($gacl_object->_db_user) .
258 " -p" . escapeshellarg($gacl_object->_db_password) .
259 " --opt --quote-names -r $file_to_compress $mysql_ssl " .
260 escapeshellarg($gacl_object->_db_name);
261 $auto_continue = true;
262 } else {
263 ++$form_step;
267 if ($form_step == 3) {
268 $form_status .= xl('Dumping OpenEMR web directory tree') . "...<br />";
269 echo nl2br($form_status);
270 $cur_dir = getcwd();
271 chdir($webserver_root);
273 // Select the files and directories to archive. Basically everything
274 // except site-specific data for other sites.
275 $file_list = array();
276 $dh = opendir($webserver_root);
277 if (!$dh) {
278 die("Cannot read directory '$webserver_root'.");
281 while (false !== ($filename = readdir($dh))) {
282 if ($filename == '.' || $filename == '..') {
283 continue;
286 if ($filename == 'sites') {
287 // Omit other sites.
288 $file_list[] = "$filename/" . $_SESSION['site_id'];
289 } else {
290 $file_list[] = $filename;
294 closedir($dh);
296 $arch_file = $BACKUP_DIR . DIRECTORY_SEPARATOR . "openemr.tar.gz";
297 if (!create_tar_archive($arch_file, "gz", $file_list)) {
298 die(xl("An error occurred while dumping OpenEMR web directory tree"));
301 chdir($cur_dir);
302 $auto_continue = true;
305 if ($form_step == 4) {
306 if ((!empty($phpgacl_location)) && ($phpgacl_location != $srcdir."/../gacl")) {
307 $form_status .= xl('Dumping phpGACL web directory tree') . "...<br />";
308 echo nl2br($form_status);
309 $cur_dir = getcwd();
310 chdir($phpgacl_location);
311 $file_list = array('.'); // archive entire directory
312 $arch_file = $BACKUP_DIR . DIRECTORY_SEPARATOR . "phpgacl.tar.gz";
313 if (!create_tar_archive($arch_file, "gz", $file_list)) {
314 die(xl("An error occurred while dumping phpGACL web directory tree"));
317 chdir($cur_dir);
318 $auto_continue = true;
319 } else {
320 ++$form_step;
324 if ($form_step == 5) { // create the final compressed tar containing all files
325 $form_status .= xl('Backup file has been created. Will now send download.') . "<br />";
326 echo nl2br($form_status);
327 $cur_dir = getcwd();
328 chdir($BACKUP_DIR);
329 $file_list = array('.');
330 if (!create_tar_archive($TAR_FILE_PATH, '', $file_list)) {
331 die(xl("Error: Unable to create downloadable archive"));
334 chdir($cur_dir);
335 /* To log the backup event */
336 if ($GLOBALS['audit_events_backup']) {
337 newEvent("backup", $_SESSION['authUser'], $_SESSION['authProvider'], 0, "Backup is completed");
340 $auto_continue = true;
343 if ($form_step == 101) {
344 echo "<p><b>&nbsp;" . xl('Select the configuration items to export') . ":</b></p>";
346 echo "<table cellspacing='10' cellpadding='0'>\n<tr>\n<td valign='top' nowrap>\n";
348 echo "<b>" . xlt('Tables') . "</b><br />\n";
349 echo "<input type='checkbox' name='form_cb_services' value='1' />\n";
350 echo " " . xl('Services') . "<br />\n";
351 echo "<input type='checkbox' name='form_cb_products' value='1' />\n";
352 echo " " . xl('Products') . "<br />\n";
353 echo "<input type='checkbox' name='form_cb_prices' value='1' />\n";
354 echo " " . xl('Prices') . "<br />\n";
355 echo "<input type='checkbox' name='form_cb_categories' value='1' />\n";
356 echo " " . xl('Document Categories') . "<br />\n";
357 echo "<input type='checkbox' name='form_cb_feesheet' value='1' />\n";
358 echo " " . xl('Fee Sheet Options') . "<br />\n";
359 echo "<input type='checkbox' name='form_cb_lang' value='1' />\n";
360 echo " " . xl('Translations') . "<br />\n";
362 // Multi-select for lists.
363 echo "</td><td valign='top'>\n";
364 echo "<b>" . xlt('Lists') . "</b><br />\n";
365 echo "<select multiple name='form_sel_lists[]' size='15'>";
366 $lres = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
367 "list_id = 'lists' AND activity = 1 ORDER BY title, seq");
368 while ($lrow = sqlFetchArray($lres)) {
369 echo "<option value='" . attr($lrow['option_id']) . "'";
370 echo ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
373 echo "</select>\n";
375 // Multi-select for layouts.
376 echo "</td><td valign='top'>\n";
377 echo "<b>" . xlt('Layouts') . "</b><br />\n";
378 echo "<select multiple name='form_sel_layouts[]' size='15'>";
379 $lres = sqlStatement("SELECT grp_form_id, grp_title FROM layout_group_properties WHERE " .
380 "grp_group_id = '' AND grp_activity = 1 ORDER BY grp_form_id");
381 while ($lrow = sqlFetchArray($lres)) {
382 $key = $lrow['grp_form_id'];
383 echo "<option value='" . attr($key) . "'";
384 echo ">" . text($key) . ": " . text(xl_layout_label($lrow['grp_title'])) . "</option>\n";
387 echo "</select>\n";
389 echo "</td>\n</tr>\n</table>\n";
390 echo "&nbsp;<br /><input type='submit' value='" . xl('Continue') . "' />\n";
393 if ($form_step == 102) {
394 $tables = '';
395 if ($_POST['form_cb_services' ]) {
396 $tables .= ' codes';
399 if ($_POST['form_cb_products' ]) {
400 $tables .= ' drugs drug_templates';
403 if ($_POST['form_cb_prices' ]) {
404 $tables .= ' prices';
407 if ($_POST['form_cb_categories']) {
408 $tables .= ' categories categories_seq';
411 if ($_POST['form_cb_feesheet' ]) {
412 $tables .= ' fee_sheet_options';
415 if ($_POST['form_cb_lang' ]) {
416 $tables .= ' lang_languages lang_constants lang_definitions';
419 if ($tables || is_array($_POST['form_sel_lists']) || is_array($_POST['form_sel_layouts'])) {
420 $form_status .= xl('Creating export file') . "...<br />";
421 echo nl2br($form_status);
422 if (file_exists($EXPORT_FILE)) {
423 if (! unlink($EXPORT_FILE)) {
424 die(xl("Couldn't remove old export file: ") . $EXPORT_FILE);
428 // The substitutions below use perl because sed's not usually on windows systems.
429 $perl = $PERL_PATH . DIRECTORY_SEPARATOR . 'perl';
432 # This condition was added because the windows operating system uses different syntax for the shell commands.
433 # The test is if it is the windows operating system.
434 if (IS_WINDOWS) {
435 # This section sets the character_set_client to utf8 in the sql file as part or the import property.
436 # windows will place the quotes in the outputted code if they are there. we removed them here.
437 $cmd = "echo SET character_set_client = utf8; > $EXPORT_FILE & ";
438 } else {
439 $cmd = "echo 'SET character_set_client = utf8;' > $EXPORT_FILE;";
442 if ($tables) {
443 $cmd .= "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
444 " -p" . escapeshellarg($sqlconf["pass"]) .
445 " -h " . escapeshellarg($sqlconf["host"]) .
446 " --port=".escapeshellarg($sqlconf["port"]) .
447 " --opt --quote-names $mysql_ssl " .
448 escapeshellarg($sqlconf["dbase"]) . " $tables";
449 if (IS_WINDOWS) {
450 # The Perl script differs in windows also.
451 $cmd .= " | $perl -pe \"s/ DEFAULT CHARSET=utf8//i; s/ collate[ =][^ ;,]*//i;\"" .
452 " >> $EXPORT_FILE & ";
453 } else {
454 $cmd .= " | $perl -pe 's/ DEFAULT CHARSET=utf8//i; s/ collate[ =][^ ;,]*//i;'" .
455 " > $EXPORT_FILE;";
459 $dumppfx = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
460 " -p" . escapeshellarg($sqlconf["pass"]) .
461 " -h " . escapeshellarg($sqlconf["host"]) .
462 " --port=".escapeshellarg($sqlconf["port"]) .
463 " --skip-opt --quote-names --complete-insert --no-create-info $mysql_ssl";
464 // Individual lists.
465 if (is_array($_POST['form_sel_lists'])) {
466 foreach ($_POST['form_sel_lists'] as $listid) {
467 if (IS_WINDOWS) {
468 # windows will place the quotes in the outputted code if they are there. we removed them here.
469 $cmd .= " echo DELETE FROM list_options WHERE list_id = '$listid'; >> $EXPORT_FILE & ";
470 $cmd .= " echo DELETE FROM list_options WHERE list_id = 'lists' AND option_id = '$listid'; >> $EXPORT_FILE & ";
471 } else {
472 $cmd .= "echo \"DELETE FROM list_options WHERE list_id = '$listid';\" >> $EXPORT_FILE;";
473 $cmd .= "echo \"DELETE FROM list_options WHERE list_id = 'lists' AND option_id = '$listid';\" >> $EXPORT_FILE;";
475 $cmd .= $dumppfx .
476 " --where=\"list_id = 'lists' AND option_id = '$listid' OR list_id = '$listid' " .
477 "ORDER BY list_id != 'lists', seq, title\" " .
478 escapeshellarg($sqlconf["dbase"]) . " list_options";
479 if (IS_WINDOWS) {
480 # windows uses the & to join statements.
481 $cmd .= " >> $EXPORT_FILE & ";
482 } else {
483 $cmd .= " >> $EXPORT_FILE;";
488 // Individual layouts.
489 if (is_array($_POST['form_sel_layouts'])) {
490 foreach ($_POST['form_sel_layouts'] as $layoutid) {
491 if (IS_WINDOWS) {
492 # windows will place the quotes in the outputted code if they are there. we removed them here.
493 $cmd .= " echo DELETE FROM layout_options WHERE form_id = '$layoutid'; >> $EXPORT_FILE & ";
494 } else {
495 $cmd .= "echo \"DELETE FROM layout_options WHERE form_id = '$layoutid';\" >> $EXPORT_FILE;";
497 if (IS_WINDOWS) {
498 # windows will place the quotes in the outputted code if they are there. we removed them here.
499 $cmd .= "echo \"DELETE FROM layout_group_properties WHERE grp_form_id = '$layoutid';\" >> $EXPORT_FILE &;";
500 } else {
501 $cmd .= "echo \"DELETE FROM layout_group_properties WHERE grp_form_id = '$layoutid';\" >> $EXPORT_FILE;";
503 $cmd .= $dumppfx .
504 " --where=\"grp_form_id = '$layoutid'\" " .
505 escapeshellarg($sqlconf["dbase"]) . " layout_group_properties";
506 if (IS_WINDOWS) {
507 # windows uses the & to join statements.
508 $cmd .= " >> $EXPORT_FILE & ";
509 } else {
510 $cmd .= " >> $EXPORT_FILE;";
512 $cmd .= $dumppfx .
513 " --where=\"form_id = '$layoutid' ORDER BY group_id, seq, title\" " .
514 escapeshellarg($sqlconf["dbase"]) . " layout_options" ;
515 if (IS_WINDOWS) {
516 # windows uses the & to join statements.
517 $cmd .= " >> $EXPORT_FILE & ";
518 } else {
519 $cmd .= " >> $EXPORT_FILE;";
523 } else {
524 echo xl('No items were selected!');
525 $form_step = -1;
528 $auto_continue = true;
531 if ($form_step == 103) {
532 $form_status .= xl('Done. Will now send download.') . "<br />";
533 echo nl2br($form_status);
534 $auto_continue = true;
537 if ($form_step == 201) {
538 echo xl('WARNING: This will overwrite configuration information with data from the uploaded file!') . " \n";
539 echo xl('Use this feature only with newly installed sites, ');
540 echo xl('otherwise you will destroy references to/from existing data.') . "\n";
541 echo "<br />&nbsp;<br />\n";
542 echo xl('File to upload') . ":\n";
543 echo "<input type='hidden' name='MAX_FILE_SIZE' value='4000000' />\n";
544 echo "<input type='file' name='userfile' /><br />&nbsp;<br />\n";
545 echo "<input type='submit' value='" . xl('Continue') . "' />\n";
548 if ($form_step == 202) {
549 // Process uploaded config file.
550 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
551 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $EXPORT_FILE)) {
552 $form_status .= xl('Applying') . "...<br />";
553 echo nl2br($form_status);
554 $cmd = "$mysql_cmd -u " . escapeshellarg($sqlconf["login"]) .
555 " -p" . escapeshellarg($sqlconf["pass"]) .
556 " -h " . escapeshellarg($sqlconf["host"]) .
557 " --port=".escapeshellarg($sqlconf["port"]) .
558 " $mysql_ssl " .
559 escapeshellarg($sqlconf["dbase"]) .
560 " < $EXPORT_FILE";
561 } else {
562 echo xl('Internal error accessing uploaded file!');
563 $form_step = -1;
565 } else {
566 echo xl('Upload failed!');
567 $form_step = -1;
570 $auto_continue = true;
573 if ($form_step == 203) {
574 $form_status .= xl('Done') . ".";
575 echo nl2br($form_status);
578 /// ViSolve : EventLog Backup
579 if ($form_step == 301) {
580 # Get the Current Timestamp, to attach with the log backup file
581 $backuptime=date("Ymd_His");
582 # Eventlog backup directory
583 $BACKUP_EVENTLOG_DIR = $GLOBALS['backup_log_dir'] . "/emr_eventlog_backup";
585 # Check if Eventlog Backup directory exists, if not create it with Write permission
586 if (!file_exists($BACKUP_EVENTLOG_DIR)) {
587 mkdir($BACKUP_EVENTLOG_DIR);
588 chmod($BACKUP_EVENTLOG_DIR, 0777);
591 # Frame the Eventlog Backup File Name
592 $BACKUP_EVENTLOG_FILE=$BACKUP_EVENTLOG_DIR.'/eventlog_'.$backuptime.'.sql';
593 # Create a new table similar to event table, rename the existing table as backup table, and rename the new table to event log table. Then export the contents of the table into a text file and drop the table.
594 $res=sqlStatement("create table if not exists log_comment_encrypt_new like log_comment_encrypt");
595 $res=sqlStatement("rename table log_comment_encrypt to log_comment_encrypt_backup,log_comment_encrypt_new to log_comment_encrypt");
596 $res=sqlStatement("create table if not exists log_new like log");
597 $res=sqlStatement("rename table log to log_backup,log_new to log");
598 $res=sqlStatement("create table if not exists log_validator_new like log_validator");
599 $res=sqlStatement("rename table log_validator to log_validator_backup, log_validator_new to log_validator");
600 echo "<br>";
601 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
602 " -p" . escapeshellarg($sqlconf["pass"]) .
603 " -h " . escapeshellarg($sqlconf["host"]) .
604 " --port=".escapeshellarg($sqlconf["port"]) .
605 " --opt --quote-names -r $BACKUP_EVENTLOG_FILE $mysql_ssl " .
606 escapeshellarg($sqlconf["dbase"]) ." --tables log_comment_encrypt_backup log_backup log_validator_backup";
607 # Set Eventlog Flag when it is done
608 $eventlog=1;
609 // 301 If ends here.
612 ++$form_step;
615 </td>
616 </tr>
617 </table>
619 <input type='hidden' name='form_step' value='<?php echo $form_step; ?>' />
620 <input type='hidden' name='form_status' value='<?php echo $form_status; ?>' />
622 </form>
624 <?php
625 ob_flush();
626 flush();
627 if ($cmd) {
628 $tmp0 = exec($cmd, $tmp1, $tmp2);
630 if ($tmp2) {
631 if ($eventlog==1) {
632 // ViSolve : Restore previous state, if backup fails.
633 $res=sqlStatement("drop table if exists log_comment_encrypt");
634 $res=sqlStatement("rename table log_comment_encrypt_backup to log_comment_encrypt");
635 $res=sqlStatement("drop table if exists log");
636 $res=sqlStatement("rename table log_backup to log");
637 $res=sqlStatement("drop table if exists log_validator");
638 $res=sqlStatement("rename table log_validator_backup to log_validator");
641 die("\"$cmd\" returned $tmp2: $tmp0");
644 // ViSolve: If the Eventlog is set, then clear the temporary table -- Start here
645 if ($eventlog==1) {
646 $res=sqlStatement("drop table if exists log_backup");
647 $res=sqlStatement("drop table if exists log_comment_encrypt_backup");
648 $res=sqlStatement("drop table if exists log_validator_backup");
649 echo "<br><b>";
650 echo xl('Backup Successfully taken in')." ";
651 echo $BACKUP_EVENTLOG_DIR;
652 echo "</b>";
655 // ViSolve: If the Eventlog is set, then clear the temporary table -- Ends here
658 // If a file was flagged to be gzip-compressed after this cmd, do it.
659 if ($file_to_compress) {
660 if (!gz_compress_file($file_to_compress)) {
661 die(xl("Error in gzip compression of file: ") . $file_to_compress);
666 </center>
668 <?php if ($auto_continue) { ?>
669 <script language="JavaScript">
670 setTimeout("document.forms[0].submit();", 500);
671 </script>
672 <?php }
674 // Recursive directory remove (like an O/S insensitive "rm -rf dirname")
675 function obliterate_dir($dir)
677 if (!file_exists($dir)) {
678 return true;
681 if (!is_dir($dir) || is_link($dir)) {
682 return unlink($dir);
685 foreach (scandir($dir) as $item) {
686 if ($item == '.' || $item == '..') {
687 continue;
690 if (!obliterate_dir($dir . DIRECTORY_SEPARATOR . $item)) {
691 chmod($dir . DIRECTORY_SEPARATOR . $item, 0777);
692 if (!obliterate_dir($dir . DIRECTORY_SEPARATOR . $item)) {
693 return false;
698 return rmdir($dir);
701 // Create a tar archive given the archive file name, compression method if any, and the
702 // array of file/directory names to archive
703 function create_tar_archive($archiveName, $compressMethod, $itemArray)
705 global $newBackupMethod;
707 if ($newBackupMethod) {
708 // Create a tar object using the pear library
709 // (this is the preferred method)
710 $tar = new Archive_Tar($archiveName, $compressMethod);
711 if ($tar->create($itemArray)) {
712 return true;
714 } else {
715 // Create the tar files via command line tools
716 // (this method used when the tar pear library is not available)
717 $files = '"' . implode('" "', $itemArray) . '"';
718 if ($compressMethod == "gz") {
719 $command = "tar --same-owner --ignore-failed-read -zcphf $archiveName $files";
720 } else {
721 $command = "tar -cpf $archiveName $files";
724 $temp0 = exec($command, $temp1, $temp2);
725 if ($temp2) {
726 die("\"$command\" returned $temp2: $temp0");
729 return true;
732 return false;
735 // Compress a file using gzip. Source file removed, leaving only the compressed
736 // *.gz file, just like gzip command line would behave.
737 function gz_compress_file($source)
739 $dest=$source.'.gz';
740 $error=false;
741 if ($fp_in=fopen($source, 'rb')) {
742 if ($fp_out=gzopen($dest, 'wb')) {
743 while (!feof($fp_in)) {
744 gzwrite($fp_out, fread($fp_in, 1024*512));
747 gzclose($fp_out);
748 fclose($fp_in);
749 unlink($source);
750 } else {
751 $error=true;
753 } else {
754 $error=true;
757 if ($error) {
758 return false;
759 } else {
760 return $dest;
765 </body>
766 </html>