Export Database and Media_Root via Admin Interface

 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
import os
from datetime import date
from django.conf import settings
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def export_database(request):
    if not request.method == 'POST':
        return render_to_response('admin/export.html', {'what': 'Database',})
    else:
        #output backup
        cmd = settings.MYSQLDUMP_BIN+' --opt --compact --skip-add-locks -u %s -p%s %s | bzip2 -c' % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
        stdin, stdout = os.popen2(cmd)
        stdin.close()
        response = HttpResponse(stdout, mimetype="application/octet-stream")
        response['Content-Disposition'] = 'attachment; filename=%s' % date.today().__str__()+'_db.sql.bz2'
        return response


@staff_member_required        
def export_media(request):
    if not request.method == 'POST':
        return render_to_response('admin/export.html', {'what': 'Media Root',})
    else:
        #output media
        stdin, stdout = os.popen2('tar -cf - %s' % settings.MEDIA_ROOT)
        stdin.close()
        #print "created process, closed stdin"
        response = HttpResponse(stdout, mimetype="application/octet-stream")
        response['Content-Disposition'] = 'attachment; filename=%s' % date.today().__str__()+'_media.tar'
        return response

More like this

  1. Generic admin action to export data into excel/csv by vinay.chittora 8 months, 2 weeks ago
  2. Export Models by brunobord 5 years, 9 months ago
  3. XML tabledump to model by fivethreeo 7 years, 1 month ago
  4. Quickly create an admin interface for an app. by bartTC 3 years, 7 months ago
  5. More generic CSV export admin action factory by Rmatt 9 months, 3 weeks ago

Comments

jezdez (on February 3, 2008):

Don't forget to set MYSQLDUMP_BIN in your settings file to the path of your mysqldump binary before using this, e.g. "/usr/bin/mysqldump".

#

andybak (on April 16, 2008):

I am having a few problems with this.

One initial small thing - you may need to include -h and then your database hostname if your database isn't localhost from Django.

I've got the following:

if settings.DATABASE_HOST=='':
    host = 'localhost'
else:
    host = settings.DATABASE_HOST    
cmd = settings.MYSQLDUMP_BIN+' --opt --compact --skip-add-locks -h %s -u %s -p%s %s | /usr/bin/bzip2 -c' % (host, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)

Anyway - that didn't quite get me working - Basically it doesn't like streaming from stdout. I add to add the line: stdout = stdout.readlines()

and remove the pipe to bzip to get any output. This is on the dev server and on Django over FCGI

#

andybak (on August 4, 2008):

Quick update - the above problems with streaming might be middleware related.

#

(Forgotten your password?)