Added validators and new transport

This commit is contained in:
Shikiryu 2015-07-12 20:34:54 +02:00
parent 091dd4cf22
commit 8426972504
9 changed files with 165 additions and 138 deletions

View File

@ -42,7 +42,11 @@ class Scenario {
public function send() public function send()
{ {
try { try {
$this->transport->send(); if ($this->backup->isValid()) {
$this->transport->send();
} else {
throw new \Exception("Backup configuration is invalid.");
}
} catch (\Exception $e) { } catch (\Exception $e) {
throw $e; throw $e;
} }

View File

@ -235,6 +235,8 @@ class Email extends TransportAbstract
public function send() public function send()
{ {
// TODO check if file is empty
// Checking files are selected // Checking files are selected
$zip = new \ZipArchive(); // Load zip library $zip = new \ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name $zip_name = time().".zip"; // Zip name

View File

@ -4,26 +4,32 @@ namespace Shikiryu\Backup\Transport;
class Ftp extends TransportAbstract class Ftp extends TransportAbstract
{ {
private $path;
private $connection; private $connection;
protected $host;
protected $login;
protected $password;
protected $folder;
private $files; private $files;
private $streams; private $streams;
public function __construct($backup, $server, $login, $pwd, $path='/') { public function __construct($backup, $config) {
parent::__construct($backup); parent::__construct($backup, $config);
$this->path = $this->config['path']; // $this->path = $this->config['path'];
if (!empty($this->config['path'])) { if (!empty($this->folder)) {
$this->path = sprintf('/%s/', ltrim(rtrim($this->config['path'], '/'),'/')); $this->folder = sprintf('/%s/', ltrim(rtrim($this->folder, '/'),'/'));
} }
$this->connection = ftp_connect($this->config['host']); $this->connection = ftp_connect($this->host);
if ($this->connection == false) {
throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
}
$login = ftp_login($this->connection, $this->config['login'], $this->config['password']); $login = @ftp_login($this->connection, $this->login, $this->password);
if (!$this->connection || !$login) { if ($login === false) {
throw new Exception('Connexion FTP refusée.'); throw new \Exception(sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password));
} }
$this->setFiles($this->backup->getFilesToBackup()); $this->setFiles($this->backup->getFilesToBackup());
@ -32,27 +38,29 @@ class Ftp extends TransportAbstract
private function setFiles($files = array()) private function setFiles($files = array())
{ {
if (is_array($files) && !empty($files)) if (is_array($files) && !empty($files)) {
$this->files = $files; $this->files = $files;
}
return $this; return $this;
} }
private function setStreams($streams = array()) { private function setStreams($streams = array()) {
if (is_array($streams) && !empty($streams)) if (is_array($streams) && !empty($streams)) {
$this->streams = $streams; $this->streams = $streams;
}
return $this; return $this;
} }
public function send() public function send()
{ {
$sent = true; $sent = true;
ftp_pasv($this->connection, true);
if (!empty($this->files)){ if (!empty($this->files)){
foreach ($this->files as $file => $name) { foreach ($this->files as $file => $name) {
// TODO PASSIVE MODE $upload = ftp_put($this->connection, $this->folder.$name, $file, FTP_BINARY);
$upload = ftp_put($this->connection, $this->path.$name, $file, FTP_ASCII);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'FTP upload manquée de '.$file.' vers '.$this->path.$name; echo 'FTP upload manquée de '.$file.' vers '.$this->folder.$name;
} }
} }
} }
@ -62,15 +70,18 @@ class Ftp extends TransportAbstract
if (count(explode('.', $name)) < 2) if (count(explode('.', $name)) < 2)
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
file_put_contents($name, $stream); file_put_contents($name, $stream);
// TODO PASSIVE MODE $upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII);
$upload = ftp_put($this->connection, $this->path.$name, $name, FTP_ASCII);
if (!$upload) { if (!$upload) {
echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name; echo 'FTP upload manquée de '.$name.' vers '.$this->folder.$name;
$sent = false; $sent = false;
} }
unlink($name); unlink($name);
} }
} }
if (!$sent) {
throw new \Exception('At least an upload didnt work.');
}
} }
public function __destruct() public function __destruct()

View File

@ -0,0 +1,43 @@
<?php
namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
class Folder extends TransportAbstract
{
protected $folder;
public function __construct(BackupAbstract $backup, array $config = array())
{
parent::__construct($backup, $config);
if (!empty($this->folder)) {
$this->folder = sprintf('%s/', rtrim($this->folder, '/'));
}
}
/**
* @return bool
* @throws \Exception
*/
public function send()
{
foreach ($this->backup->getFilesToBackup() as $file => $name) {
if (copy($file, $this->folder . $name) === false) {
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
};
}
foreach ($this->backup->getStreamsToBackup() as $name => $file) {
if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt';
}
if (file_put_contents($this->folder . $name, $file) === false) {
throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder));
}
}
return true;
}
}

