Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Added include and exlude database tables options
Browse files Browse the repository at this point in the history
  • Loading branch information
jgulledge19 committed Sep 29, 2011
1 parent d7730f4 commit 207f81c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 13 deletions.
52 changes: 46 additions & 6 deletions core/components/databackup/elements/snippets/snippet.backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,58 @@
$path = $modx->getOption('core_path').'components/databackup/';
require_once $path.'model/mysql/dbbackup.class.php';

/*
* Need to create purge date and data folder options
*/

$output = '';
// back up my modx database:
$data_folder = $modx->getOption('databackup.folder', $scriptProperties, $path.'dumps/');
$purge_time = $modx->getOption('databackup.pruge', $scriptProperties, 1814400);
// includeTables should be a comma separtaed list
$includeTables = $modx->getOption('includeTables', $scriptProperties, NULL);
// excludeTables should be a comma separtaed list
$excludeTables = $modx->getOption('excludeTables', $scriptProperties, NULL);

$write_file = $modx->getOption('writeFile', $scriptProperties, true);
if ( $write_file === 'false' ) {
$write_file = false;
$output .= ' <br>Do not write main file<br>';
}
$write_table_files = $modx->getOption('writeTableFiles', $scriptProperties, true);
if ( $write_table_files === 'false' ) {
$write_table_files = false;
$output .= ' <br>Do not write table files<br>';
}
// these are to change how the data file is written
$comment_prefix = $modx->getOption('commentPrfeix', $scriptProperties, '--');
$comment_suffix = $modx->getOption('commentSuffix', $scriptProperties, '');
$new_line = $modx->getOption('newLine', $scriptProperties, "\n");
// use the sql drop command
$use_drop = $modx->getOption('useDrop', $scriptProperties, true);
if ( $use_drop === 'false' ) {
$use_drop = false;
}
$database = $modx->getOption('database', $scriptProperties, 'modx');
// use the sql create database command
$create_database = $modx->getOption('createDatabase', $scriptProperties, false);
if ( $create_database === 'false' ) {
$create_database = false;
}

$db = new DBBackup($modx,array('base_path' => $data_folder ) );
$db = new DBBackup($modx,
array(
'comment_prefix' => $comment_prefix,
'comment_suffix' => $comment_suffix,
'new_line' => $new_line,
'base_path' => $data_folder,
'write_file' => $write_file,
'write_table_files' => $write_table_files,
'use_drop' => $use_drop,
'database' => $database,
'create_database' => $create_database,
'includeTables' => $includeTables,
'excludeTables' => $excludeTables,

));

