-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
119 lines (104 loc) · 3.29 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php namespace Octobro\BackupLog;
use Artisan, Backend;
use System\Classes\PluginBase;
use System\Classes\SettingsManager;
use Octobro\BackupLog\Models\BackupLog;
/**
* Plugin Information File
*
* @link https://docs.octobercms.com/3.x/extend/system/plugins.html
*/
class Plugin extends PluginBase
{
/**
* pluginDetails about this plugin.
*/
public function pluginDetails()
{
return [
'name' => 'BackupLog',
'description' => 'No description provided yet...',
'author' => 'octobro',
'icon' => 'icon-leaf'
];
}
/**
* register method, called when the plugin is first registered.
*/
public function register()
{
//
}
/**
* boot method, called right before the request route.
*/
public function boot()
{
//
}
/**
* registerComponents used by the frontend.
*/
public function registerComponents()
{
return []; // Remove this line to activate
return [
'Octobro\BackupLog\Components\MyComponent' => 'myComponent',
];
}
/**
* registerPermissions used by the backend.
*/
public function registerPermissions()
{
return []; // Remove this line to activate
return [
'octobro.backuplog.some_permission' => [
'tab' => 'BackupLog',
'label' => 'Some permission'
],
];
}
/**
* registerSettings used by the backend.
*/
public function registerSettings()
{
return [
'backup_logs' => [
'label' => 'backup Log',
'description' => "View Backup log with their recorded time and details.",
'category' => SettingsManager::CATEGORY_LOGS,
'icon' => 'icon-database',
'url' => Backend::url('octobro/backuplog/backuplogs'),
'permissions' => [],
'order' => 1002,
],
];
}
public function registerSchedule($schedule)
{
$this->scheduleBackupDatabase($schedule);
}
protected function scheduleBackupDatabase($schedule)
{
$backup_trigger_function = env('BACKUP_LOG_TRIGGER_FUNCTION', 'dailyAt');
$backup_trigger_at = env('BACKUP_LOG_TRIGGER_AT', '23:59');
$backup_trigger_in_day = env('BACKUP_LOG_TRIGGER_IN_DAY');
$backup_trigger_override = env('BACKUP_LOG_OVERRIDE_SCHEDULE', false);
$backup_is_today = filled($backup_trigger_in_day) ? today()->firstOfMonth()->addDays($backup_trigger_in_day)->isToday() : today()->endOfMonth()->isToday();
$schedule
->call(fn() => $this->runBackupCommand('backup:run --only-db'))
->when(boolval($backup_is_today) || boolval($backup_trigger_override))
->{$backup_trigger_function}($backup_trigger_function == 'dailyAt' ? $backup_trigger_at : null);
}
protected function runBackupCommand($command)
{
if(strpos($command, 'run') !== false){
$filename = 'octobro_backup_db_' . now()->format('jMy_H-i') . '.zip';
$command = sprintf('%s --filename=%s', $command, $filename);
}
Artisan::call($command);
BackupLog::record($filename);
}
}