Commit 901611d1 authored by ronan's avatar ronan

Refactor destinations, sources and filters

parent eb85e0c0
......@@ -10,3 +10,15 @@ entity.backup_migrate_settings.add_form:
appears_on:
- entity.backup_migrate_settings.collection
entity.backup_migrate_destination.add_form:
route_name: 'entity.backup_migrate_destination.add_form'
title: 'Add Backup Destination'
appears_on:
- entity.backup_migrate_destination.collection
entity.backup_migrate_source.add_form:
route_name: 'entity.backup_migrate_source.add_form'
title: 'Add Backup Source'
appears_on:
- entity.backup_migrate_source.collection
......@@ -28,3 +28,23 @@ backup_migrate.settings:
route_name: entity.backup_migrate_settings.collection
base_route: backup_migrate.quick_backup
backup_migrate.settings_profiles:
title: Settings Profiles
route_name: entity.backup_migrate_settings.collection
base_route: backup_migrate.quick_backup
parent_id: backup_migrate.settings
backup_migrate.destination:
title: Destinations
route_name: entity.backup_migrate_destination.collection
base_route: backup_migrate.quick_backup
parent_id: backup_migrate.settings
backup_migrate.source:
title: Sources
route_name: entity.backup_migrate_source.collection
base_route: backup_migrate.quick_backup
parent_id: backup_migrate.settings
<?php
use BackupMigrate\Core\Config\Config;
use BackupMigrate\Core\Main\BackupMigrateInterface;
use Drupal\backup_migrate\Entity\Destination;
use Drupal\backup_migrate\Entity\Schedule;
use Drupal\backup_migrate\Entity\Source;
require __DIR__.'/vendor/autoload.php';
......@@ -34,8 +37,9 @@ function backup_migrate_perform_backup($source_id, $destination_id, $config = []
*
* @param string $source_id
* @param string|array $destination_id
* @parem string $file_id
* @param string|null $file_id
* @param array $config
* @parem string $file_id
*/
function backup_migrate_perform_restore($source_id, $destination_id, $file_id = NULL, $config = []) {
try {
......@@ -55,8 +59,10 @@ function backup_migrate_perform_restore($source_id, $destination_id, $file_id =
/**
* Get a BackupMigrate service object.
*
* @param array $config_array
* An array of configuration arrays, keyed by plugin id.
* @param array $options
* A keyed array of options..
* A keyed array of options.
* @return \BackupMigrate\Core\Main\BackupMigrate
*/
function backup_migrate_get_service_object($config_array = [], $options = []) {
......@@ -64,24 +70,8 @@ function backup_migrate_get_service_object($config_array = [], $options = []) {
// If the static cached object has not been loaded.
if ($bam === NULL) {
// Create the environment services.
$logger = new \BackupMigrate\Drupal\Environment\DrupalSetMessageLogger();
$files = new \BackupMigrate\Drupal\File\DrupalTempFileAdapter(\Drupal::service('file_system'), 'temporary://', 'bam');
// Create the service locator
$services = new \BackupMigrate\Core\Service\ServiceLocator();
// Allow other modules to add services.
\Drupal::moduleHandler()->alter('backup_migrate_services', $services, $options);
// Create the plugin manager
$plugins = new \BackupMigrate\Core\Plugin\PluginManager($services);
// Allow other modules to add plugins.
\Drupal::moduleHandler()->alter('backup_migrate_plugins', $plugins, $options);
// Create the service object.
$bam = new \BackupMigrate\Core\Main\BackupMigrate($plugins);
$bam = new \BackupMigrate\Core\Main\BackupMigrate();
// Allow other modules to alter the object
\Drupal::moduleHandler()->alter('backup_migrate_service_object', $bam, $options);
......@@ -96,22 +86,24 @@ function backup_migrate_get_service_object($config_array = [], $options = []) {
}
/**
* Implements hook_backup_migrate_services_alter().
* Implements hook_backup_migrate_service_object_alter().
*
* Add the core Backup and Migrate services to the service locator.
* Add the core Backup and Migrate plugins to the service object.
*
* @param \BackupMigrate\Core\Service\ServiceLocatorInterface $services
* @param \BackupMigrate\Core\Main\BackupMigrateInterface $bam
* @param array $options
*/
function backup_migrate_backup_migrate_services_alter(\BackupMigrate\Core\Service\ServiceLocator &$services) {
function backup_migrate_backup_migrate_service_object_alter(BackupMigrateInterface &$bam, $options = []) {
$sources = &$bam->sources();
$destinations = &$bam->destinations();
$plugins = &$bam->plugins();
$services = &$bam->services();
// Add a temp file manager which can access the drupal temp directory.
$services->add('TempFileAdapter',
new \BackupMigrate\Drupal\File\DrupalTempFileAdapter(\Drupal::service('file_system'), 'temporary://', 'bam')
);
// $services->add('TempFileAdapter',
// new \BackupMigrate\Core\File\TempFileAdapter('/tmp/', 'bam')
// );
//
$services->add('TempFileManager',
new \BackupMigrate\Core\File\TempFileManager($services->get('TempFileAdapter'))
......@@ -124,56 +116,47 @@ function backup_migrate_backup_migrate_services_alter(\BackupMigrate\Core\Servic
$services->add('ArchiveReader', new \BackupMigrate\Core\Service\TarArchiveReader());
$services->add('ArchiveWriter', new \BackupMigrate\Core\Service\TarArchiveWriter());
}
/**
* Implements hook_backup_migrate_service_object_alter().
*
* Add the core Backup and Migrate plugins to the service object.
*
* @param \BackupMigrate\Core\Plugin\PluginManagerInterface $plugins
* @param array $options
*/
function backup_migrate_backup_migrate_plugins_alter(\BackupMigrate\Core\Plugin\PluginManagerInterface &$plugins, $options = []) {
// Add the default database.
$info = \Drupal\Core\Database\Database::getConnectionInfo('default', 'default');
$info = $info['default'];
if ($info['driver'] == 'mysql') {
$db = new \BackupMigrate\Core\Source\MySQLiSource(new Config($info + ['name' => t('Default Database')]));
$plugins->add('db', $db);
$sources->add('db', $db);
// Add the entire site
$plugins->add('site', new \BackupMigrate\Drupal\Source\DrupalSiteArchiveSource(new Config(['name' => t('Entire Site'), 'directory' => DRUPAL_ROOT]), $db));
$sources->add('site', new \BackupMigrate\Drupal\Source\DrupalSiteArchiveSource(new Config(['name' => t('Entire Site'), 'directory' => DRUPAL_ROOT]), $db));
}
// Add the public files
$plugins->add(
'files_public',
new \BackupMigrate\Drupal\Source\DrupalPublicFilesSource(new Config(['name' => t('Public Files Directory')]))
);
$files_public = new \BackupMigrate\Drupal\Source\DrupalPublicFilesSource(new Config(['name' => t('Public Files Directory')]));
$sources->add('files_public', $files_public);
$plugins->add('files_public_exclude', new \BackupMigrate\Drupal\Filter\DrupalPublicFileExcludeFilter(new Config(['source' => $files_public])));
// If this is a nobrowser op (cron) then do not add the browser plugins.
// TODO: Make this better.
if (empty($options['nobrowser'])) {
// Add a download destination.
$plugins->add('download', new \BackupMigrate\Drupal\Destination\DrupalBrowserDownloadDestination(new Config(['name' => t('Download')])));
$destinations->add('download', new \BackupMigrate\Drupal\Destination\DrupalBrowserDownloadDestination(new Config(['name' => t('Download')])));
// Add an upload destination
$plugins->add('upload', new \BackupMigrate\Drupal\Destination\DrupalBrowserUploadDestination(new Config(['name' => t('Upload')])));
$destinations->add('upload', new \BackupMigrate\Drupal\Destination\DrupalBrowserUploadDestination(new Config(['name' => t('Upload')])));
// Add a debug destination.
$plugins->add('debug', new \BackupMigrate\Core\Destination\DebugDestination(new Config(['name' => t('Debug'), 'format' => 'html'])));
$destinations->add('debug', new \BackupMigrate\Core\Destination\DebugDestination(new Config(['name' => t('Debug'), 'format' => 'html'])));
}
// Add a private directory destination.
$plugins->add('private', new \BackupMigrate\Drupal\Destination\DrupalDirectoryDestination(new Config(['name' => t('Private Files Directory'), 'directory' => 'private://backup_migrate/'])));
$destinations->add('private', new \BackupMigrate\Drupal\Destination\DrupalDirectoryDestination(new Config(['name' => t('Private Files Directory'), 'directory' => 'private://backup_migrate/'])));
$plugins->add('db_exclude', new \BackupMigrate\Core\Filter\DBExcludeFilter());
// Add a file naming filter.
$plugins->add('namer', new \BackupMigrate\Core\Filter\FileNamer());
// Add a compression filter
// Add a compression filter.
$plugins->add('compressor', new \BackupMigrate\Core\Filter\CompressionFilter());
// Add the Drupal utilities filter.
......@@ -188,33 +171,15 @@ function backup_migrate_backup_migrate_plugins_alter(\BackupMigrate\Core\Plugin\
])
));
// Add the custom configured sources
foreach (Source::loadMultiple() as $source) {
$source->getPlugin()->alterBackupMigrate($bam, $source->get('id'), $options);
}
// Add an S3 destination.
// $bam->plugins()->add(
// new \BackupMigrate\Drupal\Destination\DrupalS3Destination(
// new \BackupMigrate\Core\Config\Config(
// ['prefix' => 'path/to/backups'],
// ),
// new \BackupMigrate\Drupal\Destination\S3ReaderWriter([
// 'credentials' => s3_get_credentials(),
// ]),
// new \BackupMigrate\Drupal\Destination\S3Writer(),
//
// ), 'download');
//
// // Add an File destination.
// $bam->plugins()->add(
// new \BackupMigrate\Drupal\Destination\FileDirectorySource(
// new \BackupMigrate\Drupal\Services\TarArchiveReader(),
// new \BackupMigrate\Drupal\Services\TarArchiveWriter(),
// new \BackupMigrate\Core\Config\Config(
// ['directory' => 'public://']
//
// )
// ), 'files_public');
// Add the custom configured destination
foreach (Destination::loadMultiple() as $destination) {
$destination->getPlugin()->alterBackupMigrate($bam, $destination->get('id'), $options);
}
}
/**
......@@ -229,4 +194,5 @@ function backup_migrate_cron() {
foreach ($schedules as $schedule) {
$schedule->run($bam);
}
}
\ No newline at end of file
}
......@@ -22,6 +22,8 @@ backup_migrate.restore:
requirements:
_permission: 'restore from backup'
# Schedule
entity.backup_migrate_schedule.collection:
path: '/admin/config/development/backup_migrate/schedule'
defaults:
......@@ -55,6 +57,8 @@ entity.backup_migrate_schedule.delete_form:
_permission: 'administer backup and migrate'
# Settings Profile
entity.backup_migrate_settings.collection:
path: '/admin/config/development/backup_migrate/settings'
defaults:
......@@ -87,3 +91,70 @@ entity.backup_migrate_settings.delete_form:
requirements:
_permission: 'administer backup and migrate'
# Backup Destination
entity.backup_migrate_destination.collection:
path: '/admin/config/development/backup_migrate/settings/destination'
defaults:
_entity_list: 'backup_migrate_destination'
_title: 'Backup Destination'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_destination.add_form:
path: '/admin/config/development/backup_migrate/settings/destination/add'
defaults:
_entity_form: backup_migrate_destination.default
_title: 'Add destination'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_destination.edit_form:
path: '/admin/config/development/backup_migrate/settings/destination/edit/{backup_migrate_destination}'
defaults:
_entity_form: backup_migrate_destination.default
_title: 'Edit destination'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_destination.delete_form:
path: '/admin/config/development/backup_migrate/settings/destination/delete/{backup_migrate_destination}'
defaults:
_entity_form: backup_migrate_destination.delete
_title: 'Delete destination'
requirements:
_permission: 'administer backup and migrate'
# Backup Source
entity.backup_migrate_source.collection:
path: '/admin/config/development/backup_migrate/settings/source'
defaults:
_entity_list: 'backup_migrate_source'
_title: 'Backup sources'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_source.add_form:
path: '/admin/config/development/backup_migrate/settings/source/add'
defaults:
_entity_form: backup_migrate_source.default
_title: 'Add Backup Source'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_source.edit_form:
path: '/admin/config/development/backup_migrate/settings/source/edit/{backup_migrate_source}'
defaults:
_entity_form: backup_migrate_source.default
_title: 'Edit source'
requirements:
_permission: 'administer backup and migrate'
entity.backup_migrate_source.delete_form:
path: '/admin/config/development/backup_migrate/settings/source/delete/{backup_migrate_source}'
defaults:
_entity_form: backup_migrate_source.delete
_title: 'Delete source'
requirements:
_permission: 'administer backup and migrate'
services:
plugin.manager.backup_migrate_source:
class: BackupMigrate\Drupal\EntityPlugins\SourcePluginManager
parent: default_plugin_manager
plugin.manager.backup_migrate_destination:
class: BackupMigrate\Drupal\EntityPlugins\DestinationPluginManager
parent: default_plugin_manager
......@@ -12,12 +12,12 @@
"source": {
"type": "git",
"url": "https://github.com/backupmigrate/backup_migrate_core.git",
"reference": "8e6560b38a990c9f62072660675c9f617d662e8e"
"reference": "c027281c5de06af92520764a302a02a8d60949f4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/backupmigrate/backup_migrate_core/zipball/8e6560b38a990c9f62072660675c9f617d662e8e",
"reference": "8e6560b38a990c9f62072660675c9f617d662e8e",
"url": "https://api.github.com/repos/backupmigrate/backup_migrate_core/zipball/c027281c5de06af92520764a302a02a8d60949f4",
"reference": "c027281c5de06af92520764a302a02a8d60949f4",
"shasum": ""
},
"require": {
......@@ -50,7 +50,7 @@
"source": "https://github.com/backupmigrate/backup_migrate_core/tree/master",
"issues": "https://github.com/backupmigrate/backup_migrate_core/issues"
},
"time": "2015-12-09 18:16:15"
"time": "2016-03-17 03:54:58"
},
{
"name": "psr/log",
......@@ -58,12 +58,12 @@
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "9e45edca52cc9c954680072c93e621f8b71fab26"
"reference": "d8e60a5619fff77f9669da8997697443ef1a1d7e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/9e45edca52cc9c954680072c93e621f8b71fab26",
"reference": "9e45edca52cc9c954680072c93e621f8b71fab26",
"url": "https://api.github.com/repos/php-fig/log/zipball/d8e60a5619fff77f9669da8997697443ef1a1d7e",
"reference": "d8e60a5619fff77f9669da8997697443ef1a1d7e",
"shasum": ""
},
"require": {
......@@ -91,12 +91,13 @@
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"time": "2015-06-02 13:48:41"
"time": "2016-01-06 21:40:42"
}
],
"packages-dev": [],
......
......@@ -49,6 +49,9 @@ backup_migrate.backup_migrate_schedule.*:
destination_id:
type: string
label: 'Destination'
settings_profile_id:
type: string
label: 'Settings Profile'
backup_migrate.backup_migrate_settings.*:
type: config_entity
......@@ -65,3 +68,41 @@ backup_migrate.backup_migrate_settings.*:
config:
type: mapping
label: 'Configuration'
backup_migrate.backup_migrate_destination.*:
type: config_entity
label: 'Backup Destination'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string
type:
type: string
label: 'Destination Type'
config:
type: mapping
label: 'Configuration'
backup_migrate.backup_migrate_source.*:
type: config_entity
label: 'Backup Source'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string
type:
type: string
label: 'Source Type'
config:
type: mapping
label: 'Configuration'
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\backup_migrate\DestinationListBuilder.
*/
namespace Drupal\backup_migrate\Controller;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Backup Destination entities.
*/
class DestinationListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Backup Destination');
$header['id'] = $this->t('Machine name');
$header['type'] = $this->t('Type');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
$row['type'] = $entity->get('type');
if ($plugin = $entity->getPlugin()) {
$info = $plugin->getPluginDefinition();
$row['type'] = $info['title'];
}
return $row + parent::buildRow($entity);
}
}
......@@ -60,29 +60,6 @@ class ScheduleListBuilder extends ConfigEntityListBuilder {
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->hasLinkTemplate('edit-form')) {
$operations['edit'] = array(
'title' => t('Edit schedule'),
'weight' => 20,
'url' => $entity->urlInfo('edit-form'),
);
}
if ($entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = array(
'title' => t('Delete schedule'),
'weight' => 30,
'url' => $entity->urlInfo('delete-form'),
);
}
return $operations;
}
/**
* {@inheritdoc}
*/
......
......@@ -19,7 +19,6 @@ class SettingsProfileListBuilder extends ConfigEntityListBuilder {
*/
public function buildHeader() {
$header['label'] = $this->t('Profile Name');
$header['id'] = $this->t('Machine name');
return $header + parent::buildHeader();
}
......@@ -28,31 +27,7 @@ class SettingsProfileListBuilder extends ConfigEntityListBuilder {
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
// You probably want a few more properties here...
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->hasLinkTemplate('edit-form')) {
$operations['edit'] = array(
'title' => t('Edit profile'),
'weight' => 20,
'url' => $entity->urlInfo('edit-form'),
);
}
if ($entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = array(
'title' => t('Delete profile'),
'weight' => 30,
'url' => $entity->urlInfo('delete-form'),
);
}
return $operations;
}
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\SourceListBuilder.
*/
namespace Drupal\backup_migrate\Controller;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Backup Source entities.
*/
class SourceListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Backup Source');
$header['id'] = $this->t('Machine name');
$header['type'] = $this->t('Type');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
$row['type'] = $entity->get('type');
if ($plugin = $entity->getPlugin()) {
$info = $plugin->getPluginDefinition();
$row['type'] = $info['title'];
}
return $row + parent::buildRow($entity);
}
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\Entity\Destination.
*/
namespace Drupal\backup_migrate\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\backup_migrate\DestinationInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
/**
* Defines the Backup Destination entity.
*
* @ConfigEntityType(
* id = "backup_migrate_destination",
* label = @Translation("Backup Destination"),
* module = "backup_migrate",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* "type" = "type",
* "config" = "config"
* },
* admin_permission = "administer backup and migrate",
* handlers = {
* "list_builder" = "Drupal\backup_migrate\Controller\DestinationListBuilder",
* "form" = {
* "default" = "Drupal\backup_migrate\Form\DestinationForm",
* "delete" = "Drupal\backup_migrate\Form\EntityDeleteForm"
* },
* },
* links = {
* "edit-form" = "/admin/config/development/backup_migrate/settings/destination/edit/{backup_migrate_destination}",
* "delete-form" = "/admin/config/development/backup_migrate/settings/destination/delete/{backup_migrate_destination}",
* "collection" = "/admin/config/development/backup_migrate/settings/destination",
* }
* )
*/
class Destination extends WrapperEntityBase {
/**
* Return the plugin manager.
*
* @return string
*/
public function getPluginManager() {
return \Drupal::service('plugin.manager.backup_migrate_destination');
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
namespace Drupal\backup_migrate\Entity;
use BackupMigrate\Core\Config\Config;
use BackupMigrate\Core\Exception\BackupMigrateException;
use BackupMigrate\Core\Main\BackupMigrateInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
......@@ -18,6 +19,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
* id = "backup_migrate_schedule",
* label = @Translation("Schedule"),
* module = "backup_migrate",
* admin_permission = "administer backup and migrate",
* entity_keys = {
* "id" = "id",
* "label" = "label",
......@@ -27,7 +29,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
* "list_builder" = "Drupal\backup_migrate\Controller\ScheduleListBuilder",
* "form" = {
* "default" = "Drupal\backup_migrate\Form\ScheduleForm",
* "delete" = "Drupal\backup_migrate\Form\ScheduleDeleteForm"
* "delete" = "Drupal\backup_migrate\Form\EntityDeleteForm"
* },
* },
* links = {
......@@ -60,25 +62,38 @@ class Schedule extends ConfigEntityBase {
* Run the schedule even if it is not due to be run.
*/
public function run(BackupMigrateInterface $bam, $force = FALSE) {
$next_run_at = $this->getNextRun();
$should_run_now = (REQUEST_TIME >= $next_run_at);
if ($force || $should_run_now) {
$enabled = $this->get('enabled');
if ($force || ($should_run_now && $enabled)) {
// Set the last run time before attempting backup.
// This will prevent a failing schedule from retrying on every cron run.
$this->setLastRun(REQUEST_TIME);
try {
$config = [];
if ($settings_profile_id = $this->get('settings_profile_id')) {
// Load the settings profile if one is selected.
$profile = SettingsProfile::load($settings_profile_id);
if (!$profile) {
throw new BackupMigrateException(
"The settings profile '%profile' does not exist",
['%profile' => $settings_profile_id]);
}
$config = $profile->get('config');
}
\Drupal::logger('backup_migrate')->info("Running schedule %name", ['%name' => $this->get('label')]);
// TODO: Set the config (don't just use the defaults).
// Run the backup.
$bam->backup($this->get('source_id'), $this->get('destination_id'));
$bam->setConfig(new Config($config));