$backup = $db->backup();
$output = '';
if($backup){
$output .= 'The MODX data has been back up';
} else {
Expand Down
103 changes: 96 additions & 7 deletions core/components/databackup/model/mysql/dbbackup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,37 @@
* @var Array
*/
protected $config = array();
/**
*
* To include only these tables
* @var Array
*/
protected $includeTables = array();
/**
*
* True include only these tables, false don't use include
* @var boolean
*/
protected $useIncludeTables = false;
/**
*
* To exclude only these tables
* @var Array
*/
protected $excludeTables = array();
/**
*
* True exclude only these tables, false don't use exclude
* @var boolean
*/
protected $useExcludeTables = false;
/**
*
* List of files that are written, folder => path, database => path, tables => array( names => path)
* @var Array
*/
protected $filePathData = array();
/**
*
* The main function
* @method DBBackup
Expand All @@ -100,7 +130,9 @@ public function __construct($modx, $config=array()){
'use_drop' => true,
'connect' => false,
'database' => 'modx',
'create_database' => false
'create_database' => false,
'includeTables' => null,
'excludeTables' => null
);


Expand Down Expand Up @@ -140,6 +172,14 @@ public function __construct($modx, $config=array()){
} else {
$this->dbName = $this->config['database'];
}
// set the include/exclude if any
if ( !empty($this->config['includeTables']) ) {
$this->includeTables = explode(',',$this->config['includeTables']);
$this->useIncludeTables = true;
} elseif ( !empty($this->config['excludeTables']) ) {
$this->excludeTables = explode(',',$this->config['excludeTables']);
$this->useExcludeTables = true;
}
}

/**
Expand All @@ -161,12 +201,47 @@ public function backup(){
}
return true;
}
/**
* @description returns the folder/directory path that was created on for the backup files
* @return string
*/
public function folderPath() {
if ( isset($this->filePathData['folder'])) {
return $this->filePathData['folder'];
}
return null;
}
/**
* @description returns the database file path that was created on for the backup
* @return string
*/
public function DBFilePath() {
if ( isset($this->filePathData['database'])) {
return $this->filePathData['database'];
}
return null;
}
/**
* @description returns the database table file path that was created on for the backup
* @param (string) the full table name
* @return string
*/
public function tableFilePath($table) {
if ( isset($this->filePathData['tables'][$table])) {
return $this->filePathData['tables'][$table];
}
return null;
}
/**
* Get the errors
* @return string
*/
public function getErrors(){
return implode(', ', $this->error);
}
/**
* Purge file records
*
* @return void
*/
public function purge($seconds=1814400){// 21 days is the default

Expand Down Expand Up @@ -237,8 +312,7 @@ protected function _connect(){
return false;
}
}

/**
/**
*
* Generate backup string
* @uses Private use
Expand All @@ -252,8 +326,9 @@ protected function _generate(){
$this->final = $this->config['comment_prefix'].'RESTORING TABLES '.$tbl['name'].$this->config['comment_suffix'].$this->config['new_line'];
}
// create base folder - DB_backup_time()
if ( $this->config['write_file'] ) {
if ( $this->config['write_file'] || $this->config['write_table_files'] ) {
$dir = $this->config['base_path'].''.$this->dbName.'_'.date('Y_m_d').'__'.time().'/';
$this->filePathData['folder'] = $dir;
if( !is_dir($dir) ){
mkdir($dir);
}
Expand All @@ -264,14 +339,16 @@ protected function _generate(){
$table_sql .= $this->config['comment_prefix'].'INSERTING DATA INTO '.$tbl['name'].$this->config['comment_suffix'].$this->config['new_line'];
$table_sql .= $tbl['data'].$this->config['new_line'].$this->config['new_line'].$this->config['new_line'];
$this->final .= $table_sql;
// write to file
if ( $this->config['write_file'] && $this->config['write_table_files'] ) {
// write table to file
if ( $this->config['write_table_files'] ) {
file_put_contents($dir.$tbl['name'].'.sql', $table_sql );
$this->filePathData['tables'][$tbl['name']] = $dir.$tbl['name'].'.sql';
}
}
$this->final .= $this->config['comment_prefix'].' THE END'.$this->config['new_line'].$this->config['comment_suffix'].$this->config['new_line'];
if ( $this->config['write_file'] ) {
file_put_contents($dir.'complete_db_backup.sql', $this->final );
$this->filePathData['database'] = $dir.'complete_db_backup.sql';
}
}
/**
Expand All @@ -285,6 +362,18 @@ protected function _getTables(){
$tbs = $stmt->fetchAll();
$i=0;
foreach($tbs as $table){
//echo '<br>Table: '. $table[0];
if ( $this->useIncludeTables ) {
//echo ' - useIncludes';
if ( !in_array($table[0],$this->includeTables)) {
//echo ' - exclude me';
continue;
}
} elseif ( $this->useExcludeTables ) {
if ( in_array($table[0],$this->excludeTables)) {
continue;
}
}
$this->tables[$i]['name'] = $table[0];
$this->tables[$i]['create'] = $this->_getColumns($table[0]);
$this->tables[$i]['data'] = $this->_getData($table[0]);
Expand Down

0 comments on commit 207f81c

Please sign in to comment.