Added backup management command

This commit is contained in:
Fergal Moran
2015-09-13 19:14:00 +01:00
parent b0aa02a632
commit ed0502a217
3 changed files with 85 additions and 15 deletions

View File

@@ -21,6 +21,8 @@ ADMINS = (
('Fergal Moran', 'fergal.moran@gmail.com'),
)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
MANAGERS = ADMINS
AUTH_PROFILE_MODULE = 'spa.UserProfile'

View File

@@ -22,7 +22,7 @@ def _check_missing_mixes():
file = '/mnt/dev/working/Dropbox/Development/deepsouthsounds.com/media/mixes/{0}.mp3'.format(m.uid)
if os.path.isfile(file):
print(('* {0}'.format(file)))
#cdn.upload_file_to_azure(file, '{0}.mp3'.format(m.uid), 'mixes')
# cdn.upload_file_to_azure(file, '{0}.mp3'.format(m.uid), 'mixes')
found += 1
else:
found += 1
@@ -32,7 +32,6 @@ def _check_missing_mixes():
class Command(LabelCommand):
def handle_label(self, label, **options):
pass
@@ -49,21 +48,9 @@ class Command(LabelCommand):
print("Fatal error, bailing. {0}".format(ex.message))
def handle(self, *labels, **options):
verbosity = int(options.get('verbosity'))
# Django 1.4 compatibility fix
stdout = options.get('stdout', None)
stdout = stdout if stdout else sys.stdout
stderr = options.get('stderr', None)
stderr = stderr if stderr else sys.stderr
if not labels:
print(self.print_help('thumbnail', ''), file=stderr)
sys.exit(1)
if len(labels) != 1:
raise CommandError('`%s` is not a valid argument' % labels)
label = labels[0]
if label not in ['check_missing_mixes', 'update_azure_headers']:

View File

@@ -0,0 +1,81 @@
from django.core.management.base import LabelCommand, CommandError
from subprocess import Popen, PIPE, STDOUT
from dss import settings
import tarfile
import dropbox
import os, time
def _backup_database():
print("Creating database backup")
args = []
args += ["--username=%s" % settings.DATABASE_USER]
args += ["--password"]
args += ["--host=%s" % settings.DATABASE_HOST]
args += [settings.DATABASE_NAME]
remote_file = "{0}.tar.gz".format(time.strftime("%Y%m%d-%H%M%S"))
pipe = Popen('pg_dump %s > %s' % (' '.join(args), remote_file), shell=True, stdin=PIPE)
if settings.DATABASE_PASSWORD:
output = pipe.communicate(input=bytes(settings.DATABASE_PASSWORD + '\n', 'UTF-8'))
print(output)
_create_backup_bundle(remote_file, 'databases', settings.PROJECT_ROOT)
def _backup_settings():
print("Creating settings backup")
file_name = "{0}.tar.gz".format(time.strftime("%Y%m%d-%H%M%S"))
_create_backup_bundle(file_name, 'settings', settings.PROJECT_ROOT)
def _progress_filter(tarinfo):
print("Adding: {}".format(tarinfo.name, 0, 1))
return tarinfo
def _create_backup_bundle(remote_file, type, location):
backup_file = "{0}/{1}".format(settings.DSS_TEMP_PATH, remote_file)
tar = tarfile.open(backup_file, "w:gz")
tar.add(location, arcname=remote_file, filter=_progress_filter)
tar.close()
_upload_to_dropbox(type, backup_file, remote_file)
def _upload_to_dropbox(type, backup_file, remote_file):
print("Uploading {0} to dropbox".format(backup_file))
with open(backup_file, "rb") as f:
client = dropbox.client.DropboxClient(settings.DSS_DB_BACKUP_TOKEN)
response = client.put_file("{0}/{1}".format(type, remote_file), f, overwrite=True)
os.remove(backup_file)
print(response)
def _backup_media():
print("Creating media backup")
file_name = "{0}.tar.gz".format(time.strftime("%Y%m%d-%H%M%S"))
_create_backup_bundle(file_name, 'media', settings.MEDIA_ROOT)
class Command(LabelCommand):
help = (
"Handles thumbnails and key value store"
)
missing_args_message = "Enter one of [database, settings, media, all]"
def handle_label(self, label, **options):
if label == "database":
_backup_database()
if label == "settings":
_backup_settings()
if label == "media":
_backup_media()
if label == "all":
_backup_database()
_backup_settings()
_backup_media()