-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbbackup.py
50 lines (39 loc) · 1.62 KB
/
dbbackup.py
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
"""
Command for backup database
"""
import os, popen2, time
import settings
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.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']
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.endswith('mysql'):
print 'Doing Mysql backup to database %s into %s' % (self.db, outfile)
self.do_mysql_backup(outfile)
else:
print 'Backup in %s engine not implemented' % self.engine
return '/'.join([os.getcwd(), outfile])
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))