Skip to content

Commit 7698e91

Browse files
Rike-czjan-vince
andauthored
Upgrade on October CMS 4.x
- harmonized with Laravel 12 and October CMS 4, - removed Doctrine at all, - added automatic backup using prepared URL for external cron jobs, - added the ability to backup more than the default theme. --------- Co-authored-by: Jan Vince <jan@vince.cz>
1 parent 70469cf commit 7698e91

File tree

18 files changed

+360
-205
lines changed

18 files changed

+360
-205
lines changed

Plugin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ public function register()
4444
*/
4545
public function registerSchedule($schedule)
4646
{
47-
if (Models\Settings::get('db_auto')) {
47+
if (Models\Settings::get('db_mode') == 'schedule') {
4848
// $schedule->command('smallbackup:db')->daily();
4949
// Workaround because of shared hostings disables proc_open
5050
$schedule->call(function () {
5151
Artisan::call('smallbackup:db');
5252
})->daily();
5353
}
54-
if (Models\Settings::get('theme_auto')) {
54+
if (Models\Settings::get('theme_mode') == 'schedule') {
5555
// $schedule->command('smallbackup:theme')->daily();
5656
$schedule->call(function () {
5757
Artisan::call('smallbackup:theme');
5858
})->daily();
5959
}
60-
if (Models\Settings::get('storage_auto')) {
60+
if (Models\Settings::get('storage_mode') == 'schedule') {
6161
// $schedule->command('smallbackup:storage')->daily();
6262
$schedule->call(function () {
6363
Artisan::call('smallbackup:storage');
@@ -80,7 +80,7 @@ public function registerSettings()
8080
'icon' => 'icon-database',
8181
'class' => Models\Settings::class,
8282
'url' => \Backend::url('webula/smallbackup/settings/update'),
83-
'keywords' => 'database backup',
83+
'keywords' => 'backup database theme storage',
8484
'order' => 991,
8585
'permissions' => ['webula.smallbackup.access_settings'],
8686
]

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Small Backup
22
> Simple backup for database (MySQL, SQLite), active theme and CMS storages
33
4+
## Compatibility
5+
6+
* plugin in version from 2.0.0. is intended **only for OctoberCMS v4 and newer**
7+
* [older versions](https://github.com/webula-cz/smallbackup/tree/v1) (1.x.x) will run fine only on OctoberCMS v1, v2 and v3
8+
49

510
## Installation
611

@@ -61,7 +66,18 @@ There are default scheduler jobs for database and active theme to be backed up o
6166

6267
### Automatic backup (without scheduler)
6368

64-
If you cannot run Cron command directly on your server/hosting, you can create custom CMS page like this:
69+
If you cannot run Cron command directly on your server/hosting, you can:
70+
71+
#### Use prepared URL
72+
73+
In plugin's backend settings page go to Settings tab and copy fixed part of the URL (which can be changed in plugin's `config/config.php` file) and a variable part (which can be changed here in Settings tab).
74+
75+
The final URL is in form: `https://www.domain.com/webula/smallbackup/run/trigger-46d62cbc-5e27-4bb0-87dd-19bb0012345678`
76+
77+
Don't forget to allow backups from external URL in plugin's settings tabs (Database, Theme, Storage).
78+
79+
#### Use custom CMS page
80+
6581

6682
```
6783
title = "artisan"
@@ -82,7 +98,7 @@ is_hidden = 0
8298

8399
### Manual backup
84100

85-
You can create manual backup in plugin's Settings by clicking button `Backup now` on Database or Theme tab.
101+
You can create manual backup in plugin's Settings by clicking button `Backup now` on Database, Theme or Storage tab.
86102

87103
#### Console commands
88104

classes/DbBackupManager.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function backup(string $resource = null, bool $once = false): string
3434
if (!$once || !File::exists($this->getUseCompression() ? $pathname . '.zip' : $pathname)) {
3535
$stream = (new Drivers\Mysql(
3636
Db::connection($connectionName),
37-
$this->getExcludedTables(),
38-
$this->getCustomMapping()
37+
$this->getExcludedTables()
3938
))->backupStream();
4039
File::put(
4140
$pathname,
@@ -95,14 +94,4 @@ protected function getUseCompression(): bool
9594
{
9695
return boolval(Settings::get('db_use_compression'));
9796
}
98-
99-
/**
100-
* Get custom columns mapping for db backup
101-
*
102-
* @return array
103-
*/
104-
protected function getCustomMapping(): array
105-
{
106-
return array_pluck((array)Settings::get('db_custom_mapping'), 'doctrine_type', 'db_type');
107-
}
10897
}

classes/ThemeBackupManager.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use File;
44
use Exception;
55
use Cms\Classes\Theme;
6+
use Webula\SmallBackup\Models\Settings;
67
use October\Rain\Filesystem\Zip;
78

89
class ThemeBackupManager extends BackupManager
@@ -15,30 +16,54 @@ class ThemeBackupManager extends BackupManager
1516
protected $prefix = 'wsb-theme-';
1617

1718
/**
18-
* Backup Theme(s) by connection name (null = default)
19+
* Backup Theme(s) by connection name (null = default + included)
1920
*
2021
* @param string|null $resource
2122
* @param bool $once do not overwrite existing backup file
2223
* @return string backup file
2324
*/
2425
public function backup(string $resource = null, bool $once = false): string
2526
{
26-
$themeName = $resource ?: Theme::getActiveThemeCode();
27+
if ($resource) {
28+
$themes[] = $resource;
29+
} else {
30+
$themes = [
31+
Theme::getActiveThemeCode(),
32+
...$this->getAdditionalThemes()
33+
];
34+
}
2735

28-
if ($themeName && File::isDirectory(themes_path($themeName))) {
29-
$filename = $this->prefix . str_slug($themeName) . '-' . now()->format('Y-m-d') . '.zip';
30-
$pathname = $this->folder . DIRECTORY_SEPARATOR . $filename;
36+
$pathnames = [];
37+
foreach ($themes as $themeName) {
38+
if ($themeName && File::isDirectory(themes_path($themeName))) {
39+
$filename = $this->prefix . str_slug($themeName) . '-' . now()->format('Y-m-d') . '.zip';
40+
$pathname = $this->folder . DIRECTORY_SEPARATOR . $filename;
3141

32-
if (!$once || !File::exists($pathname)) {
33-
Zip::make(
34-
$pathname,
35-
themes_path($themeName)
36-
);
42+
if (!$once || !File::exists($pathname)) {
43+
Zip::make(
44+
$pathname,
45+
themes_path($themeName)
46+
);
47+
}
48+
$pathnames[] = $pathname;
49+
} else {
50+
throw new Exception(trans('webula.smallbackup::lang.backup.flash.unknown_theme', ['theme' => $themeName]));
3751
}
38-
39-
return $pathname;
40-
} else {
41-
throw new Exception(trans('webula.smallbackup::lang.backup.flash.unknown_theme', ['theme' => $themeName]));
4252
}
53+
54+
return implode(', ', $pathnames);
55+
}
56+
57+
/**
58+
* Get list of additional themes for theme backup
59+
*
60+
* @return array
61+
*/
62+
protected function getAdditionalThemes(): array
63+
{
64+
$data = Settings::get('theme_additional_themes');
65+
return $data
66+
? (is_array($data) ? $data : explode(',', $data))
67+
: [];
4368
}
4469
}

classes/drivers/Mysql.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ class Mysql implements Contracts\BackupStream
1919
protected $excludedTables = [];
2020

2121

22-
public function __construct(Connection $connection, array $excludedTables = [], array $customMapping = [])
22+
public function __construct(Connection $connection, array $excludedTables = [])
2323
{
2424
$this->connection = $connection;
2525
$this->excludedTables = $excludedTables;
26-
27-
$this->syncPlatformMapping($customMapping);
2826
}
2927

3028
/**
@@ -39,13 +37,7 @@ public function backupStream(): string
3937
$this->line() . PHP_EOL
4038
;
4139

42-
$tables = collect($this->getListOfTables())
43-
->map(function ($item) {
44-
return $item->getName();
45-
})
46-
->diff($this->excludedTables)
47-
->toArray();
48-
40+
$tables = array_diff($this->getListOfTables(), $this->excludedTables);
4941
foreach ($tables as $table) {
5042
$stream .= $this->comment('Table structure: ' . $this->wrapTable($table)) . PHP_EOL .
5143
$this->getDropTableStatement($table) . PHP_EOL .
@@ -56,13 +48,7 @@ public function backupStream(): string
5648
;
5749
}
5850

59-
$views = collect($this->getListOfViews())
60-
->map(function ($item) {
61-
return $item->getName();
62-
})
63-
->diff($this->excludedTables)
64-
->toArray();
65-
51+
$views = array_diff($this->getListOfViews(), $this->excludedTables);
6652
foreach ($views as $view) {
6753
$stream .= $this->comment('View structure: ' . $this->wrapTable($view)) . PHP_EOL .
6854
$this->getDropViewStatement($view) . PHP_EOL .
@@ -250,7 +236,8 @@ protected function getDatabaseName(): string
250236
*/
251237
protected function getListOfTables(): array
252238
{
253-
return $this->connection->getDoctrineSchemaManager()->listTables();
239+
$list = $this->connection->getSchemaBuilder()->getTables($this->getDatabaseName());
240+
return array_column($list, 'name');
254241
}
255242

256243
/**
@@ -260,7 +247,8 @@ protected function getListOfTables(): array
260247
*/
261248
protected function getListOfViews(): array
262249
{
263-
return $this->connection->getDoctrineSchemaManager()->listViews();
250+
$list = $this->connection->getSchemaBuilder()->getViews($this->getDatabaseName());
251+
return array_column($list, 'name');
264252
}
265253

266254
/**
@@ -271,11 +259,8 @@ protected function getListOfViews(): array
271259
*/
272260
protected function getListOfColumns(string $table): array
273261
{
274-
return collect($this->connection->getDoctrineSchemaManager()->listTableColumns($table))
275-
->map(function ($item) {
276-
return $item->getName();
277-
})
278-
->toArray();
262+
$list = $this->connection->getSchemaBuilder()->getColumns($table);
263+
return array_column($list, 'name');
279264
}
280265

281266
/**
@@ -289,29 +274,6 @@ protected function getCharset(): string
289274
}
290275

291276

292-
/**
293-
* Sync doctrine platform mapping
294-
*
295-
* @param array $customMapping [original_type => backup_type]
296-
* @return void
297-
*/
298-
protected function syncPlatformMapping(array $customMapping = []): void
299-
{
300-
$platform = $this->connection->getDoctrineSchemaManager()->getDatabasePlatform();
301-
if (!$platform->hasDoctrineTypeMappingFor('json')) {
302-
$platform->registerDoctrineTypeMapping('json', 'text');
303-
}
304-
if (!$platform->hasDoctrineTypeMappingFor('enum')) {
305-
$platform->registerDoctrineTypeMapping('enum', 'string');
306-
}
307-
if (!empty($customMapping)) {
308-
foreach ($customMapping as $db_type => $doctrine_type) {
309-
$platform->registerDoctrineTypeMapping($db_type, $doctrine_type);
310-
}
311-
}
312-
}
313-
314-
315277
/**
316278
* Wrap table/database name
317279
*

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webula/smallbackup-plugin",
3-
"description": "Backup databases and theme",
3+
"description": "Backup OctoberCMS databases, themes and storages",
44
"homepage": "https://www.webula.cz",
55
"type": "october-plugin",
66
"authors": [
@@ -17,7 +17,8 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=7.2",
20+
"php": "^8.2",
21+
"october/rain": "^4.0",
2122
"composer/installers": "~1.0"
2223
},
2324
"autoload" : {

config/config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
return [
3+
'trigger' => [
4+
'url_prefix' => 'webula/smallbackup/run/',
5+
],
6+
];

0 commit comments

Comments
 (0)