Backup plugin not finished, Filesystem zip

This commit is contained in:
Diego Najar 2017-10-29 20:27:52 +01:00
parent 7e4f9376e0
commit fb97bc83f5
7 changed files with 157 additions and 1 deletions

View File

@ -116,6 +116,9 @@ define('FILENAME', 'index.txt');
// Database date format
define('DB_DATE_FORMAT', 'Y-m-d H:i:s');
// Database date format
define('BACKUP_DATE_FORMAT', 'Y-m-d-H-i-s');
// Sitemap date format
define('SITEMAP_DATE_FORMAT', 'Y-m-d');

View File

@ -102,4 +102,36 @@ class Filesystem {
return rmdir($source);
}
public static function zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
if (is_dir($source) === true) {
$iterator = new RecursiveDirectoryIterator($source);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = realpath($file);
if (is_dir($file)) {
$zip->addEmptyDir(str_replace($source, '', $file));
} elseif (is_file($file)) {
$zip->addFromString(str_replace($source, '', $file), file_get_contents($file));
}
}
} elseif (is_file($source)) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "Backup",
"description": "Backup system"
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "Backup",
"description": "Backup system"
}
}

View File

@ -0,0 +1,10 @@
{
"author": "Bludit",
"email": "",
"website": "https://pro.bludit.com",
"version": "2.0",
"releaseDate": "2017-05-19",
"license": "Bludit PRO",
"compatible": "2.0",
"notes": "This plugin is delivery with Bludit PRO. Read more about it on https://pro.bludit.com"
}

View File

@ -0,0 +1,97 @@
<?php
class pluginBackup extends Plugin {
public function init()
{
// Disable default form buttons (Save and Cancel)
$this->formButtons = false;
}
// Install the plugin and create the workspace directory
public function install($position=0)
{
parent::install($position);
$workspace = $this->workspace();
return mkdir($workspace, 0755, true);
}
// Uninstall the plugin and delete the workspace directory
public function uninstall()
{
parent::uninstall();
$workspace = $this->workspace();
return Filesystem::deleteRecursive($workspace);
}
// Redefine workspace
public function workspace()
{
return PATH_CONTENT.'backup'.DS;
}
public function form()
{
$this->createBackup();
}
public function createBackup()
{
$currentDate = Date::current(BACKUP_DATE_FORMAT);
// Create backup directory with the current date
$tmp = $this->workspace().'backup-'.$currentDate;
// Copy pages directory
$destination = $tmp.DS.'pages';
mkdir($destination, 0755, true);
$source = rtrim(PATH_PAGES, '/');
Filesystem::copyRecursive($source, $destination);
// Copy databases directory
$destination = $tmp.DS.'databases';
mkdir($destination, 0755, true);
$source = rtrim(PATH_DATABASES, '/');
Filesystem::copyRecursive($source, $destination);
// Copy uploads directory
$destination = $tmp.DS.'uploads';
mkdir($destination, 0755, true);
$source = rtrim(PATH_UPLOADS, '/');
Filesystem::copyRecursive($source, $destination);
// Compress backup directory
if (Filesystem::zip($tmp, $tmp.'.zip')) {
Filesystem::deleteRecursive($tmp);
}
}
// Copy the content from the backup to /bl-content/
private function replaceContent($idExecution)
{
$source = $this->workspace().$idExecution;
$dest = rtrim(PATH_CONTENT, '/');
return Filesystem::copyRecursive($source, $dest);
}
// Delete old backups until the $idExecution
private function cleanUp($idExecution)
{
$backups = $this->getBackupsDirectories();
foreach ($backups as $dir) {
$backupIDExecution = basename($dir);
Filesystem::deleteRecursive($dir);
if($backupIDExecution==$idExecution) {
return true;
}
}
return true;
}
// Returns array with all backups directories sorted by date newer first
private function getBackupsDirectories()
{
$workspace = $this->workspace();
return Filesystem::listDirectories($workspace, $regex='*', $sortByDate=true);
}
}

View File

@ -136,7 +136,7 @@ class pluginsimpleMDE extends Plugin {
output = "\n'.PAGE_BREAK.'\n";
cm.replaceSelection(output);
},
className: "fa fa-minus-square-o",
className: "fa fa-scissors",
title: "'.$Language->get('Pagebreak').'",
}]
});';