forked from KwaMoja/KwaMoja
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBackupDatabase.php
60 lines (52 loc) · 3.13 KB
/
BackupDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
$PageSecurity = 15; //hard coded in case database is old and PageSecurity stuff cannot be retrieved
include ('includes/session.php');
$Title = _('Backup Database');
include ('includes/header.php');
if (isset($_GET['BackupFile'])) {
$BackupFiles = scandir('companies/' . $_SESSION['DatabaseName'], 0);
$DeletedFiles = false;
foreach ($BackupFiles as $BackupFile) {
if (mb_substr($BackupFile, 0, 6) == 'Backup') {
$DeleteResult = unlink('companies/' . $_SESSION['DatabaseName'] . '/' . $BackupFile);
if ($DeleteResult == true) {
prnMsg(_('Deleted') . ' companies/' . $_SESSION['DatabaseName'] . '/' . $BackupFile, 'info');
$DeletedFiles = true;
} else {
prnMsg(_('Unable to delete') . ' companies/' . $_SESSION['DatabaseName'] . '/' . $BackupFile, 'warn');
}
}
}
if ($DeletedFiles) {
prnMsg(_('All backup files on the server have been deleted'), 'success');
} else {
prnMsg(_('No backup files on the server were deleted'), 'info');
}
} else {
$BackupFile = $RootPath . '/companies/' . $_SESSION['DatabaseName'] . '/' . _('Backup') . '_' . Date('Y-m-d-H-i-s') . '.sql.gz';
$Command = 'mysqldump --opt -h' . $Host . ' -u' . $DBUser . ' -p' . $DBPassword . ' ' . $_SESSION['DatabaseName'] . '| gzip > ' . $_SERVER['DOCUMENT_ROOT'] . $BackupFile;
$CommandOutput = array();
exec($Command, $CommandOutput, $ReturnValue);
if ($ReturnValue == 0) {
prnMsg(_('The backup file has now been created. You must now download this to your computer because in case the web-server has a disk failure the backup would then not on the same machine. Use the link below') . '<br /><br /><a href="' . $BackupFile . '">' . _('Download the backup file to your locale machine') . '</a>', 'success');
prnMsg(_('Once you have downloaded the database backup file to your local machine you should use the link below to delete it - backup files can consume a lot of space on your hosting account and will accumulate if not deleted - they also contain sensitive information which would otherwise be available for others to download!'), 'info');
echo '<a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '?BackupFile=', urlencode($BackupFile), '">', _('Delete the backup file off the server'), '</a>';
} else {
prnMsg(_('There was some problem producing a backup using mysqldump. Normally this relates to a permissions issue - the web-server user must have permission to write to the companies directory'), 'error');
}
}
/*
//this could be a weighty file attachment!!
include('includes/htmlMimeMail.php');
$Mail = new htmlMimeMail();
$attachment = $Mail->getFile( $BackupFile);
$Mail->setText(_('Backup file attached'));
$Mail->addAttachment($attachment, $BackupFile, 'application/gz');
$Mail->setSubject(_('Database Backup'));
$Mail->setFrom($_SESSION['CompanyRecord']['coyname'] . '<' . $_SESSION['CompanyRecord']['email'] . '>');
$Result = $Mail->send(array('"' . $_SESSION['UsersRealName'] . '" <' . $_SESSION['UserEmail'] . '>'));
prnMsg(_('A backup of the database has been taken and emailed to you'), 'info');
unlink($BackupFile); // would be a security issue to leave it there for all to download/see
*/
include ('includes/footer.php');
?>