Skip to content

Commit 27d6f47

Browse files
committed
first commit
0 parents  commit 27d6f47

29 files changed

+2120
-0
lines changed

LICENSE.txt

+339
Large diffs are not rendered by default.

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
This module provides database export functionality with help from a php version of mysqldump. The library (https://github.com/ifsnop/mysqldump-php) supports the following features.
2+
3+
- output binary blobs as hex.
4+
- resolves view dependencies (using Stand-In tables).
5+
- output compared against original mysqldump. Linked to travis-ci testing system.
6+
- dumps stored procedures.
7+
- does extended-insert and/or complete-insert.
8+
9+
This module depends upon Composer Manager to install the mysqldump-php library; please follow the installation section below.
10+
11+
Installation (@ref, http://cgit.drupalcode.org/address/tree/README.md)
12+
13+
1. cd / navigate to the Drupal root directory
14+
15+
2. add the Drupal Packagist repository
16+
``
17+
composer config repositories.drupal composer https://packages.drupal.org/8
18+
``
19+
20+
3. use composer to download the module and it's dependencies
21+
``
22+
composer require drupal/backup_db
23+
``
24+
25+
4. enable the Backup Database module.
26+
27+
Manual usage
28+
29+
1. $client = \Drupal::service('backup_db.client');
30+
2. Do client things (update settings, set new connection)
31+
$client->setConnection();
32+
3. Select our adapter/handler.
33+
4. Do the magic
34+
$handler->export();
35+
36+
More information
37+
- https://www.drupal.org/node/2405811
38+
39+
Credits
40+
- ifsnop (https://github.com/ifsnop)

backup_db.info.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Backup Database
2+
type: module
3+
description: 'Provides database backup solutions.'
4+
package: Development
5+
core_version_requirement: ^8.8 || ^9
6+
7+
# Information added by Drupal.org packaging script on 2022-04-30
8+
version: '8.x-2.2'
9+
project: 'backup_db'
10+
datestamp: 1651351844

backup_db.install

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* backup_db.install
6+
*/
7+
8+
/**
9+
* Implements hook_requirements().
10+
*/
11+
function backup_db_requirements($phase) {
12+
$requirements = [];
13+
14+
if ($phase == 'install') {
15+
if (!class_exists('\Ifsnop\Mysqldump\Mysqldump')) {
16+
$requirements['backup_db_mysqldump'] = [
17+
'description' => t('Backup Database requires the Ifsnop\Mysqldump library, please read the readme.md and install via composer.'),
18+
'severity' => REQUIREMENT_ERROR,
19+
];
20+
}
21+
}
22+
23+
return $requirements;
24+
}
25+
26+
/**
27+
* Implements hook_schema().
28+
*/
29+
function backup_db_schema() {
30+
$schema['backup_db'] = [
31+
'description' => 'Tracks export creation and location.',
32+
'fields' => [
33+
'eid' => [
34+
'description' => 'The export identifier.',
35+
'type' => 'serial',
36+
'not null' => TRUE,
37+
],
38+
'fid' => [
39+
'description' => 'The referenced file id.',
40+
'type' => 'int',
41+
'unsigned' => TRUE,
42+
'not null' => TRUE,
43+
'default' => 0,
44+
],
45+
'name' => [
46+
'description' => 'The name of the export.',
47+
'type' => 'varchar',
48+
'length' => 255,
49+
'not null' => TRUE,
50+
'default' => '',
51+
],
52+
'uri' => [
53+
'description' => 'The uri of the export.',
54+
'type' => 'varchar',
55+
'length' => 255,
56+
'not null' => TRUE,
57+
'default' => '',
58+
],
59+
'created' => [
60+
'description' => 'The timestamp when the export was created.',
61+
'type' => 'int',
62+
'not null' => TRUE,
63+
'default' => 0,
64+
],
65+
],
66+
'primary key' => ['eid'],
67+
'foreign keys' => [
68+
'file' => [
69+
'table' => 'file_managed',
70+
'columns' => ['fid' => 'fid'],
71+
],
72+
],
73+
];
74+
75+
return $schema;
76+
}

backup_db.links.menu.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
backup_db.export:
2+
title: 'Backup Database'
3+
description: 'Backup and export database management.'
4+
route_name: backup_db.export
5+
parent: 'system.admin_config_development'

backup_db.links.task.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
backup_db.export:
2+
title: 'Export'
3+
route_name: backup_db.export
4+
base_route: backup_db.export
5+
6+
backup_db.history:
7+
title: 'Export History'
8+
route_name: backup_db.export_history
9+
base_route: backup_db.export
10+
11+
backup_db.settings:
12+
title: 'Settings'
13+
route_name: backup_db.settings
14+
base_route: backup_db.export
15+
16+
backup_db.cron:
17+
title: 'Cron'
18+
route_name: backup_db.cron
19+
base_route: backup_db.export
20+
21+
backup_db.tables:
22+
title: 'Tables'
23+
route_name: backup_db.tables
24+
base_route: backup_db.export

backup_db.module

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* backup_db.module
6+
*/
7+
8+
use Drupal\Core\StreamWrapper\PrivateStream;
9+
use Drupal\backup_db\Adapter\BackupDatabaseLocalAdapter;
10+
use Drupal\Core\Database\Database;
11+
12+
/**
13+
* Inserts an export entry.
14+
*
15+
* @param array $file
16+
* The file.
17+
*
18+
* @return \Drupal\Core\Database\StatementInterface|int|string|null
19+
*
20+
* @throws \Exception
21+
*/
22+
function backup_db_history_insert($file) {
23+
$connection = Database::getConnection();
24+
return $connection->insert('backup_db')
25+
->fields(['fid', 'name', 'uri', 'created'])
26+
->values([
27+
'fid' => $file['fid'],
28+
'name' => $file['name'],
29+
'uri' => $file['uri'],
30+
'created' => \Drupal::time()->getRequestTime(),
31+
])
32+
->execute();
33+
}
34+
35+
/**
36+
* Return export history entry.
37+
*
38+
* @param int $id
39+
* The entry id.
40+
*
41+
* @return mixed
42+
*/
43+
function backup_db_history_load($id) {
44+
$connection = Database::getConnection();
45+
return $connection->query('SELECT * FROM {backup_db} WHERE eid = :id', [':id' => $id])
46+
->fetchAll();
47+
}
48+
49+
/**
50+
* Delete history entry.
51+
*
52+
* @param int $id
53+
* The file id.
54+
*
55+
* @return bool
56+
* Return the status.
57+
*
58+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
59+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
60+
* @throws \Drupal\Core\Entity\EntityStorageException
61+
*/
62+
function backup_db_history_delete($id) {
63+
$status = FALSE;
64+
65+
// Remove backup_db entry.
66+
\Drupal::database()->delete('backup_db')
67+
->condition('fid', $id)
68+
->execute();
69+
70+
// Remove file and managed entry.
71+
$file = \Drupal::entityTypeManager()->getStorage('file')->load($id);
72+
if ($file) {
73+
$status = $file->delete();
74+
}
75+
76+
return $status;
77+
}
78+
79+
/**
80+
* Return list of all tables.
81+
*/
82+
function backup_db_show_tables() {
83+
$connection = Database::getConnection();
84+
return $connection->query('SHOW TABLES')->fetchAll();
85+
}
86+
87+
/**
88+
* File_create_url in combination with Url::fromUri does not.
89+
*
90+
* Return a correct path for private files.
91+
*
92+
* @param string $uri
93+
* The uri.
94+
*
95+
* @return string
96+
* Return the link.
97+
*/
98+
function backup_db_link($uri) {
99+
$scheme = \Drupal::service('stream_wrapper_manager')->getScheme($uri);
100+
$path = $uri;
101+
if ($scheme == 'private') {
102+
$private_path = PrivateStream::basePath();
103+
$path = str_replace('private:/', $private_path, $uri);
104+
}
105+
return $path;
106+
}
107+
108+
/**
109+
* Format results as select list options.
110+
*/
111+
function _backup_db_format_options($results) {
112+
$options = [];
113+
foreach ($results as $result) {
114+
$value = current((array) $result);
115+
$options[$value] = $value;
116+
}
117+
return $options;
118+
}
119+
120+
/**
121+
* Implements hook_cron().
122+
*/
123+
function backup_db_cron() {
124+
$config = \Drupal::config('backup_db.settings');
125+
if ($config->get('cron_backup_enabled')) {
126+
$interval = (int) $config->get('cron_interval') * 60 * 60;
127+
$expires = Drupal::state()->get('backup_db.cron_last_run') ?: \Drupal::time()->getRequestTime();
128+
$next = Drupal::state()->get('backup_db.cron_next_backup') ?: $expires + $interval;
129+
if (\Drupal::time()->getRequestTime() > $next) {
130+
$client = \Drupal::service('backup_db.client');
131+
// Use the local adapter for cron export.
132+
$handler = new BackupDatabaseLocalAdapter($client);
133+
$handler->export();
134+
\Drupal::state()->set('backup_db.cron_next_backup', \Drupal::time()->getRequestTime() + $interval);
135+
}
136+
\Drupal::state()->set('backup_db.cron_last_run', \Drupal::time()->getRequestTime());
137+
}
138+
}

backup_db.permissions.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
administer backup_db settings:
2+
title: 'Administer BackupDB settings'
3+
description: 'Administer BackupDB settings.'

backup_db.routing.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
backup_db.export:
2+
path: '/admin/config/backup_db'
3+
defaults:
4+
_form: '\Drupal\backup_db\Form\BackupDatabaseForm'
5+
_title: 'Backup Database'
6+
requirements:
7+
_permission: 'administer backup_db settings'
8+
9+
backup_db.export_history:
10+
path: '/admin/config/backup_db/history'
11+
defaults:
12+
_controller: '\Drupal\backup_db\Controller\BackupDatabaseController::historyOverview'
13+
_title: 'Backup Database history'
14+
requirements:
15+
_permission: 'administer backup_db settings'
16+
17+
backup_db.settings:
18+
path: '/admin/config/backup_db/settings'
19+
defaults:
20+
_form: '\Drupal\backup_db\Form\BackupDatabaseSettingsForm'
21+
_title: 'Backup Database settings'
22+
requirements:
23+
_permission: 'administer backup_db settings'
24+
25+
backup_db.cron:
26+
path: '/admin/config/backup_db/cron'
27+
defaults:
28+
_form: '\Drupal\backup_db\Form\BackupDatabaseCronForm'
29+
_title: 'Backup Database Cron settings'
30+
requirements:
31+
_permission: 'administer backup_db settings'
32+
33+
backup_db.tables:
34+
path: '/admin/config/backup_db/tables'
35+
defaults:
36+
_form: '\Drupal\backup_db\Form\BackupDatabaseTablesForm'
37+
_title: 'Backup Database tables'
38+
requirements:
39+
_permission: 'administer backup_db settings'

backup_db.services.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
backup_db.file:
3+
class: Drupal\backup_db\BackupDatabaseFile
4+
backup_db.handler:
5+
class: Drupal\backup_db\BackupDatabaseFileHandler
6+
arguments: [ '@backup_db.file', '@file_system' ]
7+
backup_db.client:
8+
class: Drupal\backup_db\BackupDatabaseClient
9+
arguments: [ '@backup_db.handler', '@database', '@config.factory' ]

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "drupal/backup_db",
3+
"type": "drupal-module",
4+
"description": "Provides database backup solutions via Drupal.",
5+
"homepage": "https://drupal.org/project/backup_db",
6+
"license": "GPL",
7+
"authors": [
8+
{
9+
"name": "George Anderson (geoanders)",
10+
"homepage": "https://www.drupal.org/u/geoanders",
11+
"role": "Maintainer"
12+
}
13+
],
14+
"require": {
15+
"drupal/core": "^8.8 || ^9",
16+
"ifsnop/mysqldump-php": "^2.9"
17+
},
18+
"minimum-stability": "dev"
19+
}

0 commit comments

Comments
 (0)