Login

backupdb command

Author:
msaelices
Posted:
June 24, 2008
Language:
Python
Version:
.96
Tags:
management command django-admin
Score:
5 (after 5 ratings)

backupdb command allows to make a database backup automatically. It's supposed to do this just before a syncdb, reset or flush command in a server deployment. A usual upgrade task in production server could be:

./manage.py backupdb

./manage.py reset myapp

./manage.py syncdb

Put this code in your project's management/commands/backupdb.py file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
 Command for backup database
"""

import os, popen2, 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 django.conf import settings

        self.engine = settings.DATABASE_ENGINE
        self.db = settings.DATABASE_NAME
        self.user = settings.DATABASE_USER
        self.passwd = settings.DATABASE_PASSWORD
        self.host = settings.DATABASE_HOST
        self.port = settings.DATABASE_PORT

        backup_dir = 'backups'
        if not os.path.exists(backup_dir):
            os.makedirs(backup_dir)
        outfile = os.path.join(backup_dir, 'backup_%s.sql' % time.strftime('%y%m%d%S'))

        if self.engine == 'mysql':
            print 'Doing Mysql backup to database %s into %s' % (self.db, outfile)
            self.do_mysql_backup(outfile)
        elif self.engine in ('postgresql_psycopg2', 'postgresql'):
            print 'Doing Postgresql backup to database %s into %s' % (self.db, outfile)
            self.do_postgresql_backup(outfile)
        else:
            print 'Backup in %s engine not implemented' % self.engine

    def do_mysql_backup(self, outfile):
        args = []
        if self.user:
            args += ["--user=%s" % self.user]
        if self.passwd:
            args += ["--password=%s" % self.passwd]
        if self.host:
            args += ["--host=%s" % self.host]
        if self.port:
            args += ["--port=%s" % self.port]
        args += [self.db]

        os.system('mysqldump %s > %s' % (' '.join(args), outfile))

    def do_postgresql_backup(self, outfile):
        args = []
        if self.user:
            args += ["--username=%s" % self.user]
        if self.passwd:
            args += ["--password"]
        if self.host:
            args += ["--host=%s" % self.host]
        if self.port:
            args += ["--port=%s" % self.port]
        if self.db:
            args += [self.db]
        pipe = popen2.Popen4('pg_dump %s > %s' % (' '.join(args), outfile))
        if self.passwd:
            pipe.tochild.write('%s\n' % self.passwd)
            pipe.tochild.close()

More like this

  1. freshdb management command by tomzee 7 years, 5 months ago
  2. Command to dump data as a python script by willhardy 6 years, 11 months ago
  3. Management command which helps to find temlate files by skyjur 4 years, 4 months ago
  4. extras.py for management commands by dnordberg 7 years, 9 months ago
  5. collectmedia command: Copy or link media files from installed apps by exogen 6 years, 8 months ago

Comments

furins (on September 23, 2013):

Please note that in recent versions of Django (1.2+) you have to replace

    self.engine = settings.DATABASE_ENGINE
    self.db = settings.DATABASE_NAME
    self.user = settings.DATABASE_USER
    self.passwd = settings.DATABASE_PASSWORD
    self.host = settings.DATABASE_HOST
    self.port = settings.DATABASE_PORT

with:

    self.engine = settings.DATABASES['default']['ENGINE']
    self.db = settings.DATABASES['default']['NAME']
    self.user = settings.DATABASES['default']['USER']
    self.passwd = settings.DATABASES['default']['PASSWORD']
    self.host = settings.DATABASES['default']['HOST']
    self.port = settings.DATABASES['default']['PORT']

more info here: Django docs

#

Please login first before commenting.