2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/includes/ui.inc");
17 include_once($path_to_root . "/admin/db/maintenance_db.inc");
19 $valid_paths = valid_paths();
21 if ($valid_paths != "")
23 page(_("Backup and Restore Database - Error"));
24 display_error (_("Backup paths have not been set correctly.") ." " . _("Please contact System Administrator.") . "<br>" .$valid_paths);
29 $db_name = $_SESSION["wa_current_user"]->company;
30 handle_form($db_connections[$db_name]);
32 page(_("Backup and Restore Database"), false, false, '', '', true);
34 //-------------------------------------------------------------------------------
35 start_form(true, true);
37 $cmb = get_backup_file_combo();
38 $compr = get_compr_combo();
41 <script language='javascript'>
42 function createBackup() {
44 ext = document.forms[0].cmb_comp.options[document.forms[0].cmb_comp.selectedIndex].value
45 comm = document.forms[0].comments.value
46 document.location.replace('backups.php?c=g&comp='+ext+'&comm='+comm)
48 function restoreBackup() {
50 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
51 document.location.replace('backups.php?c=r&fn='+pFilename)
53 function viewBackup() {
54 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
55 var ext = pFilename.substr(pFilename.lastIndexOf('.') + 1)
57 alert('" . _('This extension can not be be viewed: ') . "' + ext)
60 window.open('" . BACKUP_PATH . "'+pFilename, '', 'toolbar=no,scrollbars=yes')
62 function deleteBackup() {
63 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
64 if (!confirm('" . _("Are you sure you want to delete the backup file - ") . "'+pFilename+'?'))
66 document.location.replace('backups.php?c=df&fn='+pFilename)
68 function downloadBackup() {
69 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
70 document.location.replace('backups.php?c=ds&fn='+pFilename)
73 function uploadBackup() {
74 if (document.forms[0].uploadfile.value=='') {
75 alert('" . _("Please select a file to upload.") . "')
79 document.forms[0].action='backups.php?c=u&fn=' + document.forms[0].uploadfile.value
80 document.forms[0].submit()
84 <table cellpadding=2 cellspacing=2 border=0>
86 <td style='padding-right:30px'>" . _("Backup scripts") . "</td>
89 <td style='padding-right:30px'>$cmb</td>
90 <script language='javascript'>
91 if (document.forms[0].cmb_backups.options.length!=0) document.forms[0].cmb_backups.selectedIndex=0
94 <table height=160 cellpadding=0 cellspacing=0 border=0>
95 <tr><td><input onclick='javascript:createBackup()' type='button' style='width:150px' value='". _("Create Backup") . "'></td><td style='padding-left:20px'>" . _("Compression") . " $compr</td></tr>
96 <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150px' value='". _("Restore Backup") . "'></td><td> </td></tr>
97 <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150px' value='". _("View Backup") . "'></td><td> </td></tr>
98 <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150px' value='". _("Delete Backup") . "'></td><td> </td></tr>
99 <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150px' value='". _("Download Backup") . "'></td><td> </td></tr>
100 <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150px' value='". _("Upload Backup"). "'></td>
101 <td style='padding-left:20px'><input name='uploadfile' type='file'></td>
107 <td style='padding-right:30px'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
110 <td style='padding-right:30px'><textarea rows=4 cols=30 name='comments'></textarea></td>
116 //-------------------------------------------------------------------------------------------------
121 function handle_form($conn)
123 if(isset($_GET['c']))
126 case 'g': //Generate Only
127 $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
128 header("Location: backups.php?c=gs&fn=" . urlencode($filename));
131 case 'gd': //Generate and download
132 $filename = generate_backup($conn);
133 header("Location: backups.php?c=ds&fn=" . urlencode($filename));
136 case 'd': //Download the file
137 download_file(BACKUP_PATH . $_GET['fn']);
140 case 'df': //Delete the file
141 $filename = $_GET['fn'];
142 @unlink(BACKUP_PATH . $filename);
143 header("Location: backups.php?c=dff&fn=" . urlencode($filename));
147 $msg = _("File successfully deleted.")." ";
148 $msg .= _("Filename") . " = " . $_GET['fn'];
149 display_notification($msg);
152 case 'ds': //Write JS script to open download window
153 $filename = urlencode($_GET['fn']);
154 display_notification(_("Backup is being downloaded..."));
156 add_js_source("<script language='javascript'>
157 function download_file() {location.href ='backups.php?c=d&fn=$filename'};
158 Behaviour.addLoadEvent(download_file);
162 case 'gs': //Print backup success message
163 $msg = _("Backup successfully generated."). ' ';
164 $msg .= _("Filename") . " = " . $_GET['fn'];
165 display_notification($msg);
168 case 'r': //Restore backup
169 $filename=$_GET['fn'];
170 if( restore_backup(BACKUP_PATH . $filename, $conn) )
171 header("Location: backups.php?c=rs&fn=" . urlencode($filename));
174 case 'rs': //Print restore success message
175 display_notification(_("Restore backup completed."));
179 $filename = $_FILES['uploadfile']['tmp_name'];
180 if (is_uploaded_file ($filename))
182 if( restore_backup($filename, $conn) )
183 display_notification(_("Uploaded file has been restored."));
185 display_error(_("Database restore failed."));
189 display_error(_("Backup was not uploaded into the system."));
194 function generate_backup($conn, $ext='no', $comm='')
196 if ($conn['tbpref'] != "")
197 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
199 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
201 $filename = db_export($conn, $filename, $ext, $comm);
206 function restore_backup($filename, $conn)
208 return db_import($filename, $conn);
211 function get_backup_file_combo()
213 global $path_to_root;
215 default_focus('cmb_backups');
216 $dh = opendir(BACKUP_PATH);
217 while (($file = readdir($dh)) !== false)
223 foreach ($ar_files as $file)
224 if (strpos($file, ".sql") || strpos($file, ".sql"))
225 $opt_files .= "<option value='$file'>$file</option>";
227 return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
230 function get_compr_combo()
234 $ar_comps[] = _("No");
235 if (function_exists("gzcompress"))
237 if (function_exists("gzopen"))
238 $ar_comps[] = "gzip";
240 foreach ($ar_comps as $file)
241 $opt_comps .= "<option value='$file'>$file</option>";
243 return "<select name='cmb_comp'>$opt_comps</select>";
246 function download_file($filename)
248 if (empty($filename) || !file_exists($filename))
252 $saveasname = basename($filename);
253 header('Content-type: application/octet-stream');
254 header('Content-Length: '.filesize($filename));
255 header('Content-Disposition: attachment; filename="'.$saveasname.'"');
260 function valid_paths()
262 global $path_to_root;
265 if (!file_exists(BACKUP_PATH))
266 $st .= " - " . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";