View File

@ -12,6 +12,10 @@ abstract class TransportAbstract
public function __construct(BackupAbstract $backup, array $config) public function __construct(BackupAbstract $backup, array $config)
{ {
foreach ($config as $name => $value) {
$this->$name = $value;
}
$this->config = $config; $this->config = $config;
$this->backup = $backup; $this->backup = $backup;
} }

View File

@ -9,14 +9,28 @@ class BackupAbstract
protected $_streamsToBackup; protected $_streamsToBackup;
/** /**
* * @param array $config
*/ */
function __construct() function __construct($config = array())
{ {
$this->_filesToBackup = array(); foreach ($config as $name => $value) {
$this->$name = $value;
}
$this->_filesToBackup = array();
$this->_streamsToBackup = array(); $this->_streamsToBackup = array();
} }
/**
* Magic setter method
*
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->$name = $value;
}
/** /**
* @return array * @return array
*/ */
@ -43,19 +57,17 @@ class BackupAbstract
function addDate($format = 'Ymd') function addDate($format = 'Ymd')
{ {
$tmpFiles = array(); $tmpFiles = array();
foreach($this->_filesToBackup as $file => $name) foreach ($this->_filesToBackup as $file => $name) {
{
$nameA = explode('.', $name); $nameA = explode('.', $name);
$nameA[] = end($nameA); $nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format); $nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA); $name = implode('.', $nameA);
$tmpFiles[$file] = $name; $tmpFiles[$file] = $name;
} }
$this->_filesToBackup = $tmpFiles; $this->_filesToBackup = $tmpFiles;
$tmpStream = array(); $tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream) foreach ($this->_streamsToBackup as $name => $stream) {
{
$tmpStream[$name . '-' . date($format)] = $stream; $tmpStream[$name . '-' . date($format)] = $stream;
} }
$this->_streamsToBackup = $tmpStream; $this->_streamsToBackup = $tmpStream;
@ -73,19 +85,17 @@ class BackupAbstract
function addTime($format = 'his') function addTime($format = 'his')
{ {
$tmpFiles = array(); $tmpFiles = array();
foreach($this->_filesToBackup as $file => $name) foreach ($this->_filesToBackup as $file => $name) {
{
$nameA = explode('.', $name); $nameA = explode('.', $name);
$nameA[] = end($nameA); $nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format); $nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA); $name = implode('.', $nameA);
$tmpFiles[$file] = $name; $tmpFiles[$file] = $name;
} }
$this->_filesToBackup = $tmpFiles; $this->_filesToBackup = $tmpFiles;
$tmpStream = array(); $tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream) foreach ($this->_streamsToBackup as $name => $stream) {
{
$tmpStream[$name . '-' . date($format)] = $stream; $tmpStream[$name . '-' . date($format)] = $stream;
} }
$this->_streamsToBackup = $tmpStream; $this->_streamsToBackup = $tmpStream;
@ -93,65 +103,28 @@ class BackupAbstract
return $this; return $this;
} }
/**
*
* @param $name string
* @param $args mixed
*
* @return bool
*/
public function __call($name, $args)
{
if (substr($name,0,8) == 'backupTo') {
$type = substr($name, 8);
return Shikiryu_Backup_Transport_Factory::getAndSend($type, $this, $args);
}
}
/*function backupToEmail($to, $from, $object, $mes)
{
$email = new Shikiryu_Backup_Email();
$email->addTo($to)
->setFrom($from)
->setSubject($object)
->setMessage($mes)
->setFiles($this->_filesToBackup)
->setStreams($this->_streamsToBackup);
return $email->send();
}*/
function backupToDropbox() function backupToDropbox()
{ {
} }
function backupToFTP($adress, $login = '', $pwd = '', $path ='/')
{
$ftp = new Shikiryu_Backup_FTP($adress, $login, $pwd, $path);
$ftp->setFiles($this->_filesToBackup)
->setStreams($this->_streamsToBackup)
->send();
}
/** /**
* @param $folder * @param $folder
*/ */
function backupToFolder($folder) function backupToFolder($folder)
{ {
if (!empty($folder)) { if (!empty($folder)) {
$folder = sprintf('%s/',rtrim($folder, '/')); $folder = sprintf('%s/', rtrim($folder, '/'));
} }
// if($folder != '') // if($folder != '')
// { // {
// if(substr($folder, 0, -1) != '/') // if(substr($folder, 0, -1) != '/')
// $folder .= '/'; // $folder .= '/';
// } // }
foreach($this->_filesToBackup as $file => $name) foreach ($this->_filesToBackup as $file => $name) {
{
copy($file, $folder . $name); copy($file, $folder . $name);
} }
foreach($this->_streamsToBackup as $name => $file) foreach ($this->_streamsToBackup as $name => $file) {
{
if (count(explode('.', $name)) < 2) { if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
} }
@ -166,22 +139,28 @@ class BackupAbstract
* *
* @return bool * @return bool
*/ */
function checkMinimumFilesize($fs) function checkMinimumFilesize($fs)
{ {
foreach($this->_filesToBackup as $file => $name) foreach ($this->_filesToBackup as $file => $name) {
{ if (filesize($file) < $fs) {
if (filesize($file) < $fs) { return false;
return false; }
}
} }
foreach($this->_streamsToBackup as $name => $file) foreach ($this->_streamsToBackup as $name => $file) {
{
if (mb_strlen($file, 'utf-8') < $fs) { if (mb_strlen($file, 'utf-8') < $fs) {
return false; return false;
} }
} }
return true; return true;
} }
/**
* @return bool
*/
public function isValid()
{
return true;
}
} }

View File

@ -8,12 +8,12 @@ class Files extends BackupAbstract
* @param array $config * @param array $config
* @throws \Exception * @throws \Exception
*/ */
function __construct($config = array()) public function __construct(array $config = array())
{ {
parent::__construct();
if (!isset($config['files'])) { if (!isset($config['files'])) {
throw new \Exception('Files needs a "files" configuration.'); throw new \Exception('Files needs a "files" configuration.');
} }
parent::__construct();
$filesToBackup = $config['files']; $filesToBackup = $config['files'];
if(!empty($filesToBackup) && is_array($filesToBackup)){ if(!empty($filesToBackup) && is_array($filesToBackup)){
$names = array_map("basename",$filesToBackup); $names = array_map("basename",$filesToBackup);
@ -21,21 +21,17 @@ class Files extends BackupAbstract
} }
} }
/** public function isValid()
*
*
* @param string[] $filestobackup a list of file path
*
* @return $this
*/
function setFilePath($filesToBackup = array())
{ {
if(!empty($filesToBackup) && is_array($filesToBackup)) $result = true;
{ foreach ($this->_filesToBackup as $file => $name) {
$names = array_map("basename",$filesToBackup); if (!file_exists($file)) {
$this->_filesToBackup = array_combine($filesToBackup,$names); $result = false;
break;
}
} }
return $this; return $result;
} }
} }
?> ?>

