Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Sqlserver support (pyodbc) #11

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions dbdump/management/commands/dbdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings

# Lets you inform alternative locations
MYSQL_DUMP_TOOL = getattr(settings, "MYSQL_DUMP_TOOL", "mysqldump")
POSTGRE_DUMP_TOOL = getattr(settings, "POSTGRE_DUMP_TOOL", "pg_dump")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this POSTGRES_DUMP_TOOL as nobody really abbreviates postgres as "postgre"



class Command(BaseCommand):
help = 'Dump database into a file. Only MySQL and PostgreSQL engines are supported.'
Expand Down Expand Up @@ -131,6 +135,8 @@ def handle(self, *args, **options):
self.do_mysql_backup(outfile, raw_args=raw_args)
elif 'postgresql' in self.engine:
self.do_postgresql_backup(outfile, raw_args=raw_args)
elif 'sql_server' in self.engine:
self.do_sql_server_backup(outfile, raw_args=raw_args)
else:
raise CommandError('Backups of %s engine are not implemented.' % self.engine)

Expand Down Expand Up @@ -168,7 +174,7 @@ def do_mysql_backup(self, outfile, raw_args=''):
excluded_args += ['--ignore-table=%s.%s' % (self.db, excluded_table)
for excluded_table in self.excluded_tables + self.empty_tables]

command = 'mysqldump %s' % (' '.join(excluded_args + [self.db]))
command = MYSQL_DUMP_TOOL + ' %s' % (' '.join(excluded_args + [self.db]))

if outfile != self.output_stdout:
command += " > %s" % outfile
Expand All @@ -179,7 +185,7 @@ def do_mysql_backup(self, outfile, raw_args=''):
no_data_args = main_args[:] + ['--no-data', self.db]
no_data_args += [empty_table for empty_table in self.empty_tables]

command = 'mysqldump %s' % (' '.join(no_data_args))
command = MYSQL_DUMP_TOOL + ' %s' % (' '.join(no_data_args))

if outfile != self.output_stdout:
command += " >> %s" % outfile
Expand Down Expand Up @@ -219,7 +225,7 @@ def do_postgresql_backup(self, outfile, raw_args=''):
excluded_args += ['--exclude-table=%s' % excluded_table
for excluded_table in self.excluded_tables + self.empty_tables]

command = 'pg_dump %s %s' % (' '.join(excluded_args), self.db)
command = POSTGRE_DUMP_TOOL + ' %s %s' % (' '.join(excluded_args), self.db)

if outfile != self.output_stdout:
command += ' > %s' % outfile
Expand All @@ -231,7 +237,7 @@ def do_postgresql_backup(self, outfile, raw_args=''):
no_data_args += ['--table=%s' % empty_table for empty_table in self.empty_tables]
no_data_args += [self.db]

command = 'pg_dump %s %s' % (' '.join(no_data_args), self.db)
command = POSTGRE_DUMP_TOOL + ' %s %s' % (' '.join(no_data_args), self.db)

if outfile != self.output_stdout:
command += ' >> %s' % outfile
Expand All @@ -256,3 +262,40 @@ def run_postgresql_command(self, command, outfile):
if self.password:
process.stdin.write('%s\n' % self.password)
process.stdin.close()

def do_sql_server_backup(self, outfile, **kwargs):
"""Backup of an entire SQL server database (Requires pyodbc)"""
import pyodbc

# Connection string to the database.
conn = r'DSN={0.db};SERVER={0.host};UID={0.user};PWD={0.password}'.format(self)

connection = pyodbc.connect(conn,
driver='{SQL Server Native Client 11.0}',
trusted_connection='yes',
autocommit=True
)
# The path must be absolute
outfile = os.path.abspath(outfile)
filename = os.path.basename(outfile)
filename, ext = os.path.splitext(filename)

# The export only concatenates in the same file.
if os.path.exists(outfile):
filename += ("_" + time.strftime('%Y%m%d-%H%M%S'))

# The sql extension does not make sense here.
ext = '.bak' if ext == '.sql' else ext

filepath = os.path.join(os.path.dirname(outfile), filename + ext)

if self.debug:
print("exporting to '{}'".format(filepath))

query = "BACKUP DATABASE [{0.db}] TO DISK = '{1:s}';".format(self, filepath)

with connection.cursor() as cursor:
cursor.execute(query)

while cursor.nextset():
time.sleep(0.1)
62 changes: 33 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# coding=utf-8

import os

try:
from setuptools import setup, find_packages
from setuptools import setup, find_packages
except ImportError:
import ez_setup
ez_setup.use_setuptools()
from setuptools import setup, find_packages
import ez_setup

ez_setup.use_setuptools()
from setuptools import setup, find_packages


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
return open(os.path.join(os.path.dirname(__file__), fname)).read()


setup(
name = 'django-dbdump',
version = '1.2',
url = 'https://github.com/vitaliyf/django-dbdump/',
download_url = 'https://github.com/vitaliyf/django-dbdump/',
license = 'BSD',
description = 'Database backup management command.',
long_description=read('README.rst'),
author = 'Vitaliy Fuks',
author_email = '[email protected]',
packages = find_packages(),
include_package_data = True,
platforms='any',
classifiers = [
'Framework :: Django',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
install_requires=[
'Django>=1.8',
],
name='django-dbdump',
version='1.3.0',
url='https://github.com/vitaliyf/django-dbdump/',
download_url='https://github.com/vitaliyf/django-dbdump/',
license='BSD',
description='Database backup management command.',
long_description=read('README.rst'),
author='Vitaliy Fuks',
author_email='[email protected]',
packages=find_packages(),
include_package_data=True,
platforms='any',
classifiers=[
'Framework :: Django',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
install_requires=[
'Django>=1.8',
'pyodbc'
],
)