BACKUP TABLE OR DATABASE MySQL with PHP.
$backup = new SQL_Backup();
construct($con, $tablename, $folder, $querylimit, $compress, $ext, $alltableinfile, $save, $sqlunique)
var con (Object) => MySQLi connection already opened.
var table_name (String or Array) => The tables that you want to backup. READ TABLE SECTION
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 compress (Bool) => If set to true the result is compressed. (.zip)
var ext (String or Array) [NEW VERSION >= V1.0.7] => The extension of the destination file. READ EXT SECTION
var ext (Int) [OLD VERSION < V1.0.7] => The extension of the destination file. READ EXT SECTION
var alltable_in_file (Bool) => If set to true:
- If the 'compress' variable is true all the files will be saved in a single zip file otherwise all will be saved individually
- If the 'compress' variable is false all the files will be saved into a single folder (Depending on the extension) or else each file will be saved individually
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)
This Function is an simple MySQL connection (new mysqli())
EXAMPLE:
$HOST = 'localhost';
$USER = 'root';
$PASSWD = '';
$NAME = 'francesco';
Case : Connection already open...
$con = new mysqli($HOST,$USER,$PASSWD,$NAME);
$backup = new SQL_Backup($con);
Case : Connection closed....
$backup = new SQL_Backup();
$backup->con($HOST,$USER,$PASSWD,$NAME);
Run Command
Is critical . To run every time at the end!
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
$backup->folder = "backup/database"; /* ONLY DIR */
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;
If set to true the result is compressed. (.zip)
$backup->compress = true;
NEW VERSION >= V1.0.7
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)
- 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="'";
OLD VERSION < V1.0.7
The extension of the destination file.
const SQL = 13;
const CSV = 26;
const JSON = 49;
- SQL extension
$backup->ext = $backup::SQL;
$backup->ext = 13;
- CSV extension
$backup->ext = $backup::CSV;
$backup->ext = 26;
- JSON extension
$backup->ext = $backup::JSON;
$backup->ext = 49;
SQL + CSV
$backup->ext = $backup::SQL + $backup::CSV;
$backup->ext = 13 + 26; // 39
$backup->ext = array($backup::SQL, $backup::CSV);
$backup->ext = array(13,26);
$backup->ext = "39";
Same for JSON and CSV or JSON and SQL
For all three formats use ($n > 100)
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="'";
If set to TRUE:
-
If the 'compress' variable is true all the files will be saved in a single zip file otherwise all will be saved individually.
-
If the 'compress' variable is false all the files will be saved into a single folder (Depending on the extension. Example Choose SQL extension dir/sql/name_file) or else each file will be saved individually.
$backup->alltable_in_file = true;
If set to FALSE, the result will not be saved but will be loaded on the variable of Class sql, csv, json (ARRAY)
EXAMPLE
SQL:
object(SQL_Backup)[1]
...
public 'sql' =>
array (size=1)
'name_table' => string '...'
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.
Added:
If set to true, at the end of operations the MySQL connection is closed otherwise the connection will be allowed in the class.
If set to true, the output of the json will be like this:
{
"Info": {
"alldata": {
"R": 35954,
"C": 14
}
},
"Avg": 0.5024807643890381
}
Otherwise:
{"Info":{"alldata":{"R":35954,"C":14}},"Avg":0.5024807643890381}
If set to true, some information will be returned in the info array. (Look under)
Here it will be returned any errors or information.
MySQL Error, table columns and rows, etc.
Example:
{"alldata":{"R":35954,"C":14}
- Folder = "backup/database";
- Query Limit = 400
- Compress = true
- Extension = SQL
- Alltable_in_file = false
- Save = true
- Sql_unique = false
- Del= [,]
- Enc= []
- Close = null
- Info_t = null
- Json_pretty = 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