Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: None  |  size: 3.17 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Index: backupdb.py
  2. ===================================================================
  3. --- backupdb.py (revision 465)
  4. +++ backupdb.py (working copy)
  5. @@ -1,9 +1,12 @@
  6.  """
  7.   Command for backup database
  8.  """
  9. +import bz2
  10.  
  11.  import os, time
  12.  
  13. +from optparse import make_option
  14. +
  15.  import pexpect
  16.  
  17.  from django.core.management.base import BaseCommand
  18. @@ -11,6 +14,18 @@
  19.  class Command(BaseCommand):
  20.      help = "Backup database. Only Mysql and Postgresql engines are implemented"
  21.  
  22. +    option_list = BaseCommand.option_list + (
  23. +        make_option('-c', '--compress', action='store_true', dest='compress_backup',
  24. +                    help='Makes a compressed backup of the current database.'),
  25. +        )
  26. +
  27. +    def bz2_backup(self, data, outfile):
  28. +        file = bz2.BZ2File(outfile, "wb")
  29. +        try:
  30. +            file.write(data)
  31. +        finally:
  32. +           file.close()
  33. +    
  34.      def handle(self, *args, **options):
  35.          from django.conf import settings
  36.  
  37. @@ -25,17 +40,21 @@
  38.          if not os.path.exists(backup_dir):
  39.              os.makedirs(backup_dir)
  40.          outfile = os.path.join(backup_dir, 'backup_%s.sql' % time.strftime('%y%m%d%S'))
  41. +        
  42. +        compress_backup = options.get('compress_backup', False)
  43. +        if compress_backup is not None:
  44. +            outfile += '.bz2'
  45.  
  46.          if self.engine == 'mysql':
  47.              print 'Doing Mysql backup to database %s into %s' % (self.db, outfile)
  48. -            self.do_mysql_backup(outfile)
  49. +            self.do_mysql_backup(outfile, compress_backup)
  50.          elif self.engine in ('postgresql_psycopg2', 'postgresql'):
  51.              print 'Doing Postgresql backup to database %s into %s' % (self.db, outfile)
  52. -            self.do_postgresql_backup(outfile)
  53. +            self.do_postgresql_backup(outfile, compress_backup)
  54.          else:
  55.              print 'Backup in %s engine not implemented' % self.engine
  56.  
  57. -    def do_mysql_backup(self, outfile):
  58. +    def do_mysql_backup(self, outfile, compress_backup=False):
  59.          args = []
  60.          if self.user:
  61.              args += ["--user=%s" % self.user]
  62. @@ -47,9 +66,14 @@
  63.              args += ["--port=%s" % self.port]
  64.          args += [self.db]
  65.  
  66. -        os.system('mysqldump %s > %s' % (' '.join(args), outfile))
  67. +        #os.system('mysqldump %s > %s' % (' '.join(args), outfile))
  68. +        sqldata = os.popen('mysqldump %s' % (' '.join(args)))
  69. +        if compress_backup is not None:
  70. +            self.bz2_backup(sqldata, outfile)
  71. +        else:
  72. +            file(outfile, 'w').write(sqldata)
  73.  
  74. -    def do_postgresql_backup(self, outfile):
  75. +    def do_postgresql_backup(self, outfile, compress_backup=False):
  76.          args = ['pg_dump']
  77.          if self.user:
  78.              args += ["--username=%s" % self.user]
  79. @@ -69,7 +93,10 @@
  80.              child.sendline(self.passwd)
  81.  
  82.              child.expect(pexpect.EOF)
  83. -            file(outfile, "w").write(child.before)
  84. +            if compress_backup is not None:
  85. +                self.bz2_backup(child.before, outfile)
  86. +            else:
  87. +                file(outfile, "w").write(child.before)
  88.              child.close()
  89.          else:
  90.              os.system('%s > %s' % (cmd, outfile))