Skip to content

Instantly share code, notes, and snippets.

@zach-adams
Last active June 22, 2016 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zach-adams/5c32775bf6c38c0988750c4ca9aab50f to your computer and use it in GitHub Desktop.
Save zach-adams/5c32775bf6c38c0988750c4ca9aab50f to your computer and use it in GitHub Desktop.
Backup Manager with Include/Ignore tables
<?php
use BackupManager\Databases\DatabaseProvider;
use BackupManager\Config\Config;
use App\Backups\MysqlDatabase;
$include_example_database = new DatabaseProvider(new Config([
'Local' => [
'type' => 'mysql',
'host' => 'localhost',
'user' => 'user',
'pass' => 'password',
'database' => 'database',
'include_tables' => [
'table1',
'table2',
'table3'
],
],
]));
$exclude_example_database = new DatabaseProvider(new Config([
'Local' => [
'type' => 'mysql',
'host' => 'localhost',
'user' => 'user',
'pass' => 'password',
'database' => 'database',
'exclude_tables' => [
'table1',
'table2',
'table3'
],
],
]));
$include_example_database->add(new MysqlDatabase);
$exclude_example_database->add(new MysqlDatabase);
<?php namespace App\Backups;
use BackupManager\Databases\Database;
/**
* Class MysqlDatabase
* @package BackupManager\Databases
*/
class MysqlDatabase implements Database {
/** @var array */
private $config;
/**
* @param $type
* @return bool
*/
public function handles($type) {
return strtolower($type) == 'mysql';
}
/**
* @param array $config
* @return null
*/
public function setConfig(array $config) {
$this->config = $config;
}
/**
* @param $outputPath
* @return string
*/
public function getDumpCommandLine($outputPath) {
$database = $this->config['database'];
/**
* Note, this was hastily written and can certainly be improved upon.
*/
$tables = '';
if(isset($this->config['include_tables'])) {
$tables = implode(' ', $this->config['include_tables']);
$tables = str_replace("'", "", escapeshellarg($tables));
} elseif(isset($this->config['exclude_tables'])) {
$tables = implode(' ', array_map(function($table) use ($database) { return "--ignore-table=$database.". $table; }, $this->config['exclude_tables']));
$tables = str_replace("'", "", escapeshellarg($tables));
}
$command = sprintf('mysqldump --routines --host=%s --port=%s --user=%s --password=%s %s %s > %s',
escapeshellarg($this->config['host']),
escapeshellarg($this->config['port']),
escapeshellarg($this->config['user']),
escapeshellarg($this->config['pass']),
escapeshellarg($database),
$tables,
escapeshellarg($outputPath)
);
return $command;
}
/**
* @param $inputPath
* @return string
*/
public function getRestoreCommandLine($inputPath) {
return sprintf('mysql --host=%s --port=%s --user=%s --password=%s %s -e "source %s"',
escapeshellarg($this->config['host']),
escapeshellarg($this->config['port']),
escapeshellarg($this->config['user']),
escapeshellarg($this->config['pass']),
escapeshellarg($this->config['database']),
$inputPath
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment