Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Last active March 18, 2022 19:17
Show Gist options
  • Select an option

  • Save CypherpunkSamurai/2c59bb6bf15dea4a3861c8c099f3796d to your computer and use it in GitHub Desktop.

Select an option

Save CypherpunkSamurai/2c59bb6bf15dea4a3861c8c099f3796d to your computer and use it in GitHub Desktop.
Dump Database from Django App

Dump Database from Django App

Here's the two ways i found to dump data from Django application.

Using Django-DBbackup

As the django-dbbackup documentation states, we need to install the module django-dbbackup (django-dbbackup==3.3.0 latest at the time). Then we need to add these to the settings.py:

DBBACKUP_STORAGE = "django.core.files.storage.FileSystemStorage"
DBBACKUP_STORAGE_OPTIONS = {"location": "/var/log/greedybear/"}

and this to INSTALLED_APPS:

INSTALLED_APPS = {
  ...
  # db backups
  "dbbackup",

Then from django call

from django.core import management

def backup_db():
  management.call_command("dbbackup")

Using dumpdata

According to this stackoverflow and this article we can run:

dumpdata > db.json

to dump the data to json. Now we need to run it from code.

import os
from cStringIO import StringIO

from django.core import management

def create_fixture(app_name, filename):
    buf = StringIO()
    management.call_command('dumpdata', app_name, stdout=buf)
    buf.seek(0)
    with open(filename, 'w', 'utf-8') as f:
        f.write(buf.read())

Solved (?)

Hope it Helped, if you found another way or if not working comment below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment