06-22
虽然功能不是很完善,应该很不完善,但还是放出来让大家批评一下。
感谢PHP开源支持者的鼓励,感谢杜江老师的鼓励。mysqldump v1.0 demo 下载 版权所有,欢迎盗版!
源代码
#!/usr/local/bin/php -q
# mysql cron manager system
# sunboyu@gmail.com
# v1.0
define(’SUN_FILE_NAME’,'bak.sh’);
define(’SUN_BAK_PATH’,’./’); #bak path
define(’SUN_CFG_PATH’,’./’); #config file path
define(’SUN_LOG_PATH’,’./’); #log file path
define(’SUN_DATABASE_CHARACTER’,'UTF-8′); #mysql character
define(’SUN_SHELL_EXEC’,”system”);
define(’SUN_IS_TAR’,0);
#save temp argv in session
session_start();
Main::mains();
class Main
{
//main()
public static function mains()
{
KernelInfo::version();
self::getUserInput();
}
//for user’s inpyt
public function getUserInput()
{
while(($u_char = KernelBase::cgetchar(’shell’))&&($u_char != ‘quit’))
{
switch($u_char)
{
case “–help”:
KernelInfo::help();
break;
case “–version”:
KernelInfo::version();
break;
case “–check”:
KernelInfo::checksystem();
break;
case “–mysqldump”:
Mysql::MysqlGuide();
break;
default:
print(”error input!\n”);
break;
}
}
}
}
class KernelBase
{
//get input string
public function cgetchar( $cue = false )
{
if( $cue )
{
print($cue.”:”);
}
$string = trim(fgets(STDIN));
return $string;
}
//get argvs
public function cgetargv( $string )
{
if(!empty($string))
{
$argvs = explode( ” ” , $string );
$reargvs = false;
for( $i = 0; $i < count($argvs) ; $i++ )
{
if( $argvs[$i][0]==’-’ && $argvs[$i][1]==’-’ )
{
$reargvs[$argvs[$i]] = true;
$x++;
}
else if( $argvs[$i][0]==’-’ && $argvs[$i][1]!=’-’ )
{
$reargvs[$argvs[$i]] = isset($argvs[($i+1)])&&($argvs[($i+1)][0]!=’-')&&($argvs[($i+1)][0]!=”) ? $argvs[($i+1)] : ”;
isset($argvs[($i+1)])&&($argvs[($i+1)][0]!=’-')&&($argvs[($i+1)][0]!=”) ? $i++ : ”;
}
}
return $reargvs;
}
else
{
return false;
}
}
}
class KernelIO
{
//write file
public function write_file( $filename , $content )
{
$handle = fopen( $filename );
fwrite( $handle , $content );
fclose( $handle );
}
}
class KernelInfo
{
//show version
public function version()
{
print(”##################################\n”);
print(”# Mysql Back Cron Manage Shell #\n”);
print(”# Auth: sunboyu #\n”);
print(”# Version: v1.0 #\n”);
print(”##################################\n”);
}
//show help
public function help()
{
print(sprintf(”%s [--version] [--help] [--check]\n”,SUN_FILE_NAME));
print(” –version Show\n”);
print(” –help This Help\n”);
print(” –check This Help\n”);
}
//check system info
public function checksystem()
{
print(”System:”.$_GLOBAL['System'].”\n”);
$check_dir = array( SUN_BAK_PATH , SUN_CFG_PATH , SUN_LOG_PATH );
foreach($check_dir as $key => $value)
{
if(is_writable($value))
{
print(”The dir ‘”.$value.”‘ is writable!\n”);
}
else
{
print(”The dir ‘”.$value.”‘ is unwritable.Please check the power!\n”);
print(”The shell is stop!”);
exit();
}
}
$shell_exec = SUN_SHELL_EXEC;
if($shell_exec(’ls’))
{
print(”The shell can execute “.SUN_SHELL_EXEC.”;\n”);
}
else
{
print(”The shell can’t execute “.SUN_SHELL_EXEC.”;\n”);
print(”The shell is stop!”);
exit();
}
print(”The Shell can work in this system!\n”);
}
//check is writeable the dir
public function is_writeable( $dir )
{
return @is_writable( $dir );
}
}
#Mysql Dump
class Mysql
{
function MysqlGuide()
{
$mysqlargv = array(
‘-host’ => false,
‘-username’ => false,
‘-password’ => false,
‘-datebase’ => false,
‘-otherargv’ => false
);
$argvs = KernelBase::cgetargv(KernelBase::cgetchar(”input argv”));
foreach($argvs as $key => $value)
{
$mysqlargv[$key] = empty($value) ? ” : $value;
}
if($mysqlargv['-host']&&$mysqlargv['-username']&&$mysqlargv['-datebase'])
{
self::Mysqldump( $mysqlargv['-host'] , $mysqlargv['-username'] , $mysqlargv['-password'] , $mysqlargv['-datebase'] , $mysqlargv['-otherargv'] );
}
else
{
print(”error vars!\n”);
}
}
function Mysqldump( $host = false , $username = false , $password = ” , $datebase = false , $otherargv = false )
{
$shell_exec = SUN_SHELL_EXEC;
if($host&&$username&&$datebase)
{
$command = “mysqldump –opt “.$datebase.” -u”.$datebase.” -p”.$db_password.( $otherargv ? $otherargv : ” ).” > “.SUN_BAK_PATH.”db_bak.sql”;
$feedback = $shell_exec( $command );
print($feedback.”\n”);
}
}
}
?>
六月 22nd, 2008 at 08:07:20
哈哈,顶顶
六月 23rd, 2008 at 09:26:54
学习学习
六月 23rd, 2008 at 10:53:58
做得还行,sunboyu加油