Page 1 of 1

SBMD - Backup of specific tables

Posted: 27 May 2016, 17:27
by Salazar
Hi Admin,

I would like to know, how can I use this script:
https://coursesweb.net/php-mysql/simple- ... atabase_s2
to make backup of specific tables and how to restore these specific tables? Do you have any examples code for this?

For example, in the code you provide on the website:

Code: Select all

//data for connecting to MySQL
$conn_data = ['host'=>'localhost', 'user'=>'user_name', 'pass'=>'password', 'dbname'=>'database_name'];

$lang ='en';  //indice of the "lang_...json" file with texts
$dir ='backup/';  //folder to store the ZIP archive with SQL backup

//set object of backupmysql class
include 'backupmysql.class.php';
$bk = new backupmysql($lang, $dir);

$bk->setMysql($conn_data);  //set connection
$tables = $bk->getTables();  //get array with all the tables in database

//if no error
if($bk->error === false){
  //if tables, creates the SQL backup of all the tables and saves it in ZIP archive (get succesful or error message)
  $bk_sql = (count($tables) >0) ? $bk->saveBkZip($tables) :'No tables in database';

  echo $bk_sql; 
}
else echo $bk->error; 
I am trying to do the same but for specific tables in my Database and I cannot figure out it. PLease see the code below:

Code: Select all

//data for connecting to MySQL
$conn_data = ['host'=>'localhost', 'user'=>'root', 'pass'=>'', 'dbname'=>'test'];

$lang ='en';  //indice of the "lang_...json" file with texts
$dir ='wp-content/themes/TEST/backups';  //folder to store the ZIP archive with SQL backup

$bk = new backupmysql($lang, $dir);

$bk->setMysql($conn_data);  //set connection

$important_tables = array('patients','patientrecords');

$tables = $bk->getSqlBackup($important_tables);  //get array with all the tables in database 
NOTE:At this point the class will return me a string and I cannot backup it in a Z.IP file or .SQL file because the next function for saving just use an array as input.

Code: Select all

//if no error
if($bk->error === false){
  //if tables, creates the SQL backup of all the tables and saves it in ZIP archive (get succesful or error message)
  $bk_sql = (count($tables) >0) ? $bk->saveBkZip($tables) :'No tables in database';

  echo $bk_sql; 
}
else echo $bk->error;
I would appreciate any advise.
By the way, it is an excellent work.
Kind regards

SBMD - Backup of specific tables

Posted: 27 May 2016, 18:39
by Admin
Hi,
Try this code:

Code: Select all

//data for connecting to MySQL
$conn_data = ['host'=>'localhost', 'user'=>'user_name', 'pass'=>'password', 'dbname'=>'database_name'];

$lang ='en';  //indice of the "lang_...json" file with texts
$dir ='backup/';  //folder to store the ZIP archive with SQL backup

//set object of backupmysql class
include 'backupmysql.class.php';
$bk = new backupmysql($lang, $dir);

$bk->setMysql($conn_data);  //set connection
$tables = array('patients','patientrecords');  //array with tables to backup

//if no error
if($bk->error === false){
  //if tables, creates the SQL backup of the tables and saves it in ZIP archive (get succesful or error message)
  $bk_sql = (count($tables) >0) ? $bk->saveBkZip($tables) :'No tables in database';

  echo $bk_sql; 
}
else echo $bk->error; 
- This will create a ZIP archive with the SQL backup of the tables: patients and patientrecords.
The zip archive will have a name with this format: backup-DATABASE_NAME-DAY-MONTH-YEAR@HOUR.MIN.SEC.sql.zip
For example: backup-cms-27-05-2016@08.11.40.sql.zip
To restore the backup of this zip archive, use:

Code: Select all

//data for connecting to MySQL
$conn_data = ['host'=>'localhost', 'user'=>'user_name', 'pass'=>'password', 'dbname'=>'database_name'];

$lang ='en';  //indice of the "lang_...json" file with texts
$dir ='backup/';  //folder to store the ZIP archive with SQL backup

//set object of backupmysql class
include 'backupmysql.class.php';
$bk = new backupmysql($lang, $dir);

$bk->setMysql($conn_data);  //set connection

//name of the zip arhive with backup to restore, as it is in 'backup/' folder
$zp ='backup-cms-27-05-2016@08.11.40.sql.zip';
$res = $bk->restore($zp); //restores the backup

echo $res; 

SBMD - Backup of specific tables

Posted: 31 May 2016, 14:52
by Salazar
Thanks Admin,

It worked like a charm!... really nice tool!

Regards,
Salazar