Apparently the mysqldump was not acceptable for import.
[openemr.git] / interface / main / backup.php
bloba09d9435cfe0f2e284d3bb3860f62c3b456ca503
1 <?php
2 // Copyright (C) 2008 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This script creates a backup tarball and sends it to the users's
10 // browser for download. The tarball includes:
12 // * an OpenEMR database dump
13 // * a phpGACL database dump, if phpGACL is used and has its own
14 // database
15 // * a SQL-Ledger database dump, if SQL-Ledger is used
16 // * the OpenEMR web directory
17 // * the phpGACL web directory, if phpGACL is used
18 // * the SQL-Ledger web directory, if SQL-Ledger is used and its
19 // web directory exists as a sister of the openemr directory and
20 // has the name "sql-ledger" (otherwise we do not have enough
21 // information to find it)
23 // The OpenEMR web directory is important because it includes config-
24 // uration files, patient documents, and possible customizations, and
25 // also because the database structure is dependent on the installed
26 // OpenEMR version.
28 // This script depends on execution of some external programs:
29 // rm, mkdir, mysqldump, pg_dump, tar, gzip. It has been tested with
30 // Debian and Ubuntu Linux. Currently it will not work with Windows.
31 // Do not assume that it works for you until you have successfully
32 // tested a restore!
34 require_once("../globals.php");
35 require_once("$srcdir/acl.inc");
37 if (!acl_check('admin', 'super')) die("Not authorized!");
39 $form_step = isset($_POST['form_step']) ? trim($_POST['form_step']) : '0';
40 $form_status = isset($_POST['form_status' ]) ? trim($_POST['form_status' ]) : '';
42 $TMP_BASE = "/tmp/openemr_web_backup";
43 $BACKUP_DIR = "$TMP_BASE/emr_backup";
44 $TAR_FILE_PATH = "$TMP_BASE/emr_backup.tar";
46 if ($form_step > 7) {
47 header("Pragma: public");
48 header("Expires: 0");
49 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
50 header("Content-Type: application/force-download");
51 header("Content-Length: " . filesize($TAR_FILE_PATH));
52 header("Content-Disposition: attachment; filename=" . basename($TAR_FILE_PATH));
53 header("Content-Description: File Transfer");
54 readfile($TAR_FILE_PATH);
55 exit(0);
58 <html>
60 <head>
61 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
62 <title><?php xl('Backup','e'); ?></title>
63 </head>
65 <body class="body_top">
66 <center>
67 &nbsp;<br />
68 <form method='post' action='backup.php'>
70 <table style='width:30em'>
71 <tr>
72 <td>
74 <?php
75 $cmd = '';
77 if ($form_step == 0) {
78 echo "This will create a backup in tar format and then send it to your " .
79 "web browser so you can save it. Press Continue to proceed.<br />\n";
80 echo "&nbsp;<br /><center><input type='submit' value='Continue' /></center>\n";
82 if ($form_step == 1) {
83 $form_status .= "Dumping OpenEMR database ...<br />";
84 echo nl2br($form_status);
85 $cmd = "rm -rf $TMP_BASE; mkdir -p $BACKUP_DIR; " .
86 "mysqldump -u " . escapeshellarg($sqlconf["login"]) .
87 " -p" . escapeshellarg($sqlconf["pass"]) .
88 " --opt --quote-names -r $BACKUP_DIR/openemr.sql " .
89 escapeshellarg($sqlconf["dbase"]) .
90 "; gzip $BACKUP_DIR/openemr.sql";
92 if ($form_step == 2) {
93 if (!empty($phpgacl_location) && $gacl_object->_db_name != $sqlconf["dbase"]) {
94 $form_status .= "Dumping phpGACL database ...<br />";
95 echo nl2br($form_status);
96 $cmd = "mysqldump -u " . escapeshellarg($gacl_object->_db_user) .
97 " -p" . escapeshellarg($gacl_object->_db_password) .
98 " --opt --quote-names -r $BACKUP_DIR/phpgacl.sql " .
99 escapeshellarg($gacl_object->_db_name) .
100 "; gzip $BACKUP_DIR/phpgacl.sql";
102 else {
103 ++$form_step;
106 if ($form_step == 3) {
107 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] &&
108 $GLOBALS['oer_config']['ws_accounting']['enabled'] !== 2)
110 $form_status .= "Dumping SQL-Ledger database ...<br />";
111 echo nl2br($form_status);
112 $cmd = "PGPASSWORD=" . escapeshellarg($sl_dbpass) . " pg_dump -U " .
113 escapeshellarg($sl_dbuser) . " -h localhost --format=c -f " .
114 "$BACKUP_DIR/sql-ledger.sql " . escapeshellarg($sl_dbname);
116 else {
117 ++$form_step;
120 if ($form_step == 4) {
121 $form_status .= "Dumping OpenEMR web directory tree ...<br />";
122 echo nl2br($form_status);
123 $cmd = "cd $webserver_root; tar --same-owner --ignore-failed-read -zcphf $BACKUP_DIR/openemr.tar.gz .";
125 if ($form_step == 5) {
126 if ((!empty($phpgacl_location)) && ($phpgacl_location != $GLOBALS['fileroot']."/gacl")) {
127 $form_status .= "Dumping phpGACL web directory tree ...<br />";
128 echo nl2br($form_status);
129 $cmd = "cd $phpgacl_location; tar --same-owner --ignore-failed-read -zcphf $BACKUP_DIR/phpgacl.tar.gz .";
131 else {
132 ++$form_step;
135 if ($form_step == 6) {
136 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] &&
137 $GLOBALS['oer_config']['ws_accounting']['enabled'] !== 2 &&
138 is_dir("$webserver_root/../sql-ledger"))
140 $form_status .= "Dumping SQL-Ledger web directory tree ...<br />";
141 echo nl2br($form_status);
142 $cmd = "cd $webserver_root/../sql-ledger; tar --same-owner --ignore-failed-read -zcphf $BACKUP_DIR/sql-ledger.tar.gz .";
144 else {
145 ++$form_step;
148 if ($form_step == 7) {
149 $form_status .= "Backup file has been created. Will now send download.<br />";
150 echo nl2br($form_status);
151 $cmd = "cd $BACKUP_DIR; tar -cpf $TAR_FILE_PATH .";
154 ++$form_step;
157 </td>
158 </tr>
159 </table>
161 <input type='hidden' name='form_step' value='<?php echo $form_step; ?>' />
162 <input type='hidden' name='form_status' value='<?php echo $form_status; ?>' />
164 </form>
166 <?php
167 ob_flush();
168 flush();
169 if ($cmd) {
170 $tmp0 = exec($cmd, $tmp1, $tmp2);
171 if ($tmp2) die("\"$cmd\" returned $tmp2: $tmp0");
175 </center>
177 <?php if ($form_step > 1) { ?>
178 <script language="JavaScript">
179 setTimeout("document.forms[0].submit();", 500);
180 </script>
181 <?php } ?>
183 </body>
184 </html>