-
Notifications
You must be signed in to change notification settings - Fork 5
/
applybackup.py
45 lines (34 loc) · 1.58 KB
/
applybackup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.')