diff --git a/README.md b/README.md index 049880c..2c1cc22 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,18 @@ Note: Using the localhost IP address as above will route to the default app (cur Note: if the specification for the VM changes, e.g. what's in the `Vagrantfile`, those changes will need to be retrospectively applied to any existing machines. Either `vagrant reload --provision` the existing machine, or `vagrant destroy; vagrant up` to create a fresh machine to this spec. +#### Backup, and restoring from it + +A production install should automatically backup the database and key files to a cloud service. + +A django management command is included to apply this backup to a local machine, i.e. follow the above procedure with this. + +``` +vagrant up +vagrant ssh +python3.6 /folk_rnn_webapp/folk_rnn_site/manage.py apply_backup +``` + #### Tests ``` vagrant up diff --git a/folk_rnn_site/backup/management/commands/applybackup.py b/folk_rnn_site/backup/management/commands/applybackup.py new file mode 100644 index 0000000..63ed985 --- /dev/null +++ b/folk_rnn_site/backup/management/commands/applybackup.py @@ -0,0 +1,45 @@ +import os +import tarfile +import logging +from tempfile import TemporaryDirectory + +from django.core.management.base import BaseCommand +from django.core.management import call_command + +from .backup import Command as BackupCommand + +logger = logging.getLogger(__name__) + +class Command(BaseCommand): + """" + Download the latest production backup and apply to local machine + """ + help = 'Download the latest production backup and apply to local machine' + + def handle(self, *args, **kwargs): + ''' + Process the command (i.e. the django manage.py entrypoint) + ''' + logger.info('Apply Backup starting.') + + backup = BackupCommand() + + with TemporaryDirectory() as tmp: + logger.info('Downloading latest production backup...') + db_path, log_path, tunes_path = backup.download_latest_production_backup(to_dir=tmp) + + logger.info('Applying database...') + with tarfile.open(name=db_path, mode='r:bz2') as tar: + tar.extractall(path=tmp) + call_command('flush', interactive=False) # Ideally drop, create and migrate + call_command('loaddata', os.path.join(tmp, 'db_data.json')) + + logger.info('Applying logs...') + with tarfile.open(name=log_path, mode='r:bz2') as tar: + tar.extractall(path='/') + + logger.info('Applying tune...') + with tarfile.open(name=tunes_path, mode='r:bz2') as tar: + tar.extractall(path='/') + + logger.info('Apply Backup finished.') diff --git a/folk_rnn_site/backup/management/commands/backup.py b/folk_rnn_site/backup/management/commands/backup.py index e7540ec..bdc426f 100755 --- a/folk_rnn_site/backup/management/commands/backup.py +++ b/folk_rnn_site/backup/management/commands/backup.py @@ -217,7 +217,9 @@ def download_latest_production_backup(self, to_dir=''): for name in names: download_path = os.path.join(to_dir, name[2]) self.download_file(name[1], download_path) + name.append(download_path) break + return ([x[3] for x in names]) def delete_file(self, file_id): """