PHP class to move database files (like MYD, MYI and frm)
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Move Database Files (like MYD, MYI and frm) | |
|-------------------------------------------------------------------------- | |
| | |
| $config = array( | |
| 'tables' => array('table1','table2'), // array of tables / filenames to move | |
| 'extensions' => array('MYD','MYI','frm'), // required extensions for each database | |
| 'from' => dirname(__DIR__).'/transfer', // from location of files | |
| 'to' => '/var/lib/mysql/mydatabase', // location to move | |
| 'store' => dirname(__DIR__).'/transfer/store', // optional storage location (if empty original files will be left in 'from' location) | |
| 'chown' => 'mysql' // option username of the database files (will likely be 'mysql' for most linux systems) | |
| ); | |
| | |
| | |
*/ | |
class move_db | |
{ | |
function __construct(array $config) | |
{ | |
$this->tables = $config['tables']; | |
$this->dir_from = rtrim($config['from'],DIRECTORY_SEPARATOR); | |
$this->dir_to = rtrim($config['to'],DIRECTORY_SEPARATOR); | |
$this->dir_store= rtrim($config['store'],DIRECTORY_SEPARATOR); | |
$this->chown = $config['chown']; | |
$this->ext = $config['extensions']; | |
$this->files = self::files(); | |
} | |
/** | |
* Returns list of files | |
* (list of files based on 'from' variable and filters by extensions) | |
* @return array of files available | |
*/ | |
function files() | |
{ | |
$return = array(); | |
$files = glob($this->dir_from.DIRECTORY_SEPARATOR.'{'.implode(',', $this->tables).'}.{'.implode(',',$this->ext).'}',GLOB_BRACE); | |
foreach($files as $file) | |
{ | |
$path = pathinfo($file); | |
$return[] = $path['filename']; | |
} | |
$return = array_unique($return); | |
// check if all 3 extensions exist | |
foreach($return as $k => $v) | |
{ | |
foreach($this->ext as $e) | |
{ | |
if(!file_exists($this->dir_from.DIRECTORY_SEPARATOR.''.$v.'.'.$e)){ | |
unset($return[$k]); | |
} | |
} | |
} | |
sort($return); | |
return $return; | |
} | |
/** | |
* Moves files to destination | |
* (Optional: also changes owner and moves files into storage location) | |
*/ | |
function move() | |
{ | |
$count = count($this->files); | |
for($x=0;$x<$count;$x++) | |
{ | |
foreach($this->ext as $ext) | |
{ | |
$from = $this->dir_from.DIRECTORY_SEPARATOR.''.$this->files[$x].'.'.$ext; | |
$to = $this->dir_to.DIRECTORY_SEPARATOR.''.str_replace(" ", "_", $this->files[$x]).'.'.$ext; | |
if(file_exists($from)) | |
{ | |
// copy file | |
copy($from,$to); | |
// change file owner | |
if(isset($this->chown) AND !empty($this->chown)){ | |
chown($to, $this->chown); | |
} | |
// move file to storage directory | |
if(!empty($this->dir_store) AND is_dir($this->dir_store)){ | |
rename($from,$this->dir_store.'/'.str_replace(" ", "_", $this->files[$x]).'.'.$ext); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Runs the functions needed for transfer | |
*/ | |
function run() | |
{ | |
$files = self::files(); | |
if(count($files) > 0) | |
{ | |
self::move(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment