View source
<?php
use BackupMigrate\Core\Config\Config;
use BackupMigrate\Core\Services\BackupMigrate;
use BackupMigrate\Core\Main\BackupMigrateInterface;
use Drupal\backup_migrate\Entity\Destination;
use Drupal\backup_migrate\Entity\Schedule;
use Drupal\backup_migrate\Entity\Source;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\ClassLoader\Psr4ClassLoader;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\Core\Link;
$loader = new Psr4ClassLoader();
$loader
->addPrefix('BackupMigrate\\Core\\', __DIR__ . '/lib/backup_migrate_core/src');
$loader
->addPrefix('BackupMigrate\\Drupal\\', __DIR__ . '/src');
$loader
->register();
define('BACKUP_MIGRATE_MODULE_VERSION', '8.x-4.x-dev');
function backup_migrate_perform_backup($source_id, $destination_id, $config = []) {
try {
$bam = backup_migrate_get_service_object($config);
$bam
->backup($source_id, $destination_id);
drupal_set_message(t('Backup Complete.'));
} catch (Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
}
}
function backup_migrate_perform_restore($source_id, $destination_id, $file_id = NULL, $config = []) {
try {
$bam = backup_migrate_get_service_object($config);
$bam
->restore($source_id, $destination_id, $file_id);
drupal_set_message(t('Restore Complete.'));
} catch (Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
return;
}
}
function backup_migrate_get_service_object($config_array = [], $options = []) {
static $bam = NULL;
if ($bam === NULL) {
$bam = new \BackupMigrate\Core\Main\BackupMigrate();
\Drupal::moduleHandler()
->alter('backup_migrate_service_object', $bam, $options);
}
if ($config_array) {
$bam
->setConfig(new Config($config_array));
}
return $bam;
}
function backup_migrate_backup_migrate_service_object_alter(BackupMigrateInterface &$bam, $options = []) {
$sources = $bam
->sources();
$destinations = $bam
->destinations();
$plugins = $bam
->plugins();
$services = $bam
->services();
$services
->add('TempFileAdapter', new \BackupMigrate\Drupal\File\DrupalTempFileAdapter(\Drupal::service('file_system'), 'temporary://', 'bam'));
$services
->add('TempFileManager', new \BackupMigrate\Core\File\TempFileManager($services
->get('TempFileAdapter')));
$services
->add('Logger', new \BackupMigrate\Drupal\Environment\DrupalSetMessageLogger());
$services
->add('ArchiveReader', new \BackupMigrate\Core\Service\TarArchiveReader());
$services
->add('ArchiveWriter', new \BackupMigrate\Core\Service\TarArchiveWriter());
if (empty($options['nobrowser'])) {
$user = \Drupal::currentUser();
if ($user
->hasPermission('access backup files')) {
$destinations
->add('download', new \BackupMigrate\Drupal\Destination\DrupalBrowserDownloadDestination(new Config([
'name' => t('Download'),
])));
}
$destinations
->add('upload', new \BackupMigrate\Drupal\Destination\DrupalBrowserUploadDestination(new Config([
'name' => t('Upload'),
])));
}
$plugins
->add('namer', new \BackupMigrate\Core\Filter\FileNamer());
$plugins
->add('compressor', new \BackupMigrate\Core\Filter\CompressionFilter());
$plugins
->add('encrypt', new \BackupMigrate\Drupal\Filter\DrupalEncrypt());
$plugins
->add('utils', new \BackupMigrate\Drupal\Filter\DrupalUtils());
$plugins
->add('metadata', new \BackupMigrate\Core\Filter\MetadataWriter(new Config([
'generator' => 'Backup and Migrate for Drupal (https://www.drupal.org/project/backup_migrate)',
'generatorurl' => 'https://www.drupal.org/project/backup_migrate',
'generatorversion' => BACKUP_MIGRATE_MODULE_VERSION,
])));
foreach (Source::loadMultiple() as $source) {
$source
->getPlugin()
->alterBackupMigrate($bam, $source
->get('id'), $options);
}
foreach (Destination::loadMultiple() as $destination) {
$destination
->getPlugin()
->alterBackupMigrate($bam, $destination
->get('id'), $options);
}
}
function backup_migrate_cron() {
$bam = backup_migrate_get_service_object([], [
'nobrowser' => TRUE,
]);
$schedules = Schedule::loadMultiple();
foreach ($schedules as $schedule) {
$schedule
->run($bam);
}
}
function backup_migrate_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id === 'system_modules_uninstall_confirm_form') {
if (isset($form['entity_deletes']['backup_migrate_source'])) {
$form['text']['#markup'] .= '<p>' . t('Uninstalling Backup and Migrate will delete any custom Backup and Migrate configuration. <strong>Previously created backups will not be deleted.</strong>') . '</p>';
}
if (isset($form['entity_deletes']['backup_migrate_source']['#items']['entire_site'])) {
$form['entity_deletes']['backup_migrate_source']['#items']['entire_site'] = t('Full Site Backup Source');
}
}
}
function backup_migrate_help($route_name, RouteMatchInterface $route_match) {
if ($route_name == 'help.page.backup_migrate') {
$help = [];
$user = User::load(\Drupal::currentUser()
->id());
$urls = [
'restore' => 'backup_migrate.restore',
'schedules' => 'entity.backup_migrate_schedule.collection',
'manual' => 'backup_migrate.quick_backup',
'advanced' => 'backup_migrate.advanced_backup',
'settings' => 'entity.backup_migrate_settings.collection',
'destination' => 'entity.backup_migrate_destination.collection',
'sources' => 'entity.backup_migrate_source.collection',
'backups' => 'backup_migrate.backups',
'documentation' => 'https://www.drupal.org/docs/8/modules/backup-and-migrate-drupal-8',
'phpmyadmin' => 'http://www.phpmyadmin.net',
'cron' => 'http://drupal.org/cron',
];
foreach ($urls as $key => $destination) {
$urls[$key] = UrlHelper::isValid($destination, TRUE) ? Url::fromUri($destination) : Url::fromRoute($destination);
$urls[$key]
->setOptions([
'attributes' => [
'target' => '_blank',
],
]);
}
if ($user
->hasPermission('perform backups')) {
$help['intro'] = [
'#markup' => t("<p><strong>Backup and Migrate</strong> makes the task of backing up your Drupal database and migrating data from one Drupal install to another easier. It provides a function to backup the entire database to file or download, and to restore from a previous backup. You can also schedule the backup operation. Compression of backup files is also supported. The database backup files created with this module can be imported into this or any other Drupal installation with the @restore, or you can use a database tool such as @phpmyadmin or the mysql command line command. Access for backup migrate is controlled by module permissions, contact the administrator or any privileged user to get access to full module features.</p>", [
'@restore' => Link::fromTextAndUrl(t('restore feature'), $urls['restore'])
->toString(),
'@phpmyadmin' => Link::fromTextAndUrl(t('phpMyAdmin'), $urls['phpmyadmin'])
->toString(),
]),
];
$help['manual'] = [
'#markup' => t("<h3>@title</h3><p>Use this form to run simple @manual of your database. Visit the @documentation for more help using this module.</p>", [
'@title' => Link::fromTextAndUrl(t('Backup Tab - Quick Backup'), $urls['manual'])
->toString(),
'@manual' => Link::fromTextAndUrl(t('manual backups'), $urls['manual'])
->toString(),
'@documentation' => Link::fromTextAndUrl(t('documentation page'), $urls['documentation'])
->toString(),
]),
];
$help['advanced'] = [
'#markup' => t("<h3>@title</h3><p>Use this form to run manual backups of your database with more advanced options. If you have any @profile saved you can load those settings. You can save any of the changes you make to these settings as a new settings profile.</p>", [
'@title' => Link::fromTextAndUrl(t('Backup Tab - Advanced Backup'), $urls['advanced'])
->toString(),
'@profile' => Link::fromTextAndUrl(t('settings profiles'), $urls['settings'])
->toString(),
]),
];
}
if ($user
->hasPermission('restore from backup')) {
$help['restore'] = [
'#markup' => t("<h3>@title</h3><p>Upload a backup and migrate backup file. The restore function will not work with database dumps from other sources such as @phpmyadmin.</p>", [
'@title' => Link::fromTextAndUrl(t('Restore Tab'), $urls['restore'])
->toString(),
'@phpmyadmin' => Link::fromTextAndUrl(t('phpMyAdmin'), $urls['phpmyadmin'])
->toString(),
]),
];
}
if ($user
->hasPermission('administer backup and migrate')) {
$help['backups'] = [
'#markup' => t("<h3>@title</h3><p>Backups are the places you can save your backup files to or them load from.</p><p>Files can be saved to a directory on your web server, downloaded to your desktop or emailed to a specified email account. From the @destination tab you can create, delete and edit destinations or list the files which have already been backed up to the available destinations.</p>", [
'@title' => Link::fromTextAndUrl(t('Saved Backups'), $urls['backups'])
->toString(),
'@destination' => Link::fromTextAndUrl(t('destination'), $urls['destination'])
->toString(),
]),
];
$help['schedules'] = [
'#markup' => t("<h3>@title</h3><p>Automatically backup up your database on a regular schedule using @cron.</p><p>Each schedule will run a maximum of once per cron run, so they will not run more frequently than your cron is configured to run. If you specify a number of backups to keep for a schedule, old backups will be deleted as new ones created. <strong>If specifiy a number of files to keep other backup files in that schedule's @destination will get deleted</strong>.<p>", [
'@title' => Link::fromTextAndUrl(t('Shedules Tab'), $urls['schedules'])
->toString(),
'@cron' => Link::fromTextAndUrl(t('cron'), $urls['cron'])
->toString(),
'@destination' => Link::fromTextAndUrl(t('destination'), $urls['destination'])
->toString(),
]),
];
$help['settings'] = [
'#markup' => t("<h3>@title</h3><p>Settings are profile. Settings store your table exclusion settings as well as your backup file name, compression and timestamp settings. You can use profiles in @schedules and for @manual.</p><p>You can create new profiles using the add profiles tab or by checking the 'Save these settings' button on the advanced backup page.</p>", [
'@title' => Link::fromTextAndUrl(t('Settings Tab - Settings Profiles'), $urls['settings'])
->toString(),
'@schedules' => Link::fromTextAndUrl(t('schedules'), $urls['schedules'])
->toString(),
'@manual' => Link::fromTextAndUrl(t('quick backups'), $urls['manual'])
->toString(),
]),
];
$help['destination'] = [
'#markup' => t("<h3>@title</h3><p>Destinations store your custom created destionation settings as backup server folders or external clouds (Clouds destinations are in work, check the module issues lists).</p>", [
'@title' => Link::fromTextAndUrl(t('Settings Tab - Destinations'), $urls['destination'])
->toString(),
]),
];
$help['sources'] = [
'#markup' => t("<h3>@title</h3><p>Sources store your source settings you want to backup, for now is available 4 default sources to use. Follow module issues list for new source features.</p>", [
'@title' => Link::fromTextAndUrl(t('Settings Tab - Sources'), $urls['sources'])
->toString(),
]),
];
}
$output = '';
foreach ($help as $key => $value) {
$output .= render($value);
}
return $output;
}
}