From d749ff9c8f6041d6460a2fad0af99d9dda561d5f Mon Sep 17 00:00:00 2001 From: Daniel Brendel Date: Sun, 25 Aug 2024 12:03:52 +0200 Subject: [PATCH] #247 Cronjob for auto-backups --- README.md | 3 +++ app/commands/MigrationUpgrade.php | 9 +++++++ app/config/routes.php | 2 ++ app/controller/_base.php | 4 ++- app/controller/admin.php | 28 ++++++++++++++++++++ app/controller/cronjobs.php | 43 +++++++++++++++++++++++++++++++ app/lang/da/app.php | 7 ++++- app/lang/de/app.php | 7 ++++- app/lang/en/app.php | 7 ++++- app/lang/es/app.php | 7 ++++- app/lang/fr/app.php | 7 ++++- app/lang/pl/app.php | 7 ++++- app/migrations/AppModel.php | 2 ++ app/views/admin.php | 32 +++++++++++++++++++++++ 14 files changed, 158 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 986af62..cfdbb21 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,9 @@ GET /cronjob/tasks/tomorrow?cronpw={your-auth-token} # Used to inform users about due calendar dates GET /cronjob/calendar/reminder?cronpw={your-auth-token} + +# Used to perform the automatic backup of your workspace data +GET /cronjob/backup/auto ``` ## Application testing diff --git a/app/commands/MigrationUpgrade.php b/app/commands/MigrationUpgrade.php index 10a30b1..abd7ccc 100644 --- a/app/commands/MigrationUpgrade.php +++ b/app/commands/MigrationUpgrade.php @@ -8,6 +8,15 @@ * Command handler class */ class MigrationUpgrade implements Asatru\Commands\Command { + /** + * @return void + */ + public function upgradeTo3dot4() + { + AppModel::raw('ALTER TABLE `' . AppModel::tableName() . '` ADD COLUMN IF NOT EXISTS auto_backup BOOLEAN NOT NULL DEFAULT 0'); + AppModel::raw('ALTER TABLE `' . AppModel::tableName() . '` ADD COLUMN IF NOT EXISTS backup_path VARCHAR(1024) NULL'); + } + /** * @return void */ diff --git a/app/config/routes.php b/app/config/routes.php index 9baf09f..1649994 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -130,6 +130,7 @@ return [ array('/admin/mail/save', 'POST', 'admin@save_mail_settings'), array('/admin/themes/import', 'POST', 'admin@import_theme'), array('/admin/themes/remove', 'POST', 'admin@remove_theme'), + array('/admin/backup/cronjob/save', 'POST', 'admin@save_backup_cronjob_settings'), array('/admin/cronjob/token', 'POST', 'admin@generate_cronjob_token'), array('/admin/weather/save', 'POST', 'admin@save_weather_data'), array('/admin/api/add', 'ANY', 'admin@add_api_key'), @@ -140,6 +141,7 @@ return [ array('/cronjob/tasks/overdue', 'GET', 'cronjobs@overdue_tasks'), array('/cronjob/tasks/tomorrow', 'GET', 'cronjobs@tomorrow_tasks'), array('/cronjob/calendar/reminder', 'GET', 'cronjobs@calendar_reminder'), + array('/cronjob/backup/auto', 'GET', 'cronjobs@auto_backup'), /** Share Controller */ array('/share/photo/post', 'POST', 'share@share_photo'), diff --git a/app/controller/_base.php b/app/controller/_base.php index 56df40d..f4dab41 100644 --- a/app/controller/_base.php +++ b/app/controller/_base.php @@ -36,7 +36,9 @@ class BaseController extends Asatru\Controller\Controller { '/password/restore', '/password/reset', '/cronjob/tasks/overdue', - '/cronjob/tasks/tomorrow' + '/cronjob/tasks/tomorrow', + '/cronjob/calendar/reminder', + '/cronjob/backup/auto' ); if (!in_array($url, $allowed_urls)) { diff --git a/app/controller/admin.php b/app/controller/admin.php index 4de7f23..311f64a 100644 --- a/app/controller/admin.php +++ b/app/controller/admin.php @@ -808,4 +808,32 @@ class AdminController extends BaseController { ]); } } + + /** + * Handles URL: /admin/backup/cronjob/save + * + * @param Asatru\Controller\ControllerArg $request + * @return Asatru\View\JsonHandler + */ + public function save_backup_cronjob_settings($request) + { + try { + $auto_backup = (bool)$request->params()->query('auto_backup', 0); + $backup_path = $request->params()->query('backup_path', null); + + $set = [ + 'auto_backup' => $auto_backup, + 'backup_path' => $backup_path + ]; + + AppModel::updateSet($set); + + FlashMessage::setMsg('success', __('app.backup_settings_stored')); + + return redirect('/admin?tab=backup'); + } catch (\Exception $e) { + FlashMessage::setMsg('error', $e->getMessage()); + return back(); + } + } } diff --git a/app/controller/cronjobs.php b/app/controller/cronjobs.php index 2023c45..93d4966 100644 --- a/app/controller/cronjobs.php +++ b/app/controller/cronjobs.php @@ -85,4 +85,47 @@ class CronjobsController extends BaseController { ]); } } + + /** + * Handles URL: /cronjob/backup/auto + * + * @param Asatru\Controller\ControllerArg $request + * @return Asatru\View\JsonHandler + */ + public function auto_backup($request) + { + try { + if (!app('auto_backup')) { + throw new \Exception(__('app.auto_backup_not_active')); + } + + $file_name = BackupModule::start([ + 'locations' => true, + 'plants' => true, + 'gallery' => true, + 'tasks' => true, + 'inventory' => true, + 'calendar' => true + ]); + + $storage_path = app('backup_path'); + if ((is_string($storage_path)) && (strlen($storage_path) > 0) && (is_dir($storage_path))) { + if (strpos($storage_path, '/', -1) === false) { + $storage_path .= '/'; + } + + copy(public_path() . '/backup/' . $file_name, $storage_path . $file_name); + unlink(public_path() . '/backup/' . $file_name); + } + + return json([ + 'code' => 200 + ]); + } catch (\Exception $e) { + return json([ + 'code' => 500, + 'msg' => $e->getMessage() + ]); + } + } } diff --git a/app/lang/da/app.php b/app/lang/da/app.php index f8f6798..7debb5e 100644 --- a/app/lang/da/app.php +++ b/app/lang/da/app.php @@ -412,5 +412,10 @@ return [ 'photo_edit_specify_file' => 'Vælg en fil, der skal uploades', 'photo_edit_url_placeholder' => 'Indtast URL...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Aktiver automatisk sikkerhedskopiering', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Valgfri sti til at gemme backup-filer', + 'backup_settings_stored' => 'Backup cronjob-indstillinger blev gemt', + 'auto_backup_not_active' => 'Auto-backup-funktionen er ikke blevet aktiveret endnu' ]; diff --git a/app/lang/de/app.php b/app/lang/de/app.php index 0fe7f8b..1e91bd7 100644 --- a/app/lang/de/app.php +++ b/app/lang/de/app.php @@ -413,5 +413,10 @@ return [ 'photo_edit_specify_file' => 'Photo von einem Datenträger hochladen', 'photo_edit_url_placeholder' => 'URL hier eingeben...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Automatisches Backup aktivieren', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Optionaler Speicherort der Backup-Dateien', + 'backup_settings_stored' => 'Die Backup Cronjob Einstellungen wurden erfolgreich gespeichert', + 'auto_backup_not_active' => 'Das automatische Backup ist nicht aktiviert' ]; \ No newline at end of file diff --git a/app/lang/en/app.php b/app/lang/en/app.php index 87bcd5b..be32e5c 100644 --- a/app/lang/en/app.php +++ b/app/lang/en/app.php @@ -413,5 +413,10 @@ return [ 'photo_edit_specify_file' => 'Select a file to upload', 'photo_edit_url_placeholder' => 'Enter URL...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Enable Auto-Backup', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Optional path to store backup files', + 'backup_settings_stored' => 'Backup cronjob settings were saved succesfully', + 'auto_backup_not_active' => 'The auto-backup feature has not been activated yet' ]; \ No newline at end of file diff --git a/app/lang/es/app.php b/app/lang/es/app.php index bacd01f..a696e34 100644 --- a/app/lang/es/app.php +++ b/app/lang/es/app.php @@ -413,5 +413,10 @@ return [ 'photo_edit_specify_file' => 'Seleccione un archivo para cargar', 'photo_edit_url_placeholder' => 'Introducir URL...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Habilitar copia de seguridad automática', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Ruta opcional para almacenar archivos de respaldo', + 'backup_settings_stored' => 'La configuración del cronjob de respaldo se guardó exitosamente', + 'auto_backup_not_active' => 'La función de copia de seguridad automática aún no se ha activado' ]; diff --git a/app/lang/fr/app.php b/app/lang/fr/app.php index de30a7e..0dfe1b0 100644 --- a/app/lang/fr/app.php +++ b/app/lang/fr/app.php @@ -414,5 +414,10 @@ return [ 'photo_edit_specify_file' => 'Sélectionnez un fichier à télécharger', 'photo_edit_url_placeholder' => 'Entrer l\'URL...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Activer la sauvegarde automatique', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Chemin facultatif pour stocker les fichiers de sauvegarde', + 'backup_settings_stored' => 'Les paramètres de sauvegarde de la tâche cron ont été enregistrés avec succès', + 'auto_backup_not_active' => 'La fonction de sauvegarde automatique n\'a pas encore été activée' ]; diff --git a/app/lang/pl/app.php b/app/lang/pl/app.php index c98419e..37005a9 100644 --- a/app/lang/pl/app.php +++ b/app/lang/pl/app.php @@ -413,5 +413,10 @@ return [ 'photo_edit_specify_file' => 'Wybierz plik do przesłania', 'photo_edit_url_placeholder' => 'Wprowadź URL...', 'donation_sponsoring' => 'GitHub Sponsoring', - 'donation_kofi' => 'Buy Me a Coffee' + 'donation_kofi' => 'Buy Me a Coffee', + 'auto_backup' => 'Włącz automatyczną kopię zapasową', + 'auto_backup_cronjob_url' => 'Cronjob URL', + 'backup_path' => 'Opcjonalna ścieżka do przechowywania plików kopii zapasowych', + 'backup_settings_stored' => 'Ustawienia kopii zapasowej zadania cronjob zostały pomyślnie zapisane', + 'auto_backup_not_active' => 'Funkcja automatycznego tworzenia kopii zapasowych nie została jeszcze aktywowana' ]; \ No newline at end of file diff --git a/app/migrations/AppModel.php b/app/migrations/AppModel.php index c539789..32237ae 100644 --- a/app/migrations/AppModel.php +++ b/app/migrations/AppModel.php @@ -59,6 +59,8 @@ class AppModel_Migration { $this->database->add('owm_cache INT NOT NULL DEFAULT 300'); $this->database->add('allow_custom_attributes BOOLEAN NOT NULL DEFAULT 0'); $this->database->add('system_message_plant_log BOOLEAN NOT NULL DEFAULT 1'); + $this->database->add('auto_backup BOOLEAN NOT NULL DEFAULT 0'); + $this->database->add('backup_path VARCHAR(1024) NULL'); $this->database->add('created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP'); $this->database->create(); } diff --git a/app/views/admin.php b/app/views/admin.php index a03fb93..92ffb07 100644 --- a/app/views/admin.php +++ b/app/views/admin.php @@ -606,6 +606,38 @@
+
+ @csrf + +
+
+  {{ __('app.auto_backup') }} +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+
+ +
+

{{ __('app.import') }}