You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.6 KiB
43 lines
1.6 KiB
<?php
|
|
|
|
namespace utils;
|
|
|
|
class mysql {
|
|
|
|
public static function exec($db, $password, $sql, $user = "root") {
|
|
$dsn = "-D {$db} -u {$user} -p{$password}";
|
|
exec(\neato::get_user_bin . 'mysql '. safeCmdQuotes($dsn) . ' -e ' . safeCmd($sql), $output, $exit_code);
|
|
display($output);
|
|
checkForError($exit_code, "Unable to run MySQL command: {$sql}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function import($db, $password, $file, $user = "root") {
|
|
$dsn = "-D {$db} -u {$user} -p{$password}";
|
|
exec(\neato::get_user_bin . 'mysql '. safeCmdQuotes($dsn) . ' <' . safeCmd($file), $output, $exit_code);
|
|
display($output);
|
|
checkForError($exit_code, "Unable to Import MySQL file: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function backup($db, $password, $file, $user = "root") {
|
|
$what = ($db == 'all') ? '-A' : "-B {$db}";
|
|
$dsn = "{$what} -u {$user} -p{$password}";
|
|
exec(\neato::get_user_bin . 'mysqldump '. safeCmdQuotes($dsn) . ' | gzip > $(date +\%Y_\%m_\%d-\%T)'. safeCmd($file) . '.sql.gz', $output, $exit_code);
|
|
display($output);
|
|
checkForError($exit_code, "Unable to Export MySQL file: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function gunzip($file) {
|
|
exec(\neato::get_bin . 'gunzip ' . safeCmd($file) . '.sql.gz', $output, $exit_code);
|
|
display($output);
|
|
checkForError($exit_code, "Unable to unzip MySQL file: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function remove_old($path = '', int $days_old = 10) {
|
|
exec(\neato::get_user_bin . 'find ' . $path . ' -name "*.gz" -mtime +'.$days_old.' -delete');
|
|
}
|
|
|
|
} |