Ejemplo n.º 1
0
 public function backup_tables($allowed_tables = '', $full_backup = true, $archive = false)
 {
     $tables = $this->show_tables();
     $output = '';
     $backup_files_sql = array();
     $date = date('d-m-Y--H-i-s');
     $sourcedir_master_sql = $this->settings['safemode_dir'] . $this->settings['sfstore'] . 'db_backup/database/';
     $sourcedir_tables_sql = $this->settings['safemode_dir'] . $this->settings['sfstore'] . 'db_backup/tables/';
     $backup_file_master_sql = $sourcedir_master_sql . 'database_' . DB_NAME . '-' . $date . '.sql';
     $backup_file_master_zip = $sourcedir_master_sql . 'database_' . DB_NAME . '-' . $date . '.zip';
     $backup_file_tables_zip = $sourcedir_tables_sql . 'tables_database_' . DB_NAME . '-' . $date . '.zip';
     if (!empty($allowed_tables)) {
         $tables = $this->db_allowed_tables_filter($tables, $allowed_tables);
     }
     foreach ($tables as $table) {
         $create_table = $this->db_build_create_table($table);
         //echo '<pre>'.$create_table.'</pre>';
         $backup_file = $sourcedir_tables_sql . 'table_' . $table . '-' . $date . '.sql';
         // $backup_file_zip = $this->settings['safemode_dir'].$this->settings['sfstore'].'db_backup/tables/table_'.$table.'-'.$date.'.zip';
         $backup_files_sql[] = $backup_file;
         $backup_file_csv = $this->settings['safemode_dir'] . $this->settings['sfstore'] . 'db_backup/csv/' . $table . '-' . $date . '.csv';
         if (file_exists($backup_file)) {
             unlink($backup_file);
         }
         if (file_exists($backup_file_csv)) {
             unlink($backup_file_csv);
         }
         $table_records = $this->db_build_insert_records($table);
         $content = $create_table . $table_records;
         //$content = $create_table;
         file_put_contents($backup_file, $content);
         //file_put_
         //echo '<pre>'.$table_records.'</pre>';
         // file_put_contents($backup_file,'');
         // echo $backup_file;
         try {
             //   echo "SELECT * INTO OUTFILE '". $backup_file . "' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\' FROM ".DB_NAME."." . $table . "<br/>";
             //  $q = $this->query("SELECT * INTO OUTFILE '". $backup_file . "' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '),\n('  FROM ".DB_NAME."." . $table . "");
             // $q->execute();
             //$dummy = $q->fetchAll();
             // print_r($dummy);
         } catch (PDOException $ex) {
             echo '<p style="color:red">Error: </p>' . $ex->getMessage();
             return false;
         }
     }
     if ($full_backup == true) {
         $full_path = DashboardHelpers::merge_files($backup_files_sql, $backup_file_master_sql, true);
         if ($archive == false) {
             return $full_path;
         } else {
             if (DashboardHelpers::zip_data(array($full_path), $backup_file_master_zip, $sourcedir_master_sql)) {
                 unlink($full_path);
                 return $backup_file_master_zip;
             }
         }
     } else {
         if ($archive == false) {
             return $backup_files_sql;
         } else {
             if (DashboardHelpers::zip_data($backup_files_sql, $backup_file_tables_zip, $sourcedir_tables_sql)) {
                 foreach ($backup_files_sql as $table_file) {
                     unlink($table_file);
                 }
                 return $backup_file_tables_zip;
             }
             //	$backup_file_tables_zip
         }
     }
 }