mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 13:05:42 +00:00
Add db an media backups (#3783)
* [FR] Add backup into inventrees lifecycle Fixes #3778 * Add env to testing enviros * block backup from running app commands * Add more commands * fix postgres version * Update used env * add daily task to run backups * add installer changes
This commit is contained in:
@ -117,6 +117,12 @@ class InvenTreeConfig(AppConfig):
|
||||
schedule_type=Schedule.DAILY
|
||||
)
|
||||
|
||||
# Make regular backups
|
||||
InvenTree.tasks.schedule_task(
|
||||
'InvenTree.tasks.run_backup',
|
||||
schedule_type=Schedule.DAILY,
|
||||
)
|
||||
|
||||
def update_exchange_rates(self): # pragma: no cover
|
||||
"""Update exchange rates each time the server is started.
|
||||
|
||||
|
@ -160,6 +160,22 @@ def get_static_dir(create=True):
|
||||
return sd
|
||||
|
||||
|
||||
def get_backup_dir(create=True):
|
||||
"""Return the absolute path for the backup directory"""
|
||||
|
||||
bd = get_setting('INVENTREE_BACKUP_DIR', 'backup_dir')
|
||||
|
||||
if not bd:
|
||||
raise FileNotFoundError('INVENTREE_BACKUP_DIR not specified')
|
||||
|
||||
bd = Path(bd).resolve()
|
||||
|
||||
if create:
|
||||
bd.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
return bd
|
||||
|
||||
|
||||
def get_plugin_file():
|
||||
"""Returns the path of the InvenTree plugins specification file.
|
||||
|
||||
|
@ -35,6 +35,12 @@ def canAppAccessDatabase(allow_test: bool = False, allow_plugins: bool = False):
|
||||
'collectstatic',
|
||||
'makemessages',
|
||||
'compilemessages',
|
||||
'backup',
|
||||
'dbbackup',
|
||||
'mediabackup',
|
||||
'restore',
|
||||
'dbrestore',
|
||||
'mediarestore',
|
||||
]
|
||||
|
||||
if not allow_test:
|
||||
|
@ -131,6 +131,11 @@ 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()}
|
||||
DBBACKUP_SEND_EMAIL = False
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
@ -176,6 +181,7 @@ INSTALLED_APPS = [
|
||||
'error_report', # Error reporting in the admin interface
|
||||
'django_q',
|
||||
'formtools', # Form wizard tools
|
||||
'dbbackup', # Backups - django-dbbackup
|
||||
|
||||
'allauth', # Base app for SSO
|
||||
'allauth.account', # Extend user with accounts
|
||||
|
@ -9,6 +9,7 @@ from datetime import timedelta
|
||||
from django.conf import settings
|
||||
from django.core import mail as django_mail
|
||||
from django.core.exceptions import AppRegistryNotReady
|
||||
from django.core.management import call_command
|
||||
from django.db.utils import OperationalError, ProgrammingError
|
||||
from django.utils import timezone
|
||||
|
||||
@ -272,6 +273,12 @@ def update_exchange_rates():
|
||||
logger.error(f"Error updating exchange rates: {e}")
|
||||
|
||||
|
||||
def run_backup():
|
||||
"""Run the backup command."""
|
||||
call_command("dbbackup", noinput=True, clean=True, compress=True, interactive=False)
|
||||
call_command("mediabackup", noinput=True, clean=True, compress=True, interactive=False)
|
||||
|
||||
|
||||
def send_email(subject, body, recipients, from_email=None, html_message=None):
|
||||
"""Send an email with the specified subject and body, to the specified recipients list."""
|
||||
if type(recipients) == str:
|
||||
|
@ -142,6 +142,9 @@ 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_dir: '/home/inventree/data/backup'
|
||||
|
||||
# Background worker options
|
||||
background:
|
||||
workers: 4
|
||||
|
Reference in New Issue
Block a user