yano3nora
7/2/2017 - 7:21 AM

[cakephp: mysqldump] mysqldumpのcakephp実装サンプル。 #php #cakephp #mysql

[cakephp: mysqldump] mysqldumpのcakephp実装サンプル。 #php #cakephp #mysql

/**
 * [action] bakcup
 */
  public function backup(){
    if($this->request->is('post')){
      $this->autoRender = false;
      $db = $this->SystemTbl->getDbo();
      $dbHost = $db->config['host'];
      $dbUser = $db->config['login'];
      $dbPass = $db->config['password'];
      $dbName = $db->config['database'];;
      $filePath = "/var/www/tmp/";
      $fileName = date('ymd').'_'.date('His').'.sql';
      $command = "mysqldump ".$dbName." --host=".$dbHost." --user=".$dbUser." --password=".$dbPass." > ".$filePath.$fileName;
      system($command);     
      $dlFile = $filePath.$fileName;
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="'.$fileName.'"');
      header('Content-Length: '.filesize($dlFile));
      readfile($dlFile);
    }else{
      throw new MethodNotAllowedException();
    }
  }