View File

@ -6,42 +6,24 @@ class Mysql extends BackupAbstract
{ {
/** /**
* @var $_pdo PDO * @var $pdo \PDO
*/ */
private $_pdo; private $pdo;
private $_tables; private $tables;
private $host;
private $db;
private $login;
private $pwd;
/** /**
* @param $host * @param array $config
* @param $login
* @param $pwd
* @param $db
*/ */
public function __construct($host, $login, $pwd, $db) { public function __construct(array $config = array()) {
parent::__construct(); parent::__construct($config);
$this->_tables = array(); empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
$this->_pdo = new PDO('mysql:host='.$host.';dbname='.$db, $login, $pwd); $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->login, $this->pwd);
} }
/**
* set the list of table to backup
*
* @param array $tables
*
* @return $this
*/
public function setTables(array $tables)
{
if(is_array($tables) && !empty($tables)) {
$this->_tables = $tables;
}
return $this;
}
// function withSQL($sql) {
// $statement = $this->_pdo->query($sql);
// }
/** /**
* @param array $tables * @param array $tables
* @return $this|string * @return $this|string
@ -49,7 +31,7 @@ class Mysql extends BackupAbstract
public function fromTables($tables = array()) public function fromTables($tables = array())
{ {
if(!empty($tables)) { if(!empty($tables)) {
$this->_tables = $tables; $this->tables = $tables;
} }
$this->_streamsToBackup[] = $this->getFromTables(); $this->_streamsToBackup[] = $this->getFromTables();
return $this; return $this;
@ -61,9 +43,9 @@ class Mysql extends BackupAbstract
* @return $this * @return $this
*/ */
public function everything() { public function everything() {
$this->_tables = array(); $this->tables = array();
foreach($this->_pdo->query('SHOW TABLES') as $table) { foreach($this->pdo->query('SHOW TABLES') as $table) {
$this->_tables[] = $table; $this->tables[] = $table;
} }
$this->_streamsToBackup[] = $this->getFromTables(); $this->_streamsToBackup[] = $this->getFromTables();
return $this; return $this;
@ -75,15 +57,15 @@ class Mysql extends BackupAbstract
private function getFromTables() private function getFromTables()
{ {
$return = ""; $return = "";
foreach ($this->_tables as $table) { foreach ($this->tables as $table) {
if(is_array($table)) { if(is_array($table)) {
$table = $table[0]; $table = $table[0];
} }
$result = $this->_pdo->prepare('SELECT * FROM ' . $table); $result = $this->pdo->prepare('SELECT * FROM ' . $table);
$result->execute(); $result->execute();
$num_fields = $result->columnCount(); $num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';'; $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$result2 = $this->_pdo->prepare('SHOW CREATE TABLE ' . $table); $result2 = $this->pdo->prepare('SHOW CREATE TABLE ' . $table);
$result2->execute(); $result2->execute();
$row2 = $result2->fetch(); $row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n"; $return.= "\n\n" . $row2[1] . ";\n\n";

View File

@ -10,7 +10,10 @@
"host" : "mysql address", "host" : "mysql address",
"login" : "mysql login", "login" : "mysql login",
"pwd" : "mysql password", "pwd" : "mysql password",
"db" : "mysql database" "db" : "mysql database",
"tables": [
""
]
} }
}, },
"transport": { "transport": {
@ -26,6 +29,9 @@
"login" : "login", "login" : "login",
"password" : "password", "password" : "password",
"folder" : "/folder" "folder" : "/folder"
},
"Folder": {
} }
} }
} }