Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.09 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * Use this class to do a backup of your database
  5.  * @author Raul Souza Silva (raul.3k@gmail.com)
  6.  * @category Database
  7.  * @copyright No one. You can copy, edit, do anything you want. If you change anything to better, please let me know.
  8.  *
  9.  */
  10. Class DBBackup {
  11.     /**
  12.      *
  13.      * The host you will connect
  14.      * @var String
  15.      */
  16.     private $host;
  17.     /**
  18.      *
  19.      * The driver you will use to connect
  20.      * @var String
  21.      */
  22.     private $driver;
  23.     /**
  24.      *
  25.      * The user you will use to connect to a database
  26.      * @var String
  27.      */
  28.     private $user;
  29.     /**
  30.      *
  31.      * The password you will use to connect to a database
  32.      * @var String
  33.      */
  34.     private $password;
  35.     /**
  36.      *
  37.      * The database you will use to connect
  38.      * @var String
  39.      */
  40.     private $dbName;
  41.     /**
  42.      *
  43.      * String to connect to the database using PDO
  44.      * @var String
  45.      */
  46.     private $dsn;
  47.  
  48.     /**
  49.      *
  50.      * Array with the tables of the database
  51.      * @var Array
  52.      */
  53.     private $tables = array();
  54.  
  55.     /**
  56.      *
  57.      * Hold the connection
  58.      * @var ObjectConnection
  59.      */
  60.     private $handler;
  61.     /**
  62.      *
  63.      * Array to hold the errors
  64.      * @var Array
  65.      */
  66.     private $error = array();
  67.  
  68.     /**
  69.      *
  70.      * The result string. String with all queries
  71.      * @var String
  72.      */
  73.     private $final;
  74.  
  75.     /**
  76.      *
  77.      * The main function
  78.      * @method DBBackup
  79.      * @uses Constructor
  80.      * @param Array $args{host, driver, user, password, database}
  81.      * @example $db = new DBBackup(array('host'=>'my_host', 'driver'=>'bd_type(mysql)', 'user'=>'db_user', 'password'=>'db_password', 'database'=>'db_name'));
  82.      */
  83.     public function DBBackup($args){
  84.         if(!$args['host']) $this->error[] = 'Parameter host missing';
  85.         if(!$args['user']) $this->error[] = 'Parameter user missing';
  86.         if(!isset($args['password'])) $this->error[] = 'Parameter password missing';
  87.         if(!$args['database']) $this->error[] = 'Parameter database missing';
  88.         if(!$args['driver']) $this->error[] = 'Parameter driver missing';
  89.  
  90.         if(count($this->error)>0){
  91.             return;
  92.         }
  93.  
  94.         $this->host = $args['host'];
  95.         $this->driver = $args['driver'];
  96.         $this->user = $args['user'];
  97.         $this->password = $args['password'];
  98.         $this->dbName = $args['database'];
  99.  
  100.         $this->final = 'CREATE DATABASE ' . $this->dbName.";\n\n";
  101.  
  102.         if($this->host=='localhost'){
  103.             // We have a little issue in unix systems when you set the host as localhost
  104.             $this->host = '127.0.0.1';
  105.         }
  106.         $this->dsn = $this->driver.':host='.$this->host.';dbname='.$this->dbName;
  107.  
  108.         $this->connect();
  109.         $this->getTables();
  110.         $this->generate();
  111.     }
  112.  
  113.     /**
  114.      *
  115.      * Call this function to get the database backup
  116.      * @example DBBackup::backup();
  117.      */
  118.     public function backup(){
  119.         //return $this->final;
  120.         if(count($this->error)>0){
  121.             return array('error'=>true, 'msg'=>$this->error);
  122.         }
  123.         return array('error'=>false, 'msg'=>$this->final);
  124.     }
  125.  
  126.     /**
  127.      *
  128.      * Generate backup string
  129.      * @uses Private use
  130.      */
  131.     private function generate(){
  132.         foreach ($this->tables as $tbl) {
  133.             $this->final .= '--CREATING TABLE '.$tbl['name']."\n";
  134.             $this->final .= $tbl['create'] . ";\n\n";
  135.             $this->final .= '--INSERTING DATA INTO '.$tbl['name']."\n";
  136.             $this->final .= $tbl['data']."\n\n\n";
  137.         }
  138.         $this->final .= '-- THE END'."\n\n";
  139.     }
  140.  
  141.     /**
  142.      *
  143.      * Connect to a database
  144.      * @uses Private use
  145.      */
  146.     private function connect(){
  147.         try {
  148.             $this->handler = new PDO($this->dsn, $this->user, $this->password);
  149.         } catch (PDOException $e) {
  150.             $this->handler = null;
  151.             $this->error[] = $e->getMessage();
  152.             return false;
  153.         }
  154.     }
  155.  
  156.     /**
  157.      *
  158.      * Get the list of tables
  159.      * @uses Private use
  160.      */
  161.     private function getTables(){
  162.         try {
  163.             $stmt = $this->handler->query('SHOW TABLES');
  164.             $tbs = $stmt->fetchAll();
  165.             $i=0;
  166.             foreach($tbs as $table){
  167.                 $this->tables[$i]['name'] = $table[0];
  168.                 $this->tables[$i]['create'] = $this->getColumns($table[0]);
  169.                 $this->tables[$i]['data'] = $this->getData($table[0]);
  170.                 $i++;
  171.             }
  172.             unset($stmt);
  173.             unset($tbs);
  174.             unset($i);
  175.  
  176.             return true;
  177.         } catch (PDOException $e) {
  178.             $this->handler = null;
  179.             $this->error[] = $e->getMessage();
  180.             return false;
  181.         }
  182.     }
  183.  
  184.     /**
  185.      *
  186.      * Get the list of Columns
  187.      * @uses Private use
  188.      */
  189.     private function getColumns($tableName){
  190.         try {
  191.             $stmt = $this->handler->query('SHOW CREATE TABLE '.$tableName);
  192.             $q = $stmt->fetchAll();
  193.             $q[0][1] = preg_replace("/AUTO_INCREMENT=[\w]*./", '', $q[0][1]);
  194.             return $q[0][1];
  195.         } catch (PDOException $e){
  196.             $this->handler = null;
  197.             $this->error[] = $e->getMessage();
  198.             return false;
  199.         }
  200.     }
  201.  
  202.     /**
  203.      *
  204.      * Get the insert data of tables
  205.      * @uses Private use
  206.      */
  207.     private function getData($tableName){
  208.         try {
  209.             $stmt = $this->handler->query('SELECT * FROM '.$tableName);
  210.             $q = $stmt->fetchAll(PDO::FETCH_NUM);
  211.             $data = '';
  212.             foreach ($q as $pieces){
  213.                 foreach($pieces as &$value){
  214.                     $value = htmlentities(addslashes($value));
  215.                 }
  216.                 $data .= 'INSERT INTO '. $tableName .' VALUES (\'' . implode('\',\'', $pieces) . '\');'."\n";
  217.             }
  218.             return $data;
  219.         } catch (PDOException $e){
  220.             $this->handler = null;
  221.             $this->error[] = $e->getMessage();
  222.             return false;
  223.         }
  224.     }
  225. }
  226. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement