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()
{
try {
$this->transport->send();
if ($this->backup->isValid()) {
$this->transport->send();
} else {
throw new \Exception("Backup configuration is invalid.");
}
} catch (\Exception $e) {
throw $e;
}

View File

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

View File

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

View File

@ -9,14 +9,28 @@ class BackupAbstract
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();
}
/**
* Magic setter method
*
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->$name = $value;
}
/**
* @return array
*/
@ -43,19 +57,17 @@ class BackupAbstract
function addDate($format = 'Ymd')
{
$tmpFiles = array();
foreach($this->_filesToBackup as $file => $name)
{
foreach ($this->_filesToBackup as $file => $name) {
$nameA = explode('.', $name);
$nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format);
$nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream)
{
foreach ($this->_streamsToBackup as $name => $stream) {
$tmpStream[$name . '-' . date($format)] = $stream;
}
$this->_streamsToBackup = $tmpStream;
@ -73,19 +85,17 @@ class BackupAbstract
function addTime($format = 'his')
{
$tmpFiles = array();
foreach($this->_filesToBackup as $file => $name)
{
foreach ($this->_filesToBackup as $file => $name) {
$nameA = explode('.', $name);
$nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format);
$nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream)
{
foreach ($this->_streamsToBackup as $name => $stream) {
$tmpStream[$name . '-' . date($format)] = $stream;
}
$this->_streamsToBackup = $tmpStream;
@ -93,65 +103,28 @@ class BackupAbstract
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 backupToFTP($adress, $login = '', $pwd = '', $path ='/')
{
$ftp = new Shikiryu_Backup_FTP($adress, $login, $pwd, $path);
$ftp->setFiles($this->_filesToBackup)
->setStreams($this->_streamsToBackup)
->send();
}
/**
* @param $folder
*/
function backupToFolder($folder)
{
if (!empty($folder)) {
$folder = sprintf('%s/',rtrim($folder, '/'));
$folder = sprintf('%s/', rtrim($folder, '/'));
}
// if($folder != '')
// {
// if(substr($folder, 0, -1) != '/')
// $folder .= '/';
// }
foreach($this->_filesToBackup as $file => $name)
{
foreach ($this->_filesToBackup as $file => $name) {
copy($file, $folder . $name);
}
foreach($this->_streamsToBackup as $name => $file)
{
foreach ($this->_streamsToBackup as $name => $file) {
if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt';
}
@ -166,22 +139,28 @@ class BackupAbstract
*
* @return bool
*/
function checkMinimumFilesize($fs)
{
foreach($this->_filesToBackup as $file => $name)
{
if (filesize($file) < $fs) {
return false;
}
function checkMinimumFilesize($fs)
{
foreach ($this->_filesToBackup as $file => $name) {
if (filesize($file) < $fs) {
return false;
}
}
foreach($this->_streamsToBackup as $name => $file)
{
foreach ($this->_streamsToBackup as $name => $file) {
if (mb_strlen($file, 'utf-8') < $fs) {
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
* @throws \Exception
*/
function __construct($config = array())
public function __construct(array $config = array())
{
parent::__construct();
if (!isset($config['files'])) {
throw new \Exception('Files needs a "files" configuration.');
}
parent::__construct();
$filesToBackup = $config['files'];
if(!empty($filesToBackup) && is_array($filesToBackup)){
$names = array_map("basename",$filesToBackup);
@ -21,21 +21,17 @@ class Files extends BackupAbstract
}
}
/**
*
*
* @param string[] $filestobackup a list of file path
*
* @return $this
*/
function setFilePath($filesToBackup = array())
public function isValid()
{
if(!empty($filesToBackup) && is_array($filesToBackup))
{
$names = array_map("basename",$filesToBackup);
$this->_filesToBackup = array_combine($filesToBackup,$names);
$result = true;
foreach ($this->_filesToBackup as $file => $name) {
if (!file_exists($file)) {
$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 $_tables;
private $pdo;
private $tables;
private $host;
private $db;
private $login;
private $pwd;
/**
* @param $host
* @param $login
* @param $pwd
* @param $db
* @param array $config
*/
public function __construct($host, $login, $pwd, $db) {
parent::__construct();
$this->_tables = array();
$this->_pdo = new PDO('mysql:host='.$host.';dbname='.$db, $login, $pwd);
public function __construct(array $config = array()) {
parent::__construct($config);
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
$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
* @return $this|string
@ -49,7 +31,7 @@ class Mysql extends BackupAbstract
public function fromTables($tables = array())
{
if(!empty($tables)) {
$this->_tables = $tables;
$this->tables = $tables;
}
$this->_streamsToBackup[] = $this->getFromTables();
return $this;
@ -61,9 +43,9 @@ class Mysql extends BackupAbstract
* @return $this
*/
public function everything() {
$this->_tables = array();
foreach($this->_pdo->query('SHOW TABLES') as $table) {
$this->_tables[] = $table;
$this->tables = array();
foreach($this->pdo->query('SHOW TABLES') as $table) {
$this->tables[] = $table;
}
$this->_streamsToBackup[] = $this->getFromTables();
return $this;
@ -75,15 +57,15 @@ class Mysql extends BackupAbstract
private function getFromTables()
{
$return = "";
foreach ($this->_tables as $table) {
foreach ($this->tables as $table) {
if(is_array($table)) {
$table = $table[0];
}
$result = $this->_pdo->prepare('SELECT * FROM ' . $table);
$result = $this->pdo->prepare('SELECT * FROM ' . $table);
$result->execute();
$num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$result2 = $this->_pdo->prepare('SHOW CREATE TABLE ' . $table);
$result2 = $this->pdo->prepare('SHOW CREATE TABLE ' . $table);
$result2->execute();
$row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n";

View File

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