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() {
43 ext = document.forms[0].cmb_comp.options[document.forms[0].cmb_comp.selectedIndex].value
44 comm = document.forms[0].comments.value
45 document.location.replace('backups.php?c=g&comp='+ext+'&comm='+comm)
47 function restoreBackup() {
48 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
49 document.location.replace('backups.php?c=r&fn='+pFilename)
51 function viewBackup() {
52 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
53 var ext = pFilename.substr(pFilename.lastIndexOf('.') + 1)
55 alert('" . _('This extension can not be be viewed: ') . "' + ext)
58 window.open('" . BACKUP_PATH . "'+pFilename, '', 'toolbar=no,scrollbars=yes')
60 function deleteBackup() {
61 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
62 if (!confirm('" . _("Are you sure you want to delete the backup file - ") . "'+pFilename+'?'))
64 document.location.replace('backups.php?c=df&fn='+pFilename)
66 function downloadBackup() {
67 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
68 document.location.replace('backups.php?c=ds&fn='+pFilename)
71 function uploadBackup() {
72 if (document.forms[0].uploadfile.value=='') {
73 alert('" . _("Please select a file to upload.") . "')
76 document.forms[0].action='backups.php?c=u&fn=' + document.forms[0].uploadfile.value
77 document.forms[0].submit()
81 <table cellpadding=2 cellspacing=2 border=0>
83 <td style='padding-right:30px'>" . _("Backup scripts") . "</td>
86 <td style='padding-right:30px'>$cmb</td>
87 <script language='javascript'>
88 if (document.forms[0].cmb_backups.options.length!=0) document.forms[0].cmb_backups.selectedIndex=0
91 <table height=160 cellpadding=0 cellspacing=0 border=0>
92 <tr><td><input onclick='javascript:createBackup()' type='button' style='width:150px' value='". _("Create Backup") . "'></td><td style='padding-left:20px'>" . _("Compression") . " $compr</td></tr>
93 <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150px' value='". _("Restore Backup") . "'></td><td> </td></tr>
94 <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150px' value='". _("View Backup") . "'></td><td> </td></tr>
95 <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150px' value='". _("Delete Backup") . "'></td><td> </td></tr>
96 <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150px' value='". _("Download Backup") . "'></td><td> </td></tr>
97 <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150px' value='". _("Upload Backup"). "'></td>
98 <td style='padding-left:20px'><input name='uploadfile' type='file'></td>
104 <td style='padding-right:30px'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
107 <td style='padding-right:30px'><textarea rows=4 cols=30 name='comments'></textarea></td>
113 //-------------------------------------------------------------------------------------------------
118 function handle_form($conn)
120 if(isset($_GET['c']))
123 case 'g': //Generate Only
124 $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
125 header("Location: backups.php?c=gs&fn=" . urlencode($filename));
128 case 'gd': //Generate and download
129 $filename = generate_backup($conn);
130 header("Location: backups.php?c=ds&fn=" . urlencode($filename));
133 case 'd': //Download the file
134 download_file(BACKUP_PATH . $_GET['fn']);
137 case 'df': //Delete the file
138 $filename = $_GET['fn'];
139 @unlink(BACKUP_PATH . $filename);
140 header("Location: backups.php?c=dff&fn=" . urlencode($filename));
144 $msg = _("File successfully deleted.")." ";
145 $msg .= _("Filename") . " = " . $_GET['fn'];
146 display_notification($msg);
149 case 'ds': //Write JS script to open download window
150 $filename = urlencode($_GET['fn']);
151 display_notification(_("Backup is being downloaded..."));
153 add_js_source("<script language='javascript'>
154 function download_file() {location.href ='backups.php?c=d&fn=$filename'};
155 Behaviour.addLoadEvent(download_file);
159 case 'gs': //Print backup success message
160 $msg = _("Backup successfully generated."). ' ';
161 $msg .= _("Filename") . " = " . $_GET['fn'];
162 display_notification($msg);
165 case 'r': //Restore backup
166 $filename=$_GET['fn'];
167 if( restore_backup(BACKUP_PATH . $filename, $conn) )
168 header("Location: backups.php?c=rs&fn=" . urlencode($filename));
171 case 'rs': //Print restore success message
172 display_notification(_("Restore backup completed."));
176 $filename = $_FILES['uploadfile']['tmp_name'];
177 if (is_uploaded_file ($filename))
179 if( restore_backup($filename, $conn) )
180 display_notification(_("Uploaded file has been restored."));
182 display_error(_("Database restore failed."));
186 display_error(_("Backup was not uploaded into the system."));
191 function generate_backup($conn, $ext='no', $comm='')
193 if ($conn['tbpref'] != "")
194 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
196 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
198 $filename = db_export($conn, $filename, $ext, $comm);
203 function restore_backup($filename, $conn)
205 return db_import($filename, $conn);
208 function get_backup_file_combo()
210 global $path_to_root;
212 default_focus('cmb_backups');
213 $dh = opendir(BACKUP_PATH);
214 while (($file = readdir($dh)) !== false)
220 foreach ($ar_files as $file)
221 if (strpos($file, ".sql") || strpos($file, ".sql"))
222 $opt_files .= "<option value='$file'>$file</option>";
224 return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
227 function get_compr_combo()
231 $ar_comps[] = _("No");
232 if (function_exists("gzcompress"))
234 if (function_exists("gzopen"))
235 $ar_comps[] = "gzip";
237 foreach ($ar_comps as $file)
238 $opt_comps .= "<option value='$file'>$file</option>";
240 return "<select name='cmb_comp'>$opt_comps</select>";
243 function download_file($filename)
245 if (empty($filename) || !file_exists($filename))
249 $saveasname = basename($filename);
250 header('Content-type: application/octet-stream');
251 header('Content-Length: '.filesize($filename));
252 header('Content-Disposition: attachment; filename="'.$saveasname.'"');
257 function valid_paths()
259 global $path_to_root;
262 if (!file_exists(BACKUP_PATH))
263 $st .= " - " . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";