tool.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * 工具包
  4. */
  5. define('PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  6. class Tool
  7. {
  8. public static function out($text)
  9. {
  10. echo $text . "\n";
  11. }
  12. public static function run($shell)
  13. {
  14. self::out('运行中......');
  15. system($shell);
  16. }
  17. public static function input($text, $default = '')
  18. {
  19. if(is_array($text))
  20. {
  21. $text = implode("\n", $text);
  22. }
  23. self::out($text);
  24. $stdin = fopen('php://stdin','r');
  25. $shell = trim(fgets($stdin,100));
  26. return $shell ? $shell : $default;
  27. }
  28. private static function mysql()
  29. {
  30. $info['host'] = self::input('请输入mysql的主机地址:默认为localhost', 'localhost');
  31. $info['username'] = self::input('请输入mysql的账号:默认为root', 'root');
  32. $info['password'] = self::input('请输入mysql的密码:默认为空', '');
  33. $info['database'] = self::input('请输入mysql的数据库名:');
  34. if(!$info['host'] || !$info['username'] || !$info['password'] || !$info['database'])
  35. {
  36. self::out('请输入正确的数据库信息!');die;
  37. }
  38. $info['file'] = PATH . 'data/sql/' . $info['database'];
  39. $info['shell'] = ' -u' . $info['username'] . ' -p' . $info['password'] . ' -h' . $info['host'] . ' ';
  40. return $info;
  41. }
  42. public static function mysql_out()
  43. {
  44. $info = self::mysql();
  45. $info['table'] = self::input('请输入mysql的表名(不输入则备份整个'.$info['database'].'数据库):');
  46. $info['type'] = self::input('请输入备份类型:1为备份全部,2为备份结构,3为备份数据');
  47. $info['shell'] = 'mysqldump ' . $info['shell'];
  48. if($info['type'] == 2)
  49. {
  50. $info['shell'] .= ' -d ' . $info['database'];
  51. }
  52. elseif($info['type'] == 3)
  53. {
  54. $info['shell'] .= ' -t ' . $info['database'];
  55. }
  56. else
  57. {
  58. $info['shell'] .= ' ' . $info['database'];
  59. }
  60. if($info['table'])
  61. {
  62. $info['shell'] .= ' ' . $info['table'];
  63. $info['file'] .= '.' . $info['table'];
  64. }
  65. $info['file'] .= '.sql';
  66. $info['shell'] .= ' > ' . $info['file'];
  67. self::run($info['shell']);
  68. self::out('操作成功,输出路径:' . $info['file']);
  69. }
  70. public static function mysql_in()
  71. {
  72. $info = self::mysql();
  73. $info['new'] = self::input('请输入要恢复的全新数据库:为空则使用上边填的数据库', $info['database']);
  74. $create_table = 'mysqladmin ' . $info['shell'] . ' create ' . $info['new'];
  75. $info['shell'] = 'mysql ' . $info['shell'] . ' ' . $info['new'];
  76. $info['file'] .= '.sql';
  77. $info['shell'] .= ' < ' . $info['file'];
  78. self::run($create_table);
  79. self::run($info['shell']);
  80. self::out('操作成功,您已成功恢复' . $info['file'] . '里的数据');
  81. }
  82. }
  83. $text = array
  84. (
  85. '请输入命令以执行相应操作:'
  86. , 'mysql_out:备份mysql'
  87. , 'mysql_in:恢复mysql'
  88. , '请在输入命令之后按回车键'
  89. );
  90. $shell = Tool::input($text);
  91. switch($shell)
  92. {
  93. case 'mysql_out':
  94. Tool::mysql_out();
  95. break;
  96. case 'mysql_in':
  97. Tool::mysql_in();
  98. break;
  99. default:
  100. echo "未定义的方法";
  101. break;
  102. }