From 385ed8277d1c16051d4c673f6dd0b0ab205f87f8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 10 Jan 2023 07:54:25 +1100 Subject: [PATCH] Configuration options for dbbackup (#4190) * Configuration options for dbbackup Ref: https://github.com/inventree/InvenTree/discussions/4179 * DBBACKUP_SEND_EMAIL is always False * Cleanup settings file * Make backup step optional during update * Change update operation to perform backup by default --- InvenTree/InvenTree/settings.py | 17 +++++++++++++--- InvenTree/config_template.yaml | 5 ++++- tasks.py | 35 ++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index b85c45a3b4..a20f358019 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -147,10 +147,21 @@ STATIC_COLOR_THEMES_DIR = STATIC_ROOT.joinpath('css', 'color-themes').resolve() # Web URL endpoint for served media files MEDIA_URL = '/media/' -# Backup directories -DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage' -DBBACKUP_STORAGE_OPTIONS = {'location': config.get_backup_dir()} +# Database backup options +# Ref: https://django-dbbackup.readthedocs.io/en/master/configuration.html DBBACKUP_SEND_EMAIL = False +DBBACKUP_STORAGE = get_setting( + 'INVENTREE_BACKUP_STORAGE', + 'backup_storage', + 'django.core.files.storage.FileSystemStorage' +) + +# Default backup configuration +DBBACKUP_STORAGE_OPTIONS = get_setting('INVENTREE_BACKUP_OPTIONS', 'backup_options', None) +if DBBACKUP_STORAGE_OPTIONS is None: + DBBACKUP_STORAGE_OPTIONS = { + 'location': config.get_backup_dir(), + } # Application definition diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 2659595381..34311a1b6a 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -142,8 +142,11 @@ cors: # STATIC_ROOT is the local filesystem location for storing static files #static_root: '/home/inventree/data/static' -# BACKUP_DIR is the local filesystem location for storing backups +### Backup configuration options ### +# INVENTREE_BACKUP_DIR is the local filesystem location for storing backups +backup_storage: django.core.files.storage.FileSystemStorage #backup_dir: '/home/inventree/data/backup' +#backup_options: # Background worker options background: diff --git a/tasks.py b/tasks.py index 9503ee068a..0bfff3f39d 100644 --- a/tasks.py +++ b/tasks.py @@ -180,6 +180,14 @@ def translate_stats(c): The file generated from this is needed for the UI. """ + + # Recompile the translation files (.mo) + # We do not run 'invoke translate' here, as that will touch the source (.po) files too! + try: + manage(c, 'compilemessages', pty=True) + except Exception: + print("WARNING: Translation files could not be compiled:") + path = Path('InvenTree', 'script', 'translation_stats.py') c.run(f'python3 {path}') @@ -216,7 +224,7 @@ def restore(c): manage(c, "mediarestore --noinput --uncompress") -@task(pre=[backup, ], post=[rebuild_models, rebuild_thumbnails]) +@task(post=[rebuild_models, rebuild_thumbnails]) def migrate(c): """Performs database migrations. @@ -234,8 +242,13 @@ def migrate(c): print("InvenTree database migrations completed!") -@task(pre=[install, migrate, static, clean_settings, translate_stats]) -def update(c): +@task( + post=[static, clean_settings, translate_stats], + help={ + 'skip_backup': 'Skip database backup step (advanced users)' + } +) +def update(c, skip_backup=False): """Update InvenTree installation. This command should be invoked after source code has been updated, @@ -244,17 +257,21 @@ def update(c): The following tasks are performed, in order: - install + - backup (optional) - migrate - static - clean_settings - translate_stats """ - # Recompile the translation files (.mo) - # We do not run 'invoke translate' here, as that will touch the source (.po) files too! - try: - manage(c, 'compilemessages', pty=True) - except Exception: - print("WARNING: Translation files could not be compiled:") + + # Ensure required components are installed + install(c) + + if not skip_backup: + backup(c) + + # Perform database migrations + migrate(c) # Data tasks