Skip to content
Browse files

Starting on mysql utility command.

  • Loading branch information...
1 parent 1d2af9e commit 034917fbfd4806bf729402d33c840fc95d550126 @dpetzold committed Feb 22, 2012
Showing with 101 additions and 0 deletions.
  1. +101 −0 django_extensions/management/commands/mysql.py
View
101 django_extensions/management/commands/mysql.py
@@ -0,0 +1,101 @@
+from __future__ import print_function
+
+from optparse import make_option
+
+import datetime
+import socket
+import sys
+import os
+
+import django
+from django.core.management.base import CommandError, BaseCommand
+from django.conf import settings
+
+class MysqlCmd(object):
+
+ def __init__(self, router):
+
+ self.db = settings.DATABASES[router]
+
+ cmd = '%s '
+ if self.db['HOST'] != '':
+ cmd += '-h %s' % (self.db['HOST'])
+ cmd += '-p %s' % (self.db['PORT'])
+ cmd += '-u {user} --password={password}'.format(
+ user=self.db['USER'],
+ password=self.db['PASSWORD'])
+ self.mysqlbase = cmd
+
+ @property
+ def mysql(self):
+ return self.mysqlbase % ('mysql')
+
+ @property
+ def mysqladmin(self):
+ return self.mysqlbase % ('mysqladmin')
+
+ @property
+ def mysqldump(self):
+ return self.mysqlbase % ('mysqldump')
+
+ def drop(self):
+ os.system('{mysqladmin} -f DROP {database}'.format(
+ mysqladmin=self.mysqladmin,
+ database=self.db['NAME']))
+
+ def create(self):
+ os.sytem('{mysqladmin} CREATE {database}'.format(
+ mysqladmin=self.mysqladmin,
+ database=self.db['NAME']))
+
+ def source(self):
+ os.system('{mysql} {database} < {db_dump}'.format(
+ mysql=self.mysql,
+ database=self['NAME'],
+ db_dump=db_dump))
+
+ def dump(self):
+ backup_dir = '{site_root}/../backup'.format(
+ site_root=settings.SITE_ROOT)
+
+ if not os.path.exists(backup_dir):
+ os.mkdir(backup_dir)
+
+ db_file = '{site_root}/../backup/{database}.{date}-{hostname}.gz'.format(
+ site_root=settings.SITE_ROOT,
+ database=self.db['NAME'],
+ date=datetime.datetime.now().strftime('%Y%m%d_%H%M%S'),
+ hostname=socket.gethostname())
+
+ output = os.system('{mysqldump} {database} | gzip > {db_file}'.format(
+ mysqldump=self.mysqldump,
+ db_file=db_file,
+ database=settings.DATABASES['default']['NAME']))
+
+
+class Command(BaseCommand):
+
+ option_list = BaseCommand.option_list + (
+ make_option('-D', '--dump', action='store',
+ dest='dump', default=False,
+ help='Dump the database to the backup dir.'),
+ make_option('-R', '--router', action='store',
+ dest='router', default='default',
+ help='Use this router-database other then defined in settings.py'),
+ make_option('-U', '--upload', action='store',
+ dest='upload', default=False,
+ help='Dump and upload the file.'),
+ )
+ help = """Backups the database and the site directory."""
+
+ requires_model_validation = False
+ can_import_settings = True
+
+ def handle(self, *args, **options):
+
+ mysql = MysqlCmd(options.get('router'))
+ print(mysql.mysql)
+ print(mysql.mysqladmin)
+ print(mysql.mysqldump)
+
+ mysql.dump()

0 comments on commit 034917f

Please sign in to comment.
Something went wrong with that request. Please try again.