-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace F\Back\Libraries; | ||
|
||
use Phalcon\Db\Adapter\Pdo\AbstractPdo; | ||
|
||
class DbBackup | ||
{ | ||
private $db; | ||
private $backupFolderPath; | ||
|
||
public function __construct(AbstractPdo $db, string $backupFolderPath) | ||
{ | ||
$this->db = $db; | ||
$this->backupFolderPath = $backupFolderPath; | ||
} | ||
|
||
public function backupDatabase() | ||
{ | ||
$backupFilePath = $this->backupFolderPath . "/" . $this->db->getDescriptor()['dbname'] . "_" . date("Y-m-d_H-i-s") . ".sql"; | ||
|
||
$this->removePreviousBackupFiles(); | ||
|
||
$process = proc_open( | ||
sprintf( | ||
"mysqldump --host=%s --user=%s --password=%s %s", | ||
escapeshellarg($this->db->getDescriptor()['host']), | ||
escapeshellarg($this->db->getDescriptor()['username']), | ||
escapeshellarg($this->db->getDescriptor()['password']), | ||
escapeshellarg($this->db->getDescriptor()['dbname']) | ||
), | ||
[ | ||
0 => ["pipe", "r"], // stdin | ||
1 => ["file", $backupFilePath, "w"], // stdout | ||
2 => ["pipe", "w"] // stderr | ||
], | ||
$pipes | ||
); | ||
|
||
if (!is_resource($process)) { | ||
error_log("Failed to initiate mysqldump process."); | ||
echo "Failed to initiate mysqldump process."; | ||
return; | ||
} | ||
|
||
fclose($pipes[0]); // Close stdin | ||
|
||
$errors = stream_get_contents($pipes[2]); // Read errors | ||
fclose($pipes[2]); // Close stderr | ||
|
||
$return_value = proc_close($process); // Close process | ||
|
||
if ($return_value === 0) { | ||
echo "Database backup created successfully: $backupFilePath"; | ||
error_log("Database backup created successfully: $backupFilePath"); | ||
} else { | ||
echo "Errors occurred during database backup: $errors"; | ||
error_log("Errors occurred during database backup: $errors"); | ||
} | ||
} | ||
|
||
private function removePreviousBackupFiles() | ||
{ | ||
array_map('unlink', glob($this->backupFolderPath . "/" . $this->db->getDescriptor()['dbname'] . "_*.sql")); | ||
} | ||
} |