forked from django-extensions/django-extensions
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
1 changed file
with
56 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,57 @@ | ||
__author__ = 'erik' | ||
|
||
|
||
""" | ||
Command for restoring a database | ||
""" | ||
|
||
import os, time | ||
from django.core.management.base import BaseCommand | ||
|
||
class Command(BaseCommand): | ||
help = "Backup database. Only Mysql and Postgresql engines are implemented" | ||
|
||
def handle(self, *args, **options): | ||
from django.db import connection | ||
from ... import settings | ||
|
||
if not os.path.isdir(settings.BACKUP_LOCATION): | ||
os.makedirs(settings.BACKUP_LOCATION) | ||
infile = os.path.join(settings.BACKUP_LOCATION, "%s.sql" %(settings.BACKUP_BASENAME)) | ||
|
||
if 'mysql' in settings.DB_ENGINE: | ||
print 'Doing Mysql restore of database %s from %s' % (settings.DB_NAME, infile) | ||
self.do_mysql_restore(infile) | ||
elif 'postgres' in settings.DB_ENGINE: | ||
print 'Doing Postgresql restore of database %s from %s' % (settings.DB_NAME, infile) | ||
self.do_postgresql_restore(infile) | ||
else: | ||
print 'Backup in %s engine not implemented' % settings.DB_ENGINE | ||
|
||
def do_mysql_restore(self, infile): | ||
from ... import settings | ||
args = [] | ||
if settings.DB_USER: | ||
args += ["--user=%s" % settings.DB_USER] | ||
if settings.DB_PASSWD: | ||
args += ["--password=%s" % settings.DB_PASSWD] | ||
if settings.DB_HOST: | ||
args += ["--host=%s" % settings.DB_HOST] | ||
if settings.DB_PORT: | ||
args += ["--port=%s" % settings.DB_PORT] | ||
args += [settings.DB_NAME] | ||
|
||
os.system('mysql %s < %s' % (' '.join(args), infile)) | ||
|
||
def do_postgresql_restore(self, infile): | ||
from ... import settings | ||
args = [] | ||
if settings.DB_USER: | ||
args += ["--username=%s" % settings.DB_USER] | ||
if settings.DB_HOST: | ||
args += ["--host=%s" % settings.DB_HOST] | ||
if settings.DB_PORT: | ||
args += ["--port=%s" % settings.DB_PORT] | ||
if settings.DB_NAME: | ||
args += [settings.DB_NAME] | ||
os.system('PGPASSWORD=%s psql -c "drop schema public cascade; create schema public;" %s' % (settings.DB_PASSWD, ' '.join(args))) | ||
os.system('PGPASSWORD=%s psql %s < %s' % (settings.DB_PASSWD, ' '.join(args), infile)) |