2015-07-20 49 views
-7

此代码用于备份mysql数据库。我希望修改下面给出的代码,因为它会根据给定的时间段执行备份,请参阅您的帮助。PHP:如何根据给定的时间段(例如:最近6个月)对mysql数据库进行备份

Class DBBackup { 
    /** 
    * 
    * The host you will connect 
    * @var String 
    */ 
    private $host; 



/** 
    * 
    * The driver you will use to connect 
    * @var String 
    */ 
    private $driver; 
    /** 
    * 
    * The user you will use to connect to a database 
    * @var String 
    */ 
    private $user; 
    /** 
    * 
    * The password you will use to connect to a database 
    * @var String 
    */ 
    private $password; 
    /** 
    * 
    * The database you will use to connect 
    * @var String 
    */ 
    private $dbName; 
    /** 
    * 
    * String to connect to the database using PDO 
    * @var String 
    */ 
    private $dsn; 

    /** 
    * 
    * Array with the tables of the database 
    * @var Array 
    */ 
    private $tables = array(); 

    /** 
    * 
    * Hold the connection 
    * @var ObjectConnection 
    */ 
    private $handler; 
    /** 
    * 
    * Array to hold the errors 
    * @var Array 
    */ 
    private $error = array(); 

    /** 
    * 
    * The result string. String with all queries 
    * @var String 
    */ 
    private $final; 

    /** 
    * 
    * The main function 
    * @method DBBackup 
    * @uses Constructor 
    * @param Array $args{host, driver, user, password, database} 
    * @example $db = new DBBackup(array('host'=>'my_host', 'driver'=>'bd_type(mysql)', 'user'=>'db_user', 'password'=>'db_password', 'database'=>'db_name')); 
    */ 
    public function DBBackup($args){ 
     if(!$args['host']) $this->error[] = 'Parameter host missing'; 
     if(!$args['user']) $this->error[] = 'Parameter user missing'; 
     if(!isset($args['password'])) $this->error[] = 'Parameter password missing'; 
     if(!$args['database']) $this->error[] = 'Parameter database missing'; 
     if(!$args['driver']) $this->error[] = 'Parameter driver missing'; 

     if(count($this->error)>0){ 
      return; 
     } 

     $this->host = $args['host']; 
     $this->driver = $args['driver']; 
     $this->user = $args['user']; 
     $this->password = $args['password']; 
     $this->dbName = $args['database']; 

     $this->final = 'CREATE DATABASE ' . $this->dbName.";\n\n"; 

     if($this->host=='localhost'){ 
      // We have a little issue in unix systems when you set the host as localhost 
      $this->host = '127.0.0.1'; 
     } 
     $this->dsn = $this->driver.':host='.$this->host.';dbname='.$this->dbName; 

     $this->connect(); 
     $this->getTables(); 
     $this->generate(); 
    } 

    /** 
    * 
    * Call this function to get the database backup 
    * @example DBBackup::backup(); 
    */ 
    public function backup(){ 
     //return $this->final; 
     if(count($this->error)>0){ 
      return array('error'=>true, 'msg'=>$this->error); 
     } 
     return array('error'=>false, 'msg'=>$this->final); 
    } 

    /** 
    * 
    * Generate backup string 
    * @uses Private use 
    */ 
    private function generate(){ 
     foreach ($this->tables as $tbl) { 
      $this->final .= '--CREATING TABLE '.$tbl['name']."\n"; 
      $this->final .= $tbl['create'] . ";\n\n"; 
      $this->final .= '--INSERTING DATA INTO '.$tbl['name']."\n"; 
      $this->final .= $tbl['data']."\n\n\n"; 
     } 
     $this->final .= '-- THE END'."\n\n"; 
    } 

    /** 
    * 
    * Connect to a database 
    * @uses Private use 
    */ 
    private function connect(){ 
     try { 
      $this->handler = new PDO($this->dsn, $this->user, $this->password); 
     } catch (PDOException $e) { 
      $this->handler = null; 
      $this->error[] = $e->getMessage(); 
      return false; 
     } 
    } 

    /** 
    * 
    * Get the list of tables 
    * @uses Private use 
    */ 
    private function getTables(){ 
     try { 
      $stmt = $this->handler->query('SHOW TABLES'); 
      $tbs = $stmt->fetchAll(); 
      $i=0; 
      foreach($tbs as $table){ 
       $this->tables[$i]['name'] = $table[0]; 
       $this->tables[$i]['create'] = $this->getColumns($table[0]); 
       $this->tables[$i]['data'] = $this->getData($table[0]); 
       $i++; 
      } 
      unset($stmt); 
      unset($tbs); 
      unset($i); 

      return true; 
     } catch (PDOException $e) { 
      $this->handler = null; 
      $this->error[] = $e->getMessage(); 
      return false; 
     } 
    } 

    /** 
    * 
    * Get the list of Columns 
    * @uses Private use 
    */ 
    private function getColumns($tableName){ 
     try { 
      $stmt = $this->handler->query('SHOW CREATE TABLE '.$tableName); 
      $q = $stmt->fetchAll(); 
      $q[0][1] = preg_replace("/AUTO_INCREMENT=[\w]*./", '', $q[0][1]); 
      return $q[0][1]; 
     } catch (PDOException $e){ 
      $this->handler = null; 
      $this->error[] = $e->getMessage(); 
      return false; 
     } 
    } 

    /** 
    * 
    * Get the insert data of tables 
    * @uses Private use 
    */ 
    private function getData($tableName){ 
     try { 
      $stmt = $this->handler->query('SELECT * FROM '.$tableName); 
      $q = $stmt->fetchAll(PDO::FETCH_NUM); 
      $data = ''; 
      foreach ($q as $pieces){ 
       foreach($pieces as &$value){ 
        $value = htmlentities(addslashes($value)); 
       } 
       $data .= 'INSERT INTO '. $tableName .' VALUES (\'' . implode('\',\'', $pieces) . '\');'."\n"; 
      } 
      return $data; 
     } catch (PDOException $e){ 
      $this->handler = null; 
      $this->error[] = $e->getMessage(); 
      return false; 
     } 
    } 
} 
?> 
+3

你尝试过一些东西吗? – Epodax

+0

期待我们的帮助? –

回答

0

使用crontab(ç^h RO没有标签 LE)到同名但它:

  • 只是必须创造什么用你的类简单的PHP文件。
  • 定义在服务器端的时间(使用crontab -e命令),并调用文件

注意:您必须有一个到服务器的SSH访问。

更多信息here

+0

我不认为他需要每x次的备份,我想他想要过去x时间的备份。因此对备份进行了范围划分 – cujo

相关问题