Skip to content

BACKUP TABLE OR DATABASE MySQL with PHP.

License

Notifications You must be signed in to change notification settings

chak10/Backup-SQL-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backup-SQL-By-Chak10 (BETA V1.1.4)

New BACKUP TABLE OR DATABASE MySQL with PHP.

 function __construct($con = null, $table_name = null, $ext = null, $fname = null, $folder = null, $query_limit = null, $archive = null, $phpmyadmin = null, $save = null, $sql_unique = null, $down = null, $header_name = null, $del_csv = null, $enc_csv = null, $json_options = null) {}

var con (Object) => MySQLi or PDO connection already opened. (N.B. It is recommended that you use the connection to the database inside the class) READ CON SECTION

var table_name (String or Array) => The tables that you want to backup. READ TABLE SECTION

var ext (String or Array) => The extension of the destination file. READ EXT SECTION

var fname (String) => The name of the output file/directory secondary

var folder (String) => The folder where the files will be saved

var query_limit (Int) => Number of queries at a time to execute in SQL READ QUERY LIMIT SECTION

var archive (Bool) => It results as an archive. (.zip|.tar)

var phpadmin (Bool) => If set to true, it creates files that can be imported directly with phpmyadmin. (sql|csv)

var save (Bool) => If set to false, the result will not be saved but will be loaded on the variable of class sql, csv, json (Based on request) READ SAVE SECTION

var sql_unique (Bool) => If set to true the SQL dump is a single file with all the tables. (Valid only for the SQL format)

var down (Bool) => If set to true, the output is automatic downloaded. (zip)

Execution Times

USE

$backup = new SQL_Backup();

con()

/**
* @var $HOST string The MySQL host name.
* @var $USER string The MySQL user name.
* @var $PASSWD string The MySQL password.
* @var $NAME string The MySQL database name.
* @var $PORT int The port number to use when connecting to the database server otherwise it uses the default port
* @var $SOCK string The socket name to use when connecting to a local database server otherwise it uses the default socket.
* @return Bool | object
**/

public function con($HOST, $USER, $PASSWD, $NAME, $PORT = null, $SOCK = null) {}

This feature based on the extensions present uses the most appropriate connection (MYSQLi|PDO)

EXAMPLE:

$HOST = 'localhost';
$USER = 'root';
$PASSWD = '';
$NAME = 'francesco';

Case : Connection already open...

$con = new mysqli($HOST,$USER,$PASSWD,$NAME); // or PDO
$backup = new SQL_Backup($con); 

Case : Connection closed....

$backup = new SQL_Backup();
$backup->con($HOST,$USER,$PASSWD,$NAME);

execute()

/**
* @var $debug Bool (True|False)
* @return Bool | Array
**/
public function execute($debug = false) {}

Run Command

If $debug does not coincide (===) with true the result will be:

  • If $save is true and there are no errors return true otherwise return false
  • If $save is false and there are no errors return array with tables READ SAVE SECTION otherwise return false

If $debug coincides (===) with true the result will be an array with all the class variables.

EXAMPLE:

$backup = new SQL_Backup(...);
$res = $backup->execute();
var_dump($res);

Is critical . To run every time at the end!

$ table_name

The tables that you want to backup.

EXAMPLE:

String:

$backup->table_name = "users,alldata";

Array:

$backup->table_name = array('users','alldata');

If not set, it will backup all the database

$folder

$backup->folder = "backup/database"; /* ONLY DIR */

$query_limit

Number of queries at a time to execute in SQL.

Exemple q_limit = 400

INSERT INTO table ( '' ,'' ,'') VALUES ('' ,'' ,'') ,('' ,'' ,'') ,('' ,'' ,'') ...... * 400 ,('' ,'' ,''); INSERT INTO table ( '' ,'' ,'') VALUES ('' ,'' ,'') ,('' ,'' ,'') ,('' ,'' ,'') ...... * 400 ,('' ,'' ,''); ..... etc.

$backup->qlimit=400;

$archive

If set to true the result is an archive. (.zip|.tar)

N.B. Zip is compressed - Tar is not compressed.

$backup->archive = false;
// or
$backup->archive = 'tar';
// or
$backup->archive = 'zip';

More info

$ext

The extension of the destination file.

  • SQL extension
$backup->ext = "sql";

// or 

$backup->ext = "SQL";

// or 

$backup->ext = array("sql");

// or 

$backup->ext = array("SQL");
  • CSV extension
$backup->ext = "csv";

and other ... (Look SQL)

  • JSON extension
$backup->ext = "json";

and other ... (Look SQL)

Combinations

  • SQL + CSV ​
$backup->ext = "sql,csv"; // Only the comma as a separator. !!!

// or

$backup->ext = array("sql","csv");

Same for JSON and CSV or JSON and SQL

For all three formats

  • SQL + CSV + JSON ​
$backup->ext = "all";

// or

$backup->ext = array("all");

// or

$backup->ext = "sql,csv,json"; // Only the comma as a separator. !!!

// or

$backup->ext = array("sql","csv","json");

NOTE: If you use the CSV extension, if you want you can add the field delimiter and the enclosure of the camps.
By default they are:

  • Delimiter => ,
  • Enclosure =>

Example: Data,DATA2,datA3

SETTING

$backup->del_csv=";";
$backup->enc_csv="'";

$phpmyadmin

If set to true it gives a result that can be imported directly with phpmyadmin (sql|csv)

$backup->phpmyadmin = true;

$save

If set to FALSE, the result will not be saved but will be loaded on the variable of Class sql, csv, json (ARRAY)

EXAMPLE

JSON:

array (size=1)
  'json' => 
    array (size=1)
      'airports' => string '[{"id":"6523","ident":"00A","type":"heliport","name":"Total Rf Heliport","latitude_deg":"40.07080078125","longitude_deg":"-74.93360137939453","elevation_ft":"11","continent":"NA","iso_country":"US","iso_region":"US-PA","municipality":"Bensalem","scheduled_service":"no","gps_code":"00A","iata_code":"","local_code":"00A","home_link":"","wikipedia_link":"","keywords":""},{"id":"323361","ident":"00AA","type":"small_airport","name":"Aero B Ranch Airport","latitude_deg":"38.704022","longitude_deg":"-101.473911","'... (length=20358868)

$sql_unique

If set to true the SQL dump is a single file with all the tables. (Valid only for the SQL format)

Table 1 SQL + Table 2 SQL + ETC.

$json_option

The classic options that can be used with json_encode.

README

EXAMPLE

$backup->json_option = JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES;
{
    "Info": {
        "alldata": {
            "R": 35954,
            "C": 14
        }
    },
    "Avg": 0.5024807643890381
}

DEFAULT SETTING

  • Folder = "backup/database";
  • Query Limit = 400
  • Archive = zip
  • Extension = SQL
  • Phpmyadmin = false
  • Save = true
  • Sql_unique = false
  • Del= [,]
  • Enc= []
  • Json_option = null
  • Down = null

Note: Attention this class has not been tested in all possible situations. So if you have problems you disclose them to me..

Note 2: I do not take responsibility in case of failure to backup or if the result you do not like or does not work