Fixed bug: https://sourceforge.net/p/openemr/bugs/416/
[openemr.git] / interface / main / backup.php
blob9fa543dd875457814bdc800216bf6797c4c245e5
1 <?php
2 /* $Id$ */
3 // Copyright (C) 2008-2014 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 // * a SQL-Ledger database dump (gzipped), if SQL-Ledger is used
18 // (currently skipped on Windows servers)
19 // * the OpenEMR web directory (.tar.gz)
20 // * the phpGACL web directory (.tar.gz), if phpGACL is used
21 // * the SQL-Ledger web directory (.tar.gz), if SQL-Ledger is used
22 // and its web directory exists as a sister of the openemr directory
23 // and has the name "sql-ledger" (otherwise we do not have enough
24 // information to find it)
26 // The OpenEMR web directory is important because it includes config-
27 // uration files, patient documents, and possible customizations, and
28 // also because the database structure is dependent on the installed
29 // OpenEMR version.
31 // This script depends on execution of some external programs:
32 // mysqldump & pg_dump. It has been tested with Debian and Ubuntu
33 // Linux and with Windows XP.
34 // Do not assume that it works for you until you have successfully
35 // tested a restore!
36 set_time_limit(0);
37 require_once("../globals.php");
38 require_once("$srcdir/acl.inc");
39 require_once("$srcdir/log.inc");
41 if (!extension_loaded('zlib'))
43 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
45 if (!function_exists('gzopen') && function_exists('gzopen64'))
47 function gzopen($filename, $mode, $use_include_path = 0)
49 return gzopen64($filename, $mode, $use_include_path);
54 if (!acl_check('admin', 'super')) die(xl('Not authorized','','','!'));
56 include_once("Archive/Tar.php");
58 // Set up method, which will depend on OS and if pear tar.php is installed
59 if (class_exists('Archive_Tar')) {
60 # pear tar.php is installed so can use os independent method
61 $newBackupMethod = true;
63 elseif (IS_WINDOWS) {
64 # without the tar.php module, can't run backup in windows
65 die(xl("Error. You need to install the Archive/Tar.php php module."));
67 else {
68 # without the tar.php module, can run via system commands in non-windows
69 $newBackupMethod = false;
72 $BTN_TEXT_CREATE = xl('Create Backup');
73 $BTN_TEXT_EXPORT = xl('Export Configuration');
74 $BTN_TEXT_IMPORT = xl('Import Configuration');
75 // ViSolve: Create Log Backup button
76 $BTN_TEXT_CREATE_EVENTLOG = xl('Create Eventlog Backup');
78 $form_step = isset($_POST['form_step']) ? trim($_POST['form_step']) : '0';
79 $form_status = isset($_POST['form_status' ]) ? trim($_POST['form_status' ]) : '';
81 if (!empty($_POST['form_export'])) $form_step = 101;
82 if (!empty($_POST['form_import'])) $form_step = 201;
83 //ViSolve: Assign Unique Number for the Log Creation
84 if (!empty($_POST['form_backup'])) $form_step = 301;
85 // When true the current form will submit itself after a brief pause.
86 $auto_continue = false;
88 # set up main paths
89 $backup_file_prefix = "emr_backup";
90 $backup_file_suffix = ".tar";
91 $TMP_BASE = $GLOBALS['temporary_files_dir'] . "/openemr_web_backup";
92 $BACKUP_DIR = $TMP_BASE . "/emr_backup";
93 $TAR_FILE_PATH = $TMP_BASE . DIRECTORY_SEPARATOR . $backup_file_prefix . $backup_file_suffix;
94 $EXPORT_FILE = $GLOBALS['temporary_files_dir'] . "/openemr_config.sql";
95 $MYSQL_PATH = $GLOBALS['mysql_bin_dir'];
96 $PERL_PATH = $GLOBALS['perl_bin_dir'];
98 if ($form_step == 8) {
99 header("Pragma: public");
100 header("Expires: 0");
101 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102 header("Content-Type: application/force-download");
103 header("Content-Length: " . filesize($TAR_FILE_PATH));
104 header("Content-Disposition: attachment; filename=" . basename($TAR_FILE_PATH));
105 header("Content-Description: File Transfer");
106 readfile($TAR_FILE_PATH);
107 unlink($TAR_FILE_PATH);
108 obliterate_dir($BACKUP_DIR);
109 exit(0);
112 if ($form_step == 104) {
113 header("Pragma: public");
114 header("Expires: 0");
115 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
116 header("Content-Type: application/force-download");
117 header("Content-Length: " . filesize($EXPORT_FILE));
118 header("Content-Disposition: attachment; filename=" . basename($EXPORT_FILE));
119 header("Content-Description: File Transfer");
120 readfile($EXPORT_FILE);
121 unlink($EXPORT_FILE);
122 exit(0);
125 <html>
127 <head>
128 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
129 <title><?php xl('Backup','e'); ?></title>
130 </head>
132 <body class="body_top">
133 <center>
134 &nbsp;<br />
135 <form method='post' action='backup.php' enctype='multipart/form-data'>
137 <table<?php if ($form_step != 101) echo " style='width:50em'"; ?>>
138 <tr>
139 <td>
141 <?php
142 $cmd = '';
143 $mysql_cmd = $MYSQL_PATH . DIRECTORY_SEPARATOR . 'mysql';
144 $mysql_dump_cmd = $mysql_cmd . 'dump';
145 $file_to_compress = ''; // if named, this iteration's file will be gzipped after it is created
146 $eventlog=0; // Eventlog Flag
148 if ($form_step == 0) {
149 echo "<table>\n";
150 echo " <tr>\n";
151 echo " <td><input type='submit' name='form_create' value='$BTN_TEXT_CREATE' /></td>\n";
152 echo " <td>" . xl('Create and download a full backup') . "</td>\n";
153 echo " </tr>\n";
154 // The config import/export feature is optional.
155 if (!empty($GLOBALS['configuration_import_export'])) {
156 echo " <tr>\n";
157 echo " <td><input type='submit' name='form_export' value='$BTN_TEXT_EXPORT' /></td>\n";
158 echo " <td>" . xl('Download configuration data') . "</td>\n";
159 echo " </tr>\n";
160 echo " <tr>\n";
161 echo " <td><input type='submit' name='form_import' value='$BTN_TEXT_IMPORT' /></td>\n";
162 echo " <td>" . xl('Upload configuration data') . "</td>\n";
163 echo " </tr>\n";
165 // ViSolve : Add ' Create Log table backup Button'
166 echo " <tr>\n";
167 echo " <td><input type='submit' name='form_backup' value='$BTN_TEXT_CREATE_EVENTLOG' /></td>\n";
168 echo " <td>" . xl('Create Eventlog Backup') . "</td>\n";
169 echo " </tr>\n";
170 echo " <tr>\n";
171 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";
172 echo " </tr>\n";
173 echo "</table>\n";
176 if ($form_step == 1) {
177 $form_status .= xl('Dumping OpenEMR database') . "...<br />";
178 echo nl2br($form_status);
179 if (file_exists($TAR_FILE_PATH))
180 if (! unlink($TAR_FILE_PATH)) die(xl("Couldn't remove old backup file:") . " " . $TAR_FILE_PATH);
181 if (! obliterate_dir($TMP_BASE)) die(xl("Couldn't remove dir:"). " " . $TMP_BASE);
182 if (! mkdir($BACKUP_DIR, 0777, true)) die(xl("Couldn't create backup dir:") . " " . $BACKUP_DIR);
183 $file_to_compress = "$BACKUP_DIR/openemr.sql"; // gzip this file after creation
185 if($GLOBALS['include_de_identification']==1)
187 //include routines during backup when de-identification is enabled
188 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
189 " -p" . escapeshellarg($sqlconf["pass"]) .
190 " -h" . escapeshellarg($sqlconf["host"]) .
191 " --port=".escapeshellarg($sqlconf["port"]) .
192 " --routines".
193 " --opt --quote-names -r $file_to_compress " .
194 escapeshellarg($sqlconf["dbase"]);
196 else
198 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
199 " -p" . escapeshellarg($sqlconf["pass"]) .
200 " -h" . escapeshellarg($sqlconf["host"]) .
201 " --port=".escapeshellarg($sqlconf["port"]) .
202 " --opt --quote-names -r $file_to_compress " .
203 escapeshellarg($sqlconf["dbase"]);
205 $auto_continue = true;
208 if ($form_step == 2) {
209 if (!empty($phpgacl_location) && $gacl_object->_db_name != $sqlconf["dbase"]) {
210 $form_status .= xl('Dumping phpGACL database') . "...<br />";
211 echo nl2br($form_status);
212 $file_to_compress = "$BACKUP_DIR/phpgacl.sql"; // gzip this file after creation
213 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($gacl_object->_db_user) .
214 " -p" . escapeshellarg($gacl_object->_db_password) .
215 " --opt --quote-names -r $file_to_compress " .
216 escapeshellarg($gacl_object->_db_name);
217 $auto_continue = true;
219 else {
220 ++$form_step;
224 if ($form_step == 3) {
225 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] &&
226 $GLOBALS['oer_config']['ws_accounting']['enabled'] !== 2) {
227 if (IS_WINDOWS) {
228 // Somebody may want to make this work in Windows, if they have SQL-Ledger set up.
229 $form_status .= xl('Skipping SQL-Ledger dump - not implemented for Windows server') . "...<br />";
230 echo nl2br($form_status);
231 ++$form_step;
233 else {
234 $form_status .= xl('Dumping SQL-Ledger database') . "...<br />";
235 echo nl2br($form_status);
236 $file_to_compress = "$BACKUP_DIR/sql-ledger.sql"; // gzip this file after creation
237 $cmd = "PGPASSWORD=" . escapeshellarg($sl_dbpass) . " pg_dump -U " .
238 escapeshellarg($sl_dbuser) . " -h localhost --format=c -f " .
239 "$file_to_compress " . escapeshellarg($sl_dbname);
240 $auto_continue = true;
243 else {
244 ++$form_step;
248 if ($form_step == 4) {
249 $form_status .= xl('Dumping OpenEMR web directory tree') . "...<br />";
250 echo nl2br($form_status);
251 $cur_dir = getcwd();
252 chdir($webserver_root);
254 // Select the files and directories to archive. Basically everything
255 // except site-specific data for other sites.
256 $file_list = array();
257 $dh = opendir($webserver_root);
258 if (!$dh) die("Cannot read directory '$webserver_root'.");
259 while (false !== ($filename = readdir($dh))) {
260 if ($filename == '.' || $filename == '..') continue;
261 if ($filename == 'sites') {
262 // Omit other sites.
263 $file_list[] = "$filename/" . $_SESSION['site_id'];
265 else {
266 $file_list[] = $filename;
269 closedir($dh);
271 $arch_file = $BACKUP_DIR . DIRECTORY_SEPARATOR . "openemr.tar.gz";
272 if (!create_tar_archive($arch_file, "gz", $file_list))
273 die(xl("An error occurred while dumping OpenEMR web directory tree"));
274 chdir($cur_dir);
275 $auto_continue = true;
278 if ($form_step == 5) {
279 if ((!empty($phpgacl_location)) && ($phpgacl_location != $srcdir."/../gacl") ) {
280 $form_status .= xl('Dumping phpGACL web directory tree') . "...<br />";
281 echo nl2br($form_status);
282 $cur_dir = getcwd();
283 chdir($phpgacl_location);
284 $file_list = array('.'); // archive entire directory
285 $arch_file = $BACKUP_DIR . DIRECTORY_SEPARATOR . "phpgacl.tar.gz";
286 if (!create_tar_archive($arch_file, "gz", $file_list))
287 die (xl("An error occurred while dumping phpGACL web directory tree"));
288 chdir($cur_dir);
289 $auto_continue = true;
291 else {
292 ++$form_step;
296 if ($form_step == 6) {
297 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] &&
298 $GLOBALS['oer_config']['ws_accounting']['enabled'] !== 2 &&
299 is_dir("$webserver_root/../sql-ledger"))
301 $form_status .= xl('Dumping SQL-Ledger web directory tree') . "...<br />";
302 echo nl2br($form_status);
303 $cur_dir = getcwd();
304 $arch_dir = $webserver_root . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "sql-ledger";
305 chdir($arch_dir);
306 $file_list = array('.'); // archive entire directory
307 $arch_file = $BACKUP_DIR . DIRECTORY_SEPARATOR . "sql-ledger.tar.gz";
308 if (!create_tar_archive($arch_file, "gz", $file_list))
309 die(xl("An error occurred while dumping SQL-Ledger web directory tree"));
310 chdir($cur_dir);
311 $auto_continue = true;
313 else {
314 ++$form_step;
317 if ($form_step == 7) { // create the final compressed tar containing all files
318 $form_status .= xl('Backup file has been created. Will now send download.') . "<br />";
319 echo nl2br($form_status);
320 $cur_dir = getcwd();
321 chdir($BACKUP_DIR);
322 $file_list = array('.');
323 if (!create_tar_archive($TAR_FILE_PATH, '', $file_list))
324 die(xl("Error: Unable to create downloadable archive"));
325 chdir($cur_dir);
326 /* To log the backup event */
327 if ($GLOBALS['audit_events_backup']){
328 newEvent("backup", $_SESSION['authUser'], $_SESSION['authProvider'], 0,"Backup is completed");
330 $auto_continue = true;
333 if ($form_step == 101) {
334 echo "<p><b>&nbsp;" . xl('Select the configuration items to export') . ":</b></p>";
336 echo "<table cellspacing='10' cellpadding='0'>\n<tr>\n<td valign='top'>\n";
338 echo "<b>" . xlt('Tables') . "</b><br />\n";
339 echo "<input type='checkbox' name='form_cb_services' value='1' />\n";
340 echo " " . xl('Services') . "<br />\n";
341 echo "<input type='checkbox' name='form_cb_products' value='1' />\n";
342 echo " " . xl('Products') . "<br />\n";
343 echo "<input type='checkbox' name='form_cb_prices' value='1' />\n";
344 echo " " . xl('Prices') . "<br />\n";
345 echo "<input type='checkbox' name='form_cb_categories' value='1' />\n";
346 echo " " . xl('Document Categories') . "<br />\n";
347 echo "<input type='checkbox' name='form_cb_feesheet' value='1' />\n";
348 echo " " . xl('Fee Sheet Options') . "<br />\n";
349 echo "<input type='checkbox' name='form_cb_lang' value='1' />\n";
350 echo " " . xl('Translations') . "<br />\n";
352 // Multi-select for lists.
353 echo "</td><td valign='top'>\n";
354 echo "<b>" . xlt('Lists') . "</b><br />\n";
355 echo "<select multiple name='form_sel_lists[]' size='15'>";
356 $lres = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
357 "list_id = 'lists' ORDER BY title, seq");
358 while ($lrow = sqlFetchArray($lres)) {
359 echo "<option value='" . attr($lrow['option_id']) . "'";
360 echo ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
362 echo "</select>\n";
364 // Multi-select for layouts.
365 echo "</td><td valign='top'>\n";
366 echo "<b>" . xlt('Layouts') . "</b><br />\n";
367 echo "<select multiple name='form_sel_layouts[]' size='15'>";
368 $lres = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
369 "list_id = 'lbfnames' ORDER BY title, seq");
370 while ($lrow = sqlFetchArray($lres)) {
371 echo "<option value='" . attr($lrow['option_id']) . "'";
372 echo ">" . text(xl_layout_label($lrow['title'])) . "</option>\n";
374 echo "</select>\n";
376 echo "</td>\n</tr>\n</table>\n";
377 echo "&nbsp;<br /><input type='submit' value='" . xl('Continue') . "' />\n";
380 if ($form_step == 102)
382 $tables = '';
383 if ($_POST['form_cb_services' ]) $tables .= ' codes';
384 if ($_POST['form_cb_products' ]) $tables .= ' drugs drug_templates';
385 if ($_POST['form_cb_prices' ]) $tables .= ' prices';
386 if ($_POST['form_cb_categories']) $tables .= ' categories categories_seq';
387 if ($_POST['form_cb_feesheet' ]) $tables .= ' fee_sheet_options';
388 if ($_POST['form_cb_lang' ]) $tables .= ' lang_languages lang_constants lang_definitions';
389 if ($tables || is_array($_POST['form_sel_lists']) || is_array($_POST['form_sel_layouts']))
391 $form_status .= xl('Creating export file') . "...<br />";
392 echo nl2br($form_status);
393 if (file_exists($EXPORT_FILE))
395 if (! unlink($EXPORT_FILE)) die(xl("Couldn't remove old export file: ") . $EXPORT_FILE);
397 // The substitutions below use perl because sed's not usually on windows systems.
398 $perl = $PERL_PATH . DIRECTORY_SEPARATOR . 'perl';
401 # This condition was added because the windows operating system uses different syntax for the shell commands.
402 # The test is if it is the windows operating system.
403 if (IS_WINDOWS)
405 # This section sets the character_set_client to utf8 in the sql file as part or the import property.
406 # windows will place the quotes in the outputted code if they are there. we removed them here.
407 $cmd = "echo SET character_set_client = utf8; > $EXPORT_FILE & ";
409 else
411 $cmd = "echo 'SET character_set_client = utf8;' > $EXPORT_FILE;";
413 if ($tables)
415 $cmd .= "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
416 " -p" . escapeshellarg($sqlconf["pass"]) .
417 " --opt --quote-names " .
418 escapeshellarg($sqlconf["dbase"]) . " $tables";
419 if (IS_WINDOWS)
421 # The Perl script differs in windows also.
422 $cmd .= " | $perl -pe \"s/ DEFAULT CHARSET=utf8//i; s/ collate[ =][^ ;,]*//i;\"" .
423 " >> $EXPORT_FILE & ";
425 else
427 $cmd .= " | $perl -pe 's/ DEFAULT CHARSET=utf8//i; s/ collate[ =][^ ;,]*//i;'" .
428 " > $EXPORT_FILE;";
431 $dumppfx = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
432 " -p" . escapeshellarg($sqlconf["pass"]) .
433 " --skip-opt --quote-names --complete-insert --no-create-info";
434 // Individual lists.
435 if (is_array($_POST['form_sel_lists']))
437 foreach ($_POST['form_sel_lists'] as $listid)
439 if (IS_WINDOWS)
441 # windows will place the quotes in the outputted code if they are there. we removed them here.
442 $cmd .= " echo DELETE FROM list_options WHERE list_id = '$listid'; >> $EXPORT_FILE & ";
443 $cmd .= " echo DELETE FROM list_options WHERE list_id = 'lists' AND option_id = '$listid'; >> $EXPORT_FILE & ";
445 else
447 $cmd .= "echo \"DELETE FROM list_options WHERE list_id = '$listid';\" >> $EXPORT_FILE;";
448 $cmd .= "echo \"DELETE FROM list_options WHERE list_id = 'lists' AND option_id = '$listid';\" >> $EXPORT_FILE;";
450 $cmd .= $dumppfx .
451 " --where=\"list_id = 'lists' AND option_id = '$listid' OR list_id = '$listid'\" " .
452 escapeshellarg($sqlconf["dbase"]) . " list_options";
453 if (IS_WINDOWS)
455 # windows uses the & to join statements.
456 $cmd .= " >> $EXPORT_FILE & ";
458 else
460 $cmd .= " >> $EXPORT_FILE;";
464 // Individual layouts.
465 if (is_array($_POST['form_sel_layouts']))
467 foreach ($_POST['form_sel_layouts'] as $layoutid)
469 if (IS_WINDOWS)
471 # windows will place the quotes in the outputted code if they are there. we removed them here.
472 $cmd .= " echo DELETE FROM layout_options WHERE form_id = '$layoutid'; >> $EXPORT_FILE & ";
474 else
476 $cmd .= "echo \"DELETE FROM layout_options WHERE form_id = '$layoutid';\" >> $EXPORT_FILE;";
478 if (strpos($layoutid, 'LBF') === 0)
480 if (IS_WINDOWS)
482 # windows will place the quotes in the outputted code if they are there. we removed them here.
483 $cmd .= " echo DELETE FROM list_options WHERE list_id = 'lbfnames' AND option_id = '$layoutid'; >> $EXPORT_FILE & ";
485 else
487 $cmd .= "echo \"DELETE FROM list_options WHERE list_id = 'lbfnames' AND option_id = '$layoutid';\" >> $EXPORT_FILE;";
489 $cmd .= $dumppfx .
490 " --where=\"list_id = 'lbfnames' AND option_id = '$layoutid'\" " .
491 escapeshellarg($sqlconf["dbase"]) . " list_options" ;
492 if (IS_WINDOWS)
494 # windows uses the & to join statements.
495 $cmd .= " >> $EXPORT_FILE & ";
497 else
499 $cmd .= " >> $EXPORT_FILE;";
502 $cmd .= $dumppfx .
503 " --where=\"form_id = '$layoutid'\" " .
504 escapeshellarg($sqlconf["dbase"]) . " layout_options" ;
505 if (IS_WINDOWS)
507 # windows uses the & to join statements.
508 $cmd .= " >> $EXPORT_FILE & ";
510 else
512 $cmd .= " >> $EXPORT_FILE;";
517 else
519 echo xl('No items were selected!');
520 $form_step = -1;
522 $auto_continue = true;
525 if ($form_step == 103) {
526 $form_status .= xl('Done. Will now send download.') . "<br />";
527 echo nl2br($form_status);
528 $auto_continue = true;
531 if ($form_step == 201) {
532 echo xl('WARNING: This will overwrite configuration information with data from the uploaded file!') . " \n";
533 echo xl('Use this feature only with newly installed sites, ');
534 echo xl('otherwise you will destroy references to/from existing data.') . "\n";
535 echo "<br />&nbsp;<br />\n";
536 echo xl('File to upload') . ":\n";
537 echo "<input type='hidden' name='MAX_FILE_SIZE' value='4000000' />\n";
538 echo "<input type='file' name='userfile' /><br />&nbsp;<br />\n";
539 echo "<input type='submit' value='" . xl('Continue') . "' />\n";
542 if ($form_step == 202) {
543 // Process uploaded config file.
544 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
545 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $EXPORT_FILE)) {
546 $form_status .= xl('Applying') . "...<br />";
547 echo nl2br($form_status);
548 $cmd = "$mysql_cmd -u" . escapeshellarg($sqlconf["login"]) .
549 " -p" . escapeshellarg($sqlconf["pass"]) . " " .
550 escapeshellarg($sqlconf["dbase"]) .
551 " < $EXPORT_FILE";
553 else {
554 echo xl('Internal error accessing uploaded file!');
555 $form_step = -1;
558 else {
559 echo xl('Upload failed!');
560 $form_step = -1;
562 $auto_continue = true;
565 if ($form_step == 203) {
566 $form_status .= xl('Done') . ".";
567 echo nl2br($form_status);
570 /// ViSolve : EventLog Backup
571 if ($form_step == 301) {
572 # Get the Current Timestamp, to attach with the log backup file
573 $backuptime=date("Ymd_His");
574 # Eventlog backup directory
575 $BACKUP_EVENTLOG_DIR = $GLOBALS['backup_log_dir'] . "/emr_eventlog_backup";
577 # Check if Eventlog Backup directory exists, if not create it with Write permission
578 if (!file_exists($BACKUP_EVENTLOG_DIR))
580 mkdir($BACKUP_EVENTLOG_DIR);
581 chmod($BACKUP_EVENTLOG_DIR,0777);
583 # Frame the Eventlog Backup File Name
584 $BACKUP_EVENTLOG_FILE=$BACKUP_EVENTLOG_DIR.'/eventlog_'.$backuptime.'.sql';
585 # 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.
586 $res=sqlStatement("create table if not exists log_comment_encrypt_new like log_comment_encrypt");
587 $res=sqlStatement("rename table log_comment_encrypt to log_comment_encrypt_backup,log_comment_encrypt_new to log_comment_encrypt");
588 $res=sqlStatement("create table if not exists log_new like log");
589 $res=sqlStatement("rename table log to log_backup,log_new to log");
590 echo "<br>";
591 $cmd = "$mysql_dump_cmd -u " . escapeshellarg($sqlconf["login"]) .
592 " -p" . escapeshellarg($sqlconf["pass"]) .
593 " --opt --quote-names -r $BACKUP_EVENTLOG_FILE " .
594 escapeshellarg($sqlconf["dbase"]) ." --tables log_comment_encrypt_backup log_backup";
595 # Set Eventlog Flag when it is done
596 $eventlog=1;
597 // 301 If ends here.
600 ++$form_step;
603 </td>
604 </tr>
605 </table>
607 <input type='hidden' name='form_step' value='<?php echo $form_step; ?>' />
608 <input type='hidden' name='form_status' value='<?php echo $form_status; ?>' />
610 </form>
612 <?php
613 ob_flush();
614 flush();
615 if ($cmd) {
616 $tmp0 = exec($cmd, $tmp1, $tmp2);
618 if ($tmp2)
620 if ($eventlog==1)
622 // ViSolve : Restore previous state, if backup fails.
623 $res=sqlStatement("drop table if exists log_comment_encrypt");
624 $res=sqlStatement("rename table log_comment_encrypt_backup to log_comment_encrypt");
625 $res=sqlStatement("drop table if exists log");
626 $res=sqlStatement("rename table log_backup to log");
628 die("\"$cmd\" returned $tmp2: $tmp0");
630 // ViSolve: If the Eventlog is set, then clear the temporary table -- Start here
631 if ($eventlog==1) {
632 $res=sqlStatement("drop table if exists log_backup");
633 $res=sqlStatement("drop table if exists log_comment_encrypt_backup");
634 echo "<br><b>";
635 echo xl('Backup Successfully taken in')." ";
636 echo $BACKUP_EVENTLOG_DIR;
637 echo "</b>";
639 // ViSolve: If the Eventlog is set, then clear the temporary table -- Ends here
641 // If a file was flagged to be gzip-compressed after this cmd, do it.
642 if ($file_to_compress) {
643 if (!gz_compress_file($file_to_compress))
644 die (xl("Error in gzip compression of file: ") . $file_to_compress);
648 </center>
650 <?php if ($auto_continue) { ?>
651 <script language="JavaScript">
652 setTimeout("document.forms[0].submit();", 500);
653 </script>
654 <?php }
656 // Recursive directory remove (like an O/S insensitive "rm -rf dirname")
657 function obliterate_dir($dir) {
658 if (!file_exists($dir)) return true;
659 if (!is_dir($dir) || is_link($dir)) return unlink($dir);
660 foreach (scandir($dir) as $item) {
661 if ($item == '.' || $item == '..') continue;
662 if (!obliterate_dir($dir . DIRECTORY_SEPARATOR . $item)) {
663 chmod($dir . DIRECTORY_SEPARATOR . $item, 0777);
664 if (!obliterate_dir($dir . DIRECTORY_SEPARATOR . $item)) return false;
667 return rmdir($dir);
670 // Create a tar archive given the archive file name, compression method if any, and the
671 // array of file/directory names to archive
672 function create_tar_archive($archiveName, $compressMethod, $itemArray) {
673 global $newBackupMethod;
675 if ($newBackupMethod) {
676 // Create a tar object using the pear library
677 // (this is the preferred method)
678 $tar = new Archive_Tar($archiveName, $compressMethod);
679 if ($tar->create($itemArray)) return true;
681 else {
682 // Create the tar files via command line tools
683 // (this method used when the tar pear library is not available)
684 $files = '"' . implode('" "', $itemArray) . '"';
685 if ($compressMethod == "gz") {
686 $command = "tar --same-owner --ignore-failed-read -zcphf $archiveName $files";
688 else {
689 $command = "tar -cpf $archiveName $files";
691 $temp0 = exec($command, $temp1, $temp2);
692 if ($temp2) die("\"$command\" returned $temp2: $temp0");
693 return true;
695 return false;
698 // Compress a file using gzip. Source file removed, leaving only the compressed
699 // *.gz file, just like gzip command line would behave.
700 function gz_compress_file($source) {
701 $dest=$source.'.gz';
702 $error=false;
703 if ($fp_in=fopen($source,'rb')) {
704 if ($fp_out=gzopen($dest,'wb')) {
705 while(!feof($fp_in))
706 gzwrite($fp_out,fread($fp_in,1024*512));
707 gzclose($fp_out);
708 fclose($fp_in);
709 unlink($source);
711 else $error=true;
713 else $error=true;
714 if($error)
715 return false;
716 else
717 return $dest;
721 </body>
722 </html>