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