Backup your django database using fabric

Reactions: 0 pingback - 3 comments - 3 votes

The use of a simple python script called fabfile.py, in order to manage yours django applications reveals how straightforward and simple is the deploy phase by using fabric tool.

I use fabric about everyday, to manage various repetitive and tedious tasks regarding the maintenance of this blog. Currently I have a local git repository where all the source code of the blog is stored and a fabfile.py that helps me to deploy it correctly and in automatized way to my VPS server.

In this post I want to show you how simple is to create a simple command to backup your SQL database. In this example I'm using mysqldump command to dump the database contents to a compressed gzip file. Obviously you could edit the following script to include pg_dump if you're using postgresql database or whatever.

import time

def vps():
    env.hosts = ['apps.myvps.com']
    env.user = 'app'
    env.dbname = 'blog'
    env.dbuser = 'blogger'
    env.dbpass = 'password'

def backup():
    require('hosts', provided_by=[vps])
    require('dbname')
    require('dbuser')
    require('dbpass')

    date = time.strftime('%Y%m%d%H%M%S')
    fname = '/tmp/%(database)s-backup-%(date)s.xml.gz' % {
        'database': env.dbname,
        'date': date,
    }

    if exists(fname):
        run('rm "%s"' % fname)

    run('mysqldump -u %(username)s -p%(password)s %(database)s --xml | '
        'gzip > %(fname)s' % {'username': env.dbuser,
                              'password': env.dbpass,
                              'database': env.dbname,
                              'fname': fname})

    get(fname, os.path.basename(fname))
    run('rm "%s"' % fname)

As you can see everything is pretty clear. We use the vps() function to define variables needed to do a correct backup, such as the name of the database, the username and password to access to the SQL server and obviously the credentials to access through SSH to the VPS. The backup() method is really simple. It requests various variables it needs to achieve his job by using the require function, and then it proceeds with the creation of the xml-gzipped database dump. Finally the file located in /tmp will be transfered to our local machine thanks to the use of get fabric API function.

Execute

Now let's save the script with the name fabfile.py and launch it:

$ ls
fabfile.py
$ fab vps backup
[apps.myvps.com] run: mysqldump -u blogger -ppassword blog --xml | gzip > /tmp/blog-backup-20100815125758.xml.gz
[apps.myvps.com] download: blog-backup-20100815125758.xml.gz <- /tmp/blog-20100815125758.xml.gz
[apps.myvps.com] run: rm "/tmp/blog-backup-20100815125758.xml.gz"

Done.
Disconnecting from apps.myvps.com... done.
$ ls *.gz
blog-backup-20100815125758.xml.gz

That's all! Simple, clear and pythonic :)

Tags: Backup Database Django Fabric Python on Aug. 9, 2010, 2 p.m.
Garth Humphreys Feb. 19, 2011, 8:43 p.m. #
Thanks for sharing this info, it helped me figure out my own setup!
Josir March 29, 2013, 4:03 p.m. #
Just a small correction: imports must be added:

import os,time
from fabric.contrib.files import exists
angry hearts Sept. 9, 2013, 11:01 p.m. #
Awesome ! ! !

Copyright © 2010 nopper.archpwn.org

This is the official my personal blog and official one of the ArchPwn project.

ArchPwn is released under the terms of GPL license. All rights reserved.

CSS/HTML valid.