forked from duointeractive/django-fabtastic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Greg Taylor
committed
Sep 1, 2010
0 parents
commit c7ee1fd
Showing
9 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.pyc | ||
*.swp | ||
.project | ||
.pydevproject | ||
MANIFEST | ||
build | ||
dist |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.conf import settings | ||
from fabtastic.db import util | ||
|
||
db_engine = util.get_db_setting('ENGINE') | ||
|
||
if db_engine in ['postgresql_psycopg2', 'postgresql']: | ||
from fabtastic.db.postgres import * | ||
else: | ||
raise NotImplementedError("Fabtastic: DB engine '%s' is not supported" % db_engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import stat | ||
|
||
def set_pgpass(db_name, db_user, db_pass, db_host="*", db_port="*"): | ||
""" | ||
Sets the ~/.pgpass file up so that psql and pg_dump doesn't ask | ||
for a password. | ||
""" | ||
pgpass_file = os.path.expanduser('~') | ||
pgpass_file = os.path.join(pgpass_file, '.pgpass') | ||
|
||
fd = open(pgpass_file, 'wb') | ||
fd.write('*:*:%s:%s:%s' % (db_name, db_user, db_pass)) | ||
fd.close() | ||
|
||
perms = [stat.S_IRUSR, stat.S_IWUSR] | ||
os.chmod(pgpass_file, perms) | ||
|
||
def backup_to_tmp(): | ||
""" | ||
Backs up the current ligonier DB to a .gz file, stashed in /tmp/. | ||
Returns a tuple in the format of (filename, full_file_path). For example: | ||
(ligonier-<dtime>.sql.gz, /tmp/ligonier-<dtime.sql.gz) | ||
You will probably want to run _remove_tmp_backup(db_filepath) after doing | ||
whatever you need to do with the file. | ||
""" | ||
print("Backing up database '%s'. This may take a minute or so..." % db_name) | ||
|
||
today = datetime.today() | ||
db_filename = "%s-%s.sql.gz" % (db_name, today.strftime("%Y%m%d-%H%M")); | ||
db_filepath = "/tmp/%s" % db_filename | ||
|
||
# Set up a ~/.pgpass to avoid password prompts when running pg_dump. | ||
_set_pgpass() | ||
run("pg_dump -i -h %s -U %s %s | gzip > %s" % (db_host, db_user, db_name, | ||
db_filepath)) | ||
|
||
return (db_filename, db_filepath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Various database-related utility functions. | ||
""" | ||
from django.conf import settings | ||
|
||
def get_db_setting(db_setting, db_alias='default'): | ||
""" | ||
Gets a database setting from settings.py. Extra logic to support | ||
Django 1.1 and Django 1.2. | ||
""" | ||
if hasattr(settings, 'DATABASE'): | ||
return settings.DATABASE[db_alias]['ENGINE'] | ||
else: | ||
return settings.DATABASE_ENGINE |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
from datetime import datetime | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.contrib.contenttypes.models import ContentType | ||
from fabtastic import db | ||
|
||
class Command(BaseCommand): | ||
args = '[<output_file_path>]' | ||
help = 'Dumps a SQL backup of your entire DB. Defaults to CWD.' | ||
|
||
def _set_db_values(self): | ||
self.db_name = db.util.get_db_setting('NAME') | ||
self.db_user = db.util.get_db_setting('USER') | ||
self.db_password = db.util.get_db_setting('PASSWORD') | ||
|
||
def _set_dump_path(self): | ||
if len(self.args) > 0: | ||
self.dump_path = self.args[0] | ||
else: | ||
today = datetime.today() | ||
db_filename = "%s-%s.sql" % (self.db_name, today.strftime("%Y%m%d-%H%M")); | ||
self.dump_path = os.path.join(os.getcwd(), db_filename) | ||
|
||
def handle(self, *args, **options): | ||
self.args = args | ||
self.options = options | ||
|
||
|
||
db.set_pgpass(self.db_name, self.db_user, self.db_password, | ||
db_host="*", db_port="*") | ||
self._set_dump_path() |