diff --git a/.gitignore b/.gitignore index 2493a839..4e071ff2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,8 +9,6 @@ local_settings.py .DS_Store local_templates/ local_templates_hidden/ -backups -backup/ django.log .vagrant modernomad.komodoproject diff --git a/core/tasks.py b/core/tasks.py index 53316d92..78bc132d 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -1,7 +1,6 @@ from core.models import Location, Subscription, Use from core.emails.messages import guests_residents_daily_update, admin_daily_update, guest_welcome, goodbye_email from django.conf import settings -from modernomad.backup import BackupManager from modernomad.log import catch_exceptions from django.contrib.sites.models import Site import datetime @@ -54,13 +53,6 @@ def send_departure_email(): goodbye_email(use) -@catch_exceptions -def make_backup(): - logger.info("Running task: make_backup") - manager = BackupManager() - manager.make_backup() - - @catch_exceptions def generate_subscription_bills(): logger.info("Running task: generate_subscription_bills") diff --git a/modernomad/backup.py b/modernomad/backup.py deleted file mode 100644 index 6a4dd179..00000000 --- a/modernomad/backup.py +++ /dev/null @@ -1,149 +0,0 @@ -import os -import time -import urllib -import sys -import datetime -import logging -import tempfile -import shutil - -logger = logging.getLogger(__name__) - -from django.conf import settings -from django.utils import timezone - -class BackupError(Exception): - pass - -class BackupManager(object): - def __init__(self): - pass - - def call_system(self, command): - if os.system(command) == 0: return True - logger.debug('FAILED:', command) - return False - - def get_db_info(self): - if not hasattr(settings, 'DATABASES'): raise BackupError('settings.DATABASES is not defined') - if not settings.DATABASES.has_key('default'): raise BackupError('settings.DATABASES has no default db') - if settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql_psycopg2': raise BackupError('This command only works with PostgreSQL') - if settings.DATABASES['default'].has_key('PASSWORD'): - password = settings.DATABASES['default']['PASSWORD'] - else: - password = None - return (settings.DATABASES['default']['USER'], settings.DATABASES['default']['NAME'], password) - - def restore_backup(self, file_path): - backup_path = os.path.realpath(file_path) - if not os.path.exists(backup_path): raise BackupError('The backup file "%s" does not exist.' % backup_path) - if not os.path.isfile(backup_path): raise BackupError('The specified backup file "%s" is not a file.' % backup_path) - if not backup_path.endswith('.tar'): raise BackupError('The specified backup file "%s" must be a tar file.' % backup_path) - - self.check_dirs() - db_user, db_name, db_password = self.get_db_info() - - logger.debug('Restoring from backup file "%s"' % backup_path) - - # create the working directory - working_dir = tempfile.mkdtemp('backup-temp') - - # untar the backup file, which should result in two files: sql and media - command = 'cd "%s" && tar -xzf "%s"' % (working_dir, backup_path) - if not self.call_system(command): raise BackupError('Aborting restoration.') - - # create a sub directory for the media, and untar it - command = 'cd "%s" && tar -xzf %s/*-media.tgz' % (working_dir, working_dir) - if not self.call_system(command): raise BackupError('Aborting restoration.') - - # move each media dir from the temp media dir into the project media dir - media_dir = os.path.join(working_dir, 'media') - if not os.path.exists(media_dir): raise BackupError('Could not restore the media dir') - for media_file in os.listdir(media_dir): - target = os.path.join(settings.MEDIA_ROOT, media_file) - if os.path.exists(target) and os.path.isdir(target): shutil.rmtree(target) - if os.path.exists(target): os.remove(target) - shutil.move(os.path.join(media_dir, media_file), target) - - if db_password: os.environ['PGPASSWORD'] = db_password - - # now delete and recreate the database - command = 'echo "drop database %s; create database %s; grant all on database %s to %s;" | psql -U %s' % (db_name, db_name, db_name, db_user, db_user) - if not self.call_system(command): raise BackupError('Aborting restoration.') - - # now load the SQL into the database - command = 'gunzip -c %s/*-sql.gz | psql -U %s %s' % (working_dir, db_user, db_name) - if not self.call_system(command): raise BackupError('Aborting restoration.') - - def check_dirs(self): - if not hasattr(settings, 'MEDIA_ROOT'): raise BackupError('The MEDIA_ROOT is not defined') - if not os.path.exists(settings.MEDIA_ROOT): raise BackupError('MEDIA_ROOT "%s" does not exist.' % settings.MEDIA_ROOT) - if not os.path.isdir(settings.MEDIA_ROOT): raise BackupError('MEDIA_ROOT "%s" is not a directory.' % settings.MEDIA_ROOT) - - if not hasattr(settings, 'BACKUP_ROOT'): raise BackupError('You must define BACKUP_ROOT in settings.py') - if not os.path.exists(settings.BACKUP_ROOT): os.makedirs(settings.BACKUP_ROOT) - if not os.path.exists(settings.BACKUP_ROOT): raise BackupError('Backup root "%s" does not exist' % settings.BACKUP_ROOT) - if not os.path.isdir(settings.BACKUP_ROOT): raise BackupError('Backup root "%s" is not a directory' % settings.BACKUP_ROOT) - - def remove_old_files(self, backup_count): - logger.debug("remove_old_files, backup_count=%s" % backup_count) - file_count = 0 - files = os.listdir(settings.BACKUP_ROOT) - files.sort() - files.reverse() - for f in files: - if f == "latest-backup.tar": - logger.debug("%s: %s" % (f, "skipped")) - continue - if f.endswith("-backup.tar"): - if file_count < backup_count: - file_count = file_count + 1 - logger.debug("%s: %s" % (f, file_count)) - else: - logger.warn("Removing old log file: %s" % f) - os.remove(settings.BACKUP_ROOT + f) - - - def make_backup(self): - self.check_dirs() - db_user, db_name, db_password = self.get_db_info() - - now = timezone.localtime(timezone.now()) - file_token = '%d-%02d-%02d_%02d-%02d-%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second) - - sql_file = '%s-sql.gz' % file_token - sql_path = '%s%s' % (settings.BACKUP_ROOT, sql_file) - sql_pass_args = '' - if db_password: os.environ['PGPASSWORD'] = db_password - command = 'pg_dump -U %s %s | gzip > "%s"' % (db_user, db_name, sql_path) - if not self.call_system(command): - logger.info('aborting') - return - - media_file = '%s-media.tgz' % file_token - media_path = '%s%s' % (settings.BACKUP_ROOT, media_file) - dirs = settings.MEDIA_ROOT.split('/') - command = 'cd "%s" && cd .. && tar -czf "%s" "%s"' % (settings.MEDIA_ROOT, media_path, dirs[len(dirs)-1]) - if not self.call_system(command): - logger.info('aborting') - return - - backup_file = '%s-backup.tar' % file_token - backup_path = '%s%s' % (settings.BACKUP_ROOT, backup_file) - logger.info("backup_file: %s" % backup_file) - command = 'cd "%s" && tar -czf "%s" "%s" "%s"' % (settings.BACKUP_ROOT, backup_path, media_file, sql_file) - if not self.call_system(command): - logger.info('aborting') - return - - if not self.call_system('cd "%s" && ln -fs "%s" latest-backup.tar' % (settings.BACKUP_ROOT, backup_file)): - logger.info('Could not link %s to latest-backup.tar' % backup_file) - - command = 'rm -f "%s" "%s"' % (media_path, sql_path) - if not self.call_system(command): - logger.info('Could not erase temp backup files') - - if settings.BACKUP_COUNT and settings.BACKUP_COUNT > 0: - self.remove_old_files(settings.BACKUP_COUNT) - - return os.path.join(settings.BACKUP_ROOT, backup_path) diff --git a/modernomad/management/commands/make_backup.py b/modernomad/management/commands/make_backup.py deleted file mode 100644 index 7715719c..00000000 --- a/modernomad/management/commands/make_backup.py +++ /dev/null @@ -1,32 +0,0 @@ -import os -import time -import urllib -import sys -import datetime -import logging - -from django.core.management.base import BaseCommand, CommandError - -from modernomad.backup import BackupManager - -logger = logging.getLogger(__name__) - - -class Command(BaseCommand): - help = "Creates a backup containing an SQL dump and the media files." - args = "" - requires_system_checks = False - - def handle(self, *labels, **options): - manager = BackupManager() - logger.debug(manager.make_backup()) - -# Copyright 2011 Trevor F. Smith (http://trevor.smith.name/) Licensed under -# the Apache License, Version 2.0 (the "License"); you may not use this -# file except in compliance with the License. You may obtain a copy of -# the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required -# by applicable law or agreed to in writing, software distributed under the -# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -# CONDITIONS OF ANY KIND, either express or implied. See the License -# for the specific language governing permissions and limitations under -# the License. diff --git a/modernomad/management/commands/remove_old_backups.py b/modernomad/management/commands/remove_old_backups.py deleted file mode 100644 index f13af514..00000000 --- a/modernomad/management/commands/remove_old_backups.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -import time -import urllib -import sys -import datetime - -from django.core.management.base import BaseCommand, CommandError -from django.conf import settings - -from modernomad.backup import BackupManager - -class Command(BaseCommand): - help = "Remove Old Backup Files." - args = "[backup_count]" - requires_system_checks = False - - def handle(self, *labels, **options): - backup_count = settings.BACKUP_COUNT - if labels or len(labels) > 1: - backup_count = labels[0] - - manager = BackupManager() - manager.remove_old_files(backup_count) diff --git a/modernomad/management/commands/restore_backup.py b/modernomad/management/commands/restore_backup.py deleted file mode 100644 index 207bf80a..00000000 --- a/modernomad/management/commands/restore_backup.py +++ /dev/null @@ -1,21 +0,0 @@ -import os -import time -import urllib -import datetime -import sys - -from django.core.management.base import BaseCommand, CommandError -from django.conf import settings -from modernomad.backup import BackupManager - -class Command(BaseCommand): - help = "Deletes and then restores the DB and non-static media from a backup created using the make_backup management command." - args = "[backup_file_path]" - requires_system_checks = False - - def handle(self, *labels, **options): - if not labels or len(labels) != 1: raise CommandError('Enter one argument, the path to the backup tar file.') - manager = BackupManager() - manager.restore_backup(labels[0]) - -# Copyright 2010 Trevor F. Smith (http://trevor.smith.name/) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/modernomad/settings/common.py b/modernomad/settings/common.py index 89ab4989..ffe8e5c8 100644 --- a/modernomad/settings/common.py +++ b/modernomad/settings/common.py @@ -13,8 +13,6 @@ environ.Env.read_env(str(BASE_DIR / '.env')) env = environ.Env() -BACKUP_ROOT = BASE_DIR / 'backups' - ADMINS = (( env('ADMIN_NAME', default='Unnamed'), env('ADMIN_EMAIL', default='none@example.com')