Skip to content

Commit

Permalink
Django management command: applybackup
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyspark committed Nov 23, 2018
1 parent 1baff1a commit 3503212
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions 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.')
2 changes: 2 additions & 0 deletions folk_rnn_site/backup/management/commands/backup.py
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 3503212

Please sign in to comment.