beifenAction.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * 运行方式:E:\php\php-5.4.14\php.exe E:\IIS\app\xinhu\task.php beifen
  4. * url:http://demo.rockoa.com/task.php?m=beifen|runt
  5. * url:http://127.0.0.1/app/xinhu/task.php?m=beifen|runt
  6. */
  7. class beifenClassAction extends runtAction
  8. {
  9. //每天cli备份数据为sql文件的
  10. public function defaultAction()
  11. {
  12. if(PHP_SAPI != 'cli') return 'plase cli run';
  13. $alltabls = $this->db->getalltable();
  14. $nobeifne = array(''.PREFIX.'log',''.PREFIX.'logintoken',''.PREFIX.'kqanay',''.PREFIX.'email_cont',''.PREFIX.'reads',''.PREFIX.'dailyfx',''.PREFIX.'todo',''.PREFIX.'city'); //不备份的表;
  15. $data = array();
  16. $strstr = "/*
  17. 备份时间:".$this->now."
  18. */
  19. ";
  20. foreach($alltabls as $tabs){
  21. if(in_array($tabs, $nobeifne))continue;
  22. $strstr .= "DROP TABLE IF EXISTS `$tabs`;\n";
  23. $sqla = $this->db->getall('show create table `'.$tabs.'`');
  24. $strstr .= "".$sqla[0]['Create Table'].";\n";
  25. $rows = $this->db->getall('select * from `'.$tabs.'`');
  26. foreach($rows as $k=>$rs){
  27. $vstr = '';
  28. foreach($rs as $k1=>$v1){
  29. if(!isempt($v1))$v1 = str_replace("\n",'\n', $v1);
  30. $v1 = ($v1==null) ? 'null' : "'$v1'";
  31. $vstr.=",$v1";
  32. }
  33. $strstr .= "INSERT INTO `$tabs` VALUES(".substr($vstr,1).");\n";
  34. }
  35. $strstr .= "\n";
  36. }
  37. $rnd = str_shuffle('abcedfghijk').rand(1000,9999);
  38. $file = ''.DB_BASE.'_'.date('Y.m.d.H.i.s').'_'.$rnd.'.sql';
  39. $filepath = ''.UPDIR.'/data/'.$file.'';
  40. $this->rock->createtxt($filepath, $strstr);
  41. //给管理员邮箱发邮件
  42. m('email')->sendmail(''.TITLE.'数据库备份',''.TITLE.'数据库备份'.$this->rock->now.'', 1 , array(), 1, array(
  43. 'attachname'=> $file,
  44. 'attachpath'=> $filepath,
  45. ));
  46. @unlink($filepath);
  47. return 'success';
  48. }
  49. /**
  50. * 备份数据库
  51. * php task.php beifen,create -table=menu
  52. */
  53. public function createAction()
  54. {
  55. if(PHP_SAPI != 'cli') return 'plase cli run:php task.php beifen,create';
  56. $table = $this->getparams('table');
  57. ob_end_clean();
  58. $path = ''.ROOT_PATH.'/'.DB_BASE.''.$table.'_'.date('YmdHis').'.sql';
  59. $file = fopen($path, 'ab+');
  60. $nobeifne = array(''.PREFIX.'log',''.PREFIX.'logintoken',''.PREFIX.'kqanay',''.PREFIX.'email_cont',''.PREFIX.'dailyfx',''.PREFIX.'reads',''.PREFIX.'todo',''.PREFIX.'city'); //不备份的表;
  61. if($table){
  62. $alltabls[] = ''.PREFIX.''.$table.'';
  63. }else{
  64. $alltabls = $this->db->getalltable();
  65. }
  66. foreach($alltabls as $tabs){
  67. $sqla = $this->db->getall('show create table `'.$tabs.'`');
  68. $createsql = $sqla[0]['Create Table'];
  69. $createsql = str_replace('`'.$tabs.'`', 'IF NOT EXISTS `'.$tabs.'`', $createsql).";\n";
  70. if(!in_array($tabs, $nobeifne)){
  71. $strstr = "\nDROP TABLE IF EXISTS `$tabs`;\n";
  72. $strstr .= $createsql;
  73. fwrite($file,$strstr);
  74. $rows = $this->db->getall('select * from `'.$tabs.'`', function($rs, $cans){
  75. $vstr = '';
  76. foreach($rs as $k1=>$v1){
  77. if(!isempt($v1))$v1 = str_replace("\n",'\n', $v1);
  78. $v1 = ($v1===null) ? 'NULL' : "'$v1'";
  79. $vstr.=",$v1";
  80. }
  81. $vstr = substr($vstr,1);
  82. $tabs = $cans['tabs'];
  83. $strstr = "INSERT INTO `$tabs` VALUES($vstr);\n";
  84. fwrite($cans['file'], $strstr);
  85. return $vstr;
  86. }, array(
  87. 'file' => $file,
  88. 'tabs' => $tabs
  89. ));
  90. echo ''.$tabs.' success count('.count($rows).')'.PHP_EOL;
  91. }else{
  92. fwrite($file,$createsql);
  93. echo ''.$tabs.' break'.PHP_EOL;
  94. }
  95. }
  96. fclose($file);
  97. echo 'success'.PHP_EOL;
  98. }
  99. }
粤ICP备19079148号