mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
Going namespaced, going scenario style
This commit is contained in:
44
app/code/Scenario.php
Normal file
44
app/code/Scenario.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup;
|
||||
|
||||
use Shikiryu\Backup\Backup\Factory as BackupFactory;
|
||||
use Shikiryu\Backup\Transport\Factory as TransportFactory;
|
||||
|
||||
class Scenario {
|
||||
|
||||
private $backup;
|
||||
private $to;
|
||||
|
||||
/**
|
||||
* @param array $scenario
|
||||
*/
|
||||
private function __construct(array $scenario)
|
||||
{
|
||||
$this->backup = BackupFactory::build($scenario['backup']);
|
||||
$this->to = TransportFactory::build($scenario['to']);
|
||||
}
|
||||
|
||||
|
||||
public static function launch($scenario)
|
||||
{
|
||||
if (is_readable($scenario)) {
|
||||
$scenario = json_decode(file_get_contents($scenario), true);
|
||||
if (static::isValid($scenario)) {
|
||||
$scenario = new self($scenario);
|
||||
}
|
||||
throw new \Exception('invalid scenario.');
|
||||
}
|
||||
throw new \Exception('scenario not found.');
|
||||
}
|
||||
|
||||
public static function isValid(\StdClass $scenario)
|
||||
{
|
||||
return
|
||||
isset($scenario->backup) &&
|
||||
count($scenario->backup) === 1 &&
|
||||
isset($scenario->to) &&
|
||||
count($scenario->to) === 1;
|
||||
}
|
||||
|
||||
}
|
26
app/code/Transport/Abstract.php
Normal file
26
app/code/Transport/Abstract.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
use Shikiryu\Backup\Backup\BackupAbstract;
|
||||
|
||||
abstract class TransportAbstract
|
||||
{
|
||||
|
||||
protected $backup;
|
||||
protected $config;
|
||||
|
||||
public function __construct(BackupAbstract $backup)
|
||||
{
|
||||
$config = parse_ini_file(dirname(__FILE__).'/../config/config.ini');
|
||||
$classname = get_class($this);
|
||||
$type = substr($classname, strrpos($classname, '_')+1);
|
||||
$this->config = $config[ucfirst(strtolower($type))];
|
||||
$this->backup = $backup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public abstract function send();
|
||||
}
|
290
app/code/Transport/Email.php
Normal file
290
app/code/Transport/Email.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
class Email extends TransportAbstract
|
||||
{
|
||||
|
||||
private $to;
|
||||
private $from;
|
||||
private $encoding;
|
||||
private $subject;
|
||||
private $message;
|
||||
private $files;
|
||||
private $streams;
|
||||
public static $mimeTypes = array(
|
||||
'txt' => 'text/plain',
|
||||
'htm' => 'text/html',
|
||||
'html' => 'text/html',
|
||||
'xhtml' => 'application/xhtml+xml',
|
||||
'xht' => 'application/xhtml+xml',
|
||||
'php' => 'text/html',
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'json' => 'application/json',
|
||||
'xml' => 'application/xml',
|
||||
'xslt' => 'application/xslt+xml',
|
||||
'xsl' => 'application/xml',
|
||||
'dtd' => 'application/xml-dtd',
|
||||
'atom' => 'application/atom+xml',
|
||||
'mathml' => 'application/mathml+xml',
|
||||
'rdf' => 'application/rdf+xml',
|
||||
'smi' => 'application/smil',
|
||||
'smil' => 'application/smil',
|
||||
'vxml' => 'application/voicexml+xml',
|
||||
'latex' => 'application/x-latex',
|
||||
'tcl' => 'application/x-tcl',
|
||||
'tex' => 'application/x-tex',
|
||||
'texinfo' => 'application/x-texinfo',
|
||||
'wrl' => 'model/vrml',
|
||||
'wrml' => 'model/vrml',
|
||||
'ics' => 'text/calendar',
|
||||
'ifb' => 'text/calendar',
|
||||
'sgml' => 'text/sgml',
|
||||
'htc' => 'text/x-component',
|
||||
// images
|
||||
'png' => 'image/png',
|
||||
'jpe' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'jpg' => 'image/jpeg',
|
||||
'gif' => 'image/gif',
|
||||
'bmp' => 'image/bmp',
|
||||
'ico' => 'image/x-icon',
|
||||
'tiff' => 'image/tiff',
|
||||
'tif' => 'image/tiff',
|
||||
'svg' => 'image/svg+xml',
|
||||
'svgz' => 'image/svg+xml',
|
||||
'djvu' => 'image/vnd.djvu',
|
||||
'djv' => 'image/vnd.djvu',
|
||||
// archives
|
||||
'zip' => 'application/zip',
|
||||
'rar' => 'application/x-rar-compressed',
|
||||
'exe' => 'application/x-msdownload',
|
||||
'msi' => 'application/x-msdownload',
|
||||
'cab' => 'application/vnd.ms-cab-compressed',
|
||||
'tar' => 'application/x-tar',
|
||||
'gz' => 'application/x-gzip',
|
||||
'tgz' => 'application/x-gzip',
|
||||
// audio/video
|
||||
'mp2' => 'audio/mpeg',
|
||||
'mp3' => 'audio/mpeg',
|
||||
'qt' => 'video/quicktime',
|
||||
'mov' => 'video/quicktime',
|
||||
'mpeg' => 'video/mpeg',
|
||||
'mpg' => 'video/mpeg',
|
||||
'mpe' => 'video/mpeg',
|
||||
'wav' => 'audio/wav',
|
||||
'aiff' => 'audio/aiff',
|
||||
'aif' => 'audio/aiff',
|
||||
'avi' => 'video/msvideo',
|
||||
'wmv' => 'video/x-ms-wmv',
|
||||
'ogg' => 'application/ogg',
|
||||
'flv' => 'video/x-flv',
|
||||
'dvi' => 'application/x-dvi',
|
||||
'au' => 'audio/basic',
|
||||
'snd' => 'audio/basic',
|
||||
'mid' => 'audio/midi',
|
||||
'midi' => 'audio/midi',
|
||||
'm3u' => 'audio/x-mpegurl',
|
||||
'm4u' => 'video/vnd.mpegurl',
|
||||
'ram' => 'audio/x-pn-realaudio',
|
||||
'ra' => 'audio/x-pn-realaudio',
|
||||
'rm' => 'application/vnd.rn-realmedia',
|
||||
// adobe
|
||||
'pdf' => 'application/pdf',
|
||||
'psd' => 'image/vnd.adobe.photoshop',
|
||||
'ai' => 'application/postscript',
|
||||
'eps' => 'application/postscript',
|
||||
'ps' => 'application/postscript',
|
||||
'swf' => 'application/x-shockwave-flash',
|
||||
// ms office
|
||||
'doc' => 'application/msword',
|
||||
'docx' => 'application/msword',
|
||||
'rtf' => 'application/rtf',
|
||||
'xls' => 'application/vnd.ms-excel',
|
||||
'xlm' => 'application/vnd.ms-excel',
|
||||
'xla' => 'application/vnd.ms-excel',
|
||||
'xld' => 'application/vnd.ms-excel',
|
||||
'xlt' => 'application/vnd.ms-excel',
|
||||
'xlc' => 'application/vnd.ms-excel',
|
||||
'xlw' => 'application/vnd.ms-excel',
|
||||
'xll' => 'application/vnd.ms-excel',
|
||||
'ppt' => 'application/vnd.ms-powerpoint',
|
||||
'pps' => 'application/vnd.ms-powerpoint',
|
||||
// open office
|
||||
'odt' => 'application/vnd.oasis.opendocument.text',
|
||||
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
||||
);
|
||||
|
||||
/**
|
||||
* Determine mime type
|
||||
*
|
||||
* @param string $file path to the file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getMimeType($file) {
|
||||
if (function_exists('finfo_open')) {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$type = (string) finfo_file($finfo, $file);
|
||||
finfo_close($finfo);
|
||||
return $type;
|
||||
} else if (function_exists('mime_content_type')) {
|
||||
return mime_content_type($file);
|
||||
} else {
|
||||
$ext = strtolower(array_pop(explode('.', $file)));
|
||||
if (array_key_exists($ext, self::$mimeTypes))
|
||||
return self::$mimeTypes[$ext];
|
||||
else
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Shikiryu_Backup_Abstract $backup
|
||||
*/
|
||||
public function __construct(Shikiryu_Backup_Abstract $backup) {
|
||||
parent::__construct($backup);
|
||||
$this->setFiles($this->backup->getFilesToBackup());
|
||||
$this->setStreams($this->backup->getStreamsToBackup());
|
||||
$this->to = $this->config['to'];
|
||||
$this->from = $this->config['from'];
|
||||
$this->encoding = $this->config['encoding'];
|
||||
$this->subjet = $this->config['subject'];
|
||||
$this->message = $this->config['message'];
|
||||
$this->encoding = $this->config['encoding'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a recipient
|
||||
*
|
||||
* @param string $to
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function addTo($to)
|
||||
{
|
||||
$this->to = $to;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the sender
|
||||
*
|
||||
* @param $from
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function setFrom($from)
|
||||
{
|
||||
$this->from = $from;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the subject
|
||||
*
|
||||
* @param $sub
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function setSubject($sub)
|
||||
{
|
||||
$this->subject = strip_tags($sub);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the message (in text)
|
||||
*
|
||||
* @param $mes
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function setMessage($mes)
|
||||
{
|
||||
$this->message = strip_tags($mes);
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setFiles($files = array())
|
||||
{
|
||||
if (is_array($files) && !empty($files)) {
|
||||
$this->files = $files;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setStreams($streams = array())
|
||||
{
|
||||
if (is_array($streams) && !empty($streams)) {
|
||||
$this->streams = $streams;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the mail
|
||||
*
|
||||
* @see #mail
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function send() {
|
||||
|
||||
// Checking files are selected
|
||||
$zip = new ZipArchive(); // Load zip library
|
||||
$zip_name = time().".zip"; // Zip name
|
||||
if($zip->open(dirname(__FILE__).'/bu/'.$zip_name, ZIPARCHIVE::CREATE)==TRUE) {
|
||||
if(!empty($this->files)) {
|
||||
foreach($this->files as $file)
|
||||
{
|
||||
$zip->addFile($file); // Adding files into zip
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
$this->files = array(dirname(__FILE__).'/bu/'.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name);
|
||||
|
||||
$random_hash = md5(date('r', time()));
|
||||
$headers = "From: " . $this->from . "\r\nReply-To: " . $this->from;
|
||||
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . "\"";
|
||||
$output = "
|
||||
|
||||
--$random_hash
|
||||
Content-Type: text/plain; charset='" . strtolower($this->encoding) . "'
|
||||
Content-Transfer-Encoding: 8bit
|
||||
|
||||
" . $this->message . "\r\n";
|
||||
|
||||
if(!empty($this->files))
|
||||
foreach($this->files as $file=>$name) {
|
||||
$name = end(explode('/', $name));
|
||||
$output .= "
|
||||
--$random_hash
|
||||
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename=" . $name . "
|
||||
|
||||
" . chunk_split(base64_encode(file_get_contents($file)));
|
||||
}
|
||||
|
||||
if(!empty($this->streams))
|
||||
foreach($this->streams as $name=>$stream) {
|
||||
if(count(explode('.',$name))<2) $name = 'backup'.$name.'.txt';
|
||||
$output .= "
|
||||
--$random_hash
|
||||
Content-Type: text/plain; name=" . $name . "
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename=" . $name . "
|
||||
|
||||
" . chunk_split(base64_encode($stream));
|
||||
}
|
||||
$output.="--$random_hash--";
|
||||
return mail($this->to, $this->subject, $output, $headers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
91
app/code/Transport/FTP.php
Normal file
91
app/code/Transport/FTP.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
class Ftp extends TransportAbstract
|
||||
{
|
||||
|
||||
private $path;
|
||||
|
||||
private $connection;
|
||||
|
||||
private $files;
|
||||
private $streams;
|
||||
|
||||
public function __construct($backup, $server, $login, $pwd, $path='/') {
|
||||
parent::__construct($backup);
|
||||
|
||||
$this->path = $this->config['path'];
|
||||
if (!empty($this->config['path'])) {
|
||||
$this->path = sprintf('/%s/', ltrim(rtrim($this->config['path'], '/'),'/'));
|
||||
}
|
||||
|
||||
$this->connection = ftp_connect($this->config['host']);
|
||||
|
||||
$login = ftp_login($this->connection, $this->config['login'], $this->config['password']);
|
||||
if (!$this->connection || !$login) {
|
||||
throw new Exception('Connexion FTP refusée.');
|
||||
}
|
||||
|
||||
$this->setFiles($this->backup->getFilesToBackup());
|
||||
$this->setStreams($this->backup->getStreamsToBackup());
|
||||
}
|
||||
|
||||
private function setFiles($files = array())
|
||||
{
|
||||
if (is_array($files) && !empty($files))
|
||||
$this->files = $files;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setStreams($streams = array()) {
|
||||
if (is_array($streams) && !empty($streams))
|
||||
$this->streams = $streams;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$sent = 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);
|
||||
if (!$upload) {
|
||||
$sent = false;
|
||||
echo 'FTP upload manquée de '.$file.' vers '.$this->path.$name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->streams)){
|
||||
foreach ($this->streams as $name => $stream) {
|
||||
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);
|
||||
if (!$upload) {
|
||||
echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name;
|
||||
$sent = false;
|
||||
}
|
||||
unlink($name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
ftp_close($this->connection);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
?>
|
22
app/code/Transport/Factory.php
Normal file
22
app/code/Transport/Factory.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
* @param $type
|
||||
* @param Shikiryu_Backup_Abstract $backup
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function build(array $config)
|
||||
{
|
||||
$class = array_keys($config)[0];
|
||||
if (class_exists($class)) {
|
||||
/* @var $instance BackupAbstract */
|
||||
return new $class(array_values($config));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
188
app/code/backup/Abstract.php
Normal file
188
app/code/backup/Abstract.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Backup;
|
||||
|
||||
class BackupAbstract
|
||||
{
|
||||
|
||||
protected $_filesToBackup;
|
||||
protected $_streamsToBackup;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$this->_filesToBackup = array();
|
||||
$this->_streamsToBackup = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFilesToBackup()
|
||||
{
|
||||
return $this->_filesToBackup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getStreamsToBackup()
|
||||
{
|
||||
return $this->_streamsToBackup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the current date with the given format into the files names
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
function addDate($format = 'Ymd')
|
||||
{
|
||||
$tmpFiles = array();
|
||||
foreach($this->_filesToBackup as $file => $name)
|
||||
{
|
||||
$nameA = explode('.', $name);
|
||||
$nameA[] = end($nameA);
|
||||
$nameA[count($nameA)-2] = date($format);
|
||||
$name = implode('.', $nameA);
|
||||
$tmpFiles[$file] = $name;
|
||||
}
|
||||
$this->_filesToBackup = $tmpFiles;
|
||||
|
||||
$tmpStream = array();
|
||||
foreach($this->_streamsToBackup as $name => $stream)
|
||||
{
|
||||
$tmpStream[$name . '-' . date($format)] = $stream;
|
||||
}
|
||||
$this->_streamsToBackup = $tmpStream;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the current time with the given format into the files names
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
function addTime($format = 'his')
|
||||
{
|
||||
$tmpFiles = array();
|
||||
foreach($this->_filesToBackup as $file => $name)
|
||||
{
|
||||
$nameA = explode('.', $name);
|
||||
$nameA[] = end($nameA);
|
||||
$nameA[count($nameA)-2] = date($format);
|
||||
$name = implode('.', $nameA);
|
||||
$tmpFiles[$file] = $name;
|
||||
}
|
||||
$this->_filesToBackup = $tmpFiles;
|
||||
|
||||
$tmpStream = array();
|
||||
foreach($this->_streamsToBackup as $name => $stream)
|
||||
{
|
||||
$tmpStream[$name . '-' . date($format)] = $stream;
|
||||
}
|
||||
$this->_streamsToBackup = $tmpStream;
|
||||
|
||||
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, '/'));
|
||||
}
|
||||
// if($folder != '')
|
||||
// {
|
||||
// if(substr($folder, 0, -1) != '/')
|
||||
// $folder .= '/';
|
||||
// }
|
||||
foreach($this->_filesToBackup as $file => $name)
|
||||
{
|
||||
copy($file, $folder . $name);
|
||||
}
|
||||
foreach($this->_streamsToBackup as $name => $file)
|
||||
{
|
||||
if (count(explode('.', $name)) < 2) {
|
||||
$name = 'backup' . $name . '.txt';
|
||||
}
|
||||
file_put_contents($folder . $name, $file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if all files got the minimum given size.
|
||||
*
|
||||
* @param int $fs
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function checkMinimumFilesize($fs)
|
||||
{
|
||||
foreach($this->_filesToBackup as $file => $name)
|
||||
{
|
||||
if (filesize($file) < $fs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
foreach($this->_streamsToBackup as $name => $file)
|
||||
{
|
||||
if (mb_strlen($file, 'utf-8') < $fs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
20
app/code/backup/Factory.php
Normal file
20
app/code/backup/Factory.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Backup;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
* @param array $config
|
||||
* @return BackupAbstract
|
||||
*/
|
||||
public static function build(array $config)
|
||||
{
|
||||
$class = array_keys($config)[0];
|
||||
if (class_exists($class)) {
|
||||
/* @var $instance BackupAbstract */
|
||||
return new $class(array_values($config));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
36
app/code/backup/Files.php
Normal file
36
app/code/backup/Files.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Shikiryu\Backup\Backup;
|
||||
|
||||
class Files extends BackupAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $filesToBackup
|
||||
*/
|
||||
function __construct($filesToBackup = array())
|
||||
{
|
||||
parent::__construct();
|
||||
if(!empty($filesToBackup) && is_array($filesToBackup)){
|
||||
$names = array_map("basename",$filesToBackup);
|
||||
$this->_filesToBackup = array_combine($filesToBackup,$names);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string[] $filestobackup a list of file path
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
function setFilePath($filesToBackup = array())
|
||||
{
|
||||
if(!empty($filesToBackup) && is_array($filesToBackup))
|
||||
{
|
||||
$names = array_map("basename",$filesToBackup);
|
||||
$this->_filesToBackup = array_combine($filesToBackup,$names);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
?>
|
113
app/code/backup/Mysql.php
Normal file
113
app/code/backup/Mysql.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Backup;
|
||||
|
||||
class Mysql extends BackupAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var $_pdo PDO
|
||||
*/
|
||||
private $_pdo;
|
||||
private $_tables;
|
||||
|
||||
/**
|
||||
* @param $host
|
||||
* @param $login
|
||||
* @param $pwd
|
||||
* @param $db
|
||||
*/
|
||||
public function __construct($host, $login, $pwd, $db) {
|
||||
parent::__construct();
|
||||
$this->_tables = array();
|
||||
$this->_pdo = new PDO('mysql:host='.$host.';dbname='.$db, $login, $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
|
||||
*/
|
||||
public function fromTables($tables = array())
|
||||
{
|
||||
if(!empty($tables)) {
|
||||
$this->_tables = $tables;
|
||||
}
|
||||
$this->_streamsToBackup[] = $this->getFromTables();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the list of table to backup to all tables
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function everything() {
|
||||
$this->_tables = array();
|
||||
foreach($this->_pdo->query('SHOW TABLES') as $table) {
|
||||
$this->_tables[] = $table;
|
||||
}
|
||||
$this->_streamsToBackup[] = $this->getFromTables();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getFromTables()
|
||||
{
|
||||
$return = "";
|
||||
foreach ($this->_tables as $table) {
|
||||
if(is_array($table)) {
|
||||
$table = $table[0];
|
||||
}
|
||||
$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->execute();
|
||||
$row2 = $result2->fetch();
|
||||
$return.= "\n\n" . $row2[1] . ";\n\n";
|
||||
foreach($result as $i=>$row){
|
||||
$return.= 'INSERT INTO ' . $table . ' VALUES(';
|
||||
for ($j = 0; $j < $num_fields; $j++) {
|
||||
$row[$j] = addslashes($row[$j]);
|
||||
$row[$j] = preg_replace("\n", "\\n", $row[$j]);
|
||||
if (isset($row[$j])) {
|
||||
$return.= '"' . $row[$j] . '"';
|
||||
} else {
|
||||
$return.= '""';
|
||||
}
|
||||
if ($j < ($num_fields - 1)) {
|
||||
$return.= ',';
|
||||
}
|
||||
}
|
||||
$return.= ");\n";
|
||||
}
|
||||
$return.="\n\n\n";
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user