Created
March 15, 2013 19:54
-
-
Save letsspeak/5172638 to your computer and use it in GitHub Desktop.
FuelPHP MySQL backup script
This file contains hidden or 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
<?php | |
namespace Fuel\Tasks; | |
class MySQLBackup | |
{ | |
public static function system_ex($cmd, $stdin = "") | |
{ | |
$descriptorspec = array( | |
0 => array("pipe", "r"), | |
1 => array("pipe", "w"), | |
2 => array("pipe", "w") | |
); | |
$process = proc_open($cmd, $descriptorspec, $pipes); | |
$result_message = ""; | |
$error_message = ""; | |
$return = null; | |
if (is_resource($process)) | |
{ | |
fputs($pipes[0], $stdin); | |
fclose($pipes[0]); | |
while ($error = fgets($pipes[2])){ | |
$error_message .= $error; | |
} | |
while ($result = fgets($pipes[1])){ | |
$result_message .= $result; | |
} | |
foreach ($pipes as $k=>$_rs){ | |
if (is_resource($_rs)){ | |
fclose($_rs); | |
} | |
} | |
$return = proc_close($process); | |
} | |
return array( | |
'return' => $return, | |
'stdout' => $result_message, | |
'stderr' => $error_message, | |
); | |
} | |
public static function run($args = NULL) | |
{ | |
\Config::load('db', true); | |
$name = \Config::get('db.active'); | |
$dsn = \Config::get('db.'.$name.'.connection.dsn'); | |
$username = \Config::get('db.'.$name.'.connection.username'); | |
$password = \Config::get('db.'.$name.'.connection.password'); | |
@preg_match('/^(.+):host=(.+);dbname=(.+)$/i', $dsn, $matches); | |
if (count($matches) !== 4){ | |
$message = "Config dsn doesn't match.".PHP_EOL; | |
$message .= 'check fuel/app/config/development/db.php'.PHP_EOL; | |
echo $message; | |
\Model_AlertMail::send_error('Fuel MySQLBackup Error', $message); | |
return; | |
} | |
$dbtype = $matches[1]; | |
$dbhost = $matches[2]; | |
$dbname = $matches[3]; | |
if (strtolower($dbtype) !== 'mysql') { | |
$message = 'Config database type is not MySQL.'.PHP_EOL; | |
$message .= 'check fuel/app/config/development/db.php'.PHP_EOL; | |
echo $message; | |
// \Model_AlertMail::send_error('Fuel MySQLBackup Error', $message); | |
return; | |
} | |
$fileDir = '/var/log/fuel/'; | |
$fileName = date('ymd').'_'.date('His').'.sql'; | |
if ( ! file_exists($fileDir) ) { | |
try | |
{ | |
if ( ! @mkdir($fileDir) ){ | |
$message = 'Cannnot create directory '.$fileDir.PHP_EOL; | |
echo $message; | |
\Model_AlertMail::send_error('Fuel MySQLBackup Error', $message); | |
return; | |
} | |
} | |
catch (\Exception $e) | |
{ | |
echo "Cannnot create directory ",$fileDir.PHP_EOL; | |
\Model_AlertMail::send_error('Fuel MySQLBackup Error', $message); | |
return; | |
} | |
} | |
$command = "mysqldump --default-character-set=binary ".$dbname." --host=".$dbhost." --user=".$username." --password=".$password." > ".$fileDir.$fileName; | |
$return = MySQLBackup::system_ex($command); | |
if ($return["stderr"] !== "") { | |
$message = 'mysqldump failed for reasion:'.PHP_EOL.$return["stderr"]; | |
\Model_AlertMail::send_error('Fuel MySQLBackup Error', $message); | |
return; | |
} | |
echo 'MySQLBackup successfully completed.'.PHP_EOL; | |
} | |
} | |
/* End of file tasks/MySQLBackup.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment