View source
<?php
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\backup_db\Adapter\BackupDatabaseLocalAdapter;
function backup_db_history_insert($file) {
$connection = \Drupal\Core\Database\Database::getConnection();
return $connection
->insert('backup_db')
->fields(array(
'fid',
'name',
'uri',
'created',
))
->values(array(
'fid' => $file['fid'],
'name' => $file['name'],
'uri' => $file['uri'],
'created' => REQUEST_TIME,
))
->execute();
}
function backup_db_history_load($id) {
$connection = \Drupal\Core\Database\Database::getConnection();
return $connection
->query('SELECT * FROM {backup_db} WHERE eid = :id', array(
':id' => $id,
))
->fetchAll();
}
function backup_db_history_delete($id) {
db_delete('backup_db')
->condition('fid', $id)
->execute();
return file_delete($id);
}
function backup_db_show_tables() {
$connection = \Drupal\Core\Database\Database::getConnection();
return $connection
->query('SHOW TABLES')
->fetchAll();
}
function backup_db_link($uri) {
$scheme = \Drupal::service('file_system')
->uriScheme($uri);
$path = $uri;
if ($scheme == 'private') {
$private_path = PrivateStream::basePath();
$path = str_replace('private:/', $private_path, $uri);
}
return $path;
}
function _backup_db_format_options($results) {
$options = array();
foreach ($results as $result) {
$value = current((array) $result);
$options[$value] = $value;
}
return $options;
}
function backup_db_cron() {
$config = \Drupal::config('backup_db.settings');
if ($config
->get('cron_backup_enabled')) {
$interval = (int) $config
->get('cron_interval') * 60 * 60;
$expires = Drupal::state()
->get('backup_db.cron_last_run') ?: REQUEST_TIME;
$next = Drupal::state()
->get('backup_db.cron_next_backup') ?: $expires + $interval;
if (REQUEST_TIME > $next) {
$client = \Drupal::service('backup_db.client');
$handler = new BackupDatabaseLocalAdapter($client);
$handler
->export();
}
Drupal::state()
->set('backup_db.cron_last_run', REQUEST_TIME);
}
}