123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /*
- * 工具包
- */
- define('PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
- class Tool
- {
- public static function out($text)
- {
- echo $text . "\n";
- }
-
- public static function run($shell)
- {
- self::out('运行中......');
- system($shell);
- }
-
- public static function input($text, $default = '')
- {
- if(is_array($text))
- {
- $text = implode("\n", $text);
- }
-
- self::out($text);
- $stdin = fopen('php://stdin','r');
- $shell = trim(fgets($stdin,100));
-
- return $shell ? $shell : $default;
- }
-
- private static function mysql()
- {
- $info['host'] = self::input('请输入mysql的主机地址:默认为localhost', 'localhost');
- $info['username'] = self::input('请输入mysql的账号:默认为root', 'root');
- $info['password'] = self::input('请输入mysql的密码:默认为空', '');
- $info['database'] = self::input('请输入mysql的数据库名:');
-
- if(!$info['host'] || !$info['username'] || !$info['password'] || !$info['database'])
- {
- self::out('请输入正确的数据库信息!');die;
- }
-
- $info['file'] = PATH . 'data/sql/' . $info['database'];
-
- $info['shell'] = ' -u' . $info['username'] . ' -p' . $info['password'] . ' -h' . $info['host'] . ' ';
- return $info;
- }
-
- public static function mysql_out()
- {
- $info = self::mysql();
-
- $info['table'] = self::input('请输入mysql的表名(不输入则备份整个'.$info['database'].'数据库):');
- $info['type'] = self::input('请输入备份类型:1为备份全部,2为备份结构,3为备份数据');
-
- $info['shell'] = 'mysqldump ' . $info['shell'];
- if($info['type'] == 2)
- {
- $info['shell'] .= ' -d ' . $info['database'];
- }
- elseif($info['type'] == 3)
- {
- $info['shell'] .= ' -t ' . $info['database'];
- }
- else
- {
- $info['shell'] .= ' ' . $info['database'];
- }
-
- if($info['table'])
- {
- $info['shell'] .= ' ' . $info['table'];
- $info['file'] .= '.' . $info['table'];
- }
-
- $info['file'] .= '.sql';
-
- $info['shell'] .= ' > ' . $info['file'];
-
- self::run($info['shell']);
- self::out('操作成功,输出路径:' . $info['file']);
- }
-
- public static function mysql_in()
- {
- $info = self::mysql();
-
- $info['new'] = self::input('请输入要恢复的全新数据库:为空则使用上边填的数据库', $info['database']);
- $create_table = 'mysqladmin ' . $info['shell'] . ' create ' . $info['new'];
- $info['shell'] = 'mysql ' . $info['shell'] . ' ' . $info['new'];
-
- $info['file'] .= '.sql';
-
- $info['shell'] .= ' < ' . $info['file'];
-
- self::run($create_table);
- self::run($info['shell']);
- self::out('操作成功,您已成功恢复' . $info['file'] . '里的数据');
- }
- }
- $text = array
- (
- '请输入命令以执行相应操作:'
- , 'mysql_out:备份mysql'
- , 'mysql_in:恢复mysql'
- , '请在输入命令之后按回车键'
-
- );
- $shell = Tool::input($text);
- switch($shell)
- {
- case 'mysql_out':
- Tool::mysql_out();
- break;
- case 'mysql_in':
- Tool::mysql_in();
- break;
- default:
- echo "未定义的方法";
- break;
- }
|