mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
Added validators and new transport
This commit is contained in:
@@ -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,44 +103,9 @@ 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();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,19 +114,17 @@ class BackupAbstract
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
@@ -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";
|
||||
|
Reference in New Issue
Block a user