Django Backup Script

This is a backup script for Django projects. It’s able to automate backups of both the database and files to a local folder and a remote FTP server. It is somewhat old and has a few limitations, mainly supporting only MySQL and not supporting the new way of specifying databases introduced in Django 1.2.

It’s loosely based on my WordPress backup script and inspired the database settings auto-detection found in the newer WordPress backup script.

Usage is simple:

$ django_backup /path/to/my/proj
$ django_backup --db-only /path/to/my/proj

The latter command only backs up the database.

The script uses a few configuration variables at the top of the script to set the folder where the local backups are kept and the remote FTP server settings. The database settings are extracted directly from the settings.py of the backed-up project.

#!/bin/bash
 
# (C) 2009 Guy Rutenberg
# Backup Django sites

BACKUP_DIR=
FTP_HOST=
FTP_USER=
FTP_PASS=
FTP_BACKUP_DIR=

# end of user configurable section

PROG=`basename "$0"`
print_usage () {
    echo "USAGE: $0 [options] PROJ_ROOT"
    echo "Backup a Django project located in PROJ_ROOT"
}

print_help ()  {
    print_usage
    cat << EOF
 
Options:
    -h, --help          show this help message and exit
    --db-only           only backup the database
 
EOF
}

TEMP=`getopt -o h --long help,db-only -n "$PROG" -- "$@"`
if (($?)); then
    print_usage
    exit 1
fi

eval set -- "$TEMP"
 
DB_ONLY=0
while true ; do
    case "$1" in
        -h|--help) print_help; exit ;;
        --db-only) DB_ONLY="1" ; shift ;;
        --) shift; break;;
    esac
done



if [ -z "$1" ]
then
 print_usage > /dev/stderr
 exit 1
fi

PROJECT_DIR=$1

# extract database variables from settings.py
cd "$PROJECT_DIR"


DB_ENGINE=`python -c "from settings import *; print DATABASE_ENGINE"`
if [ "$DB_ENGINE" != "mysql" ]; then
 echo $DB_ENGINE
 echo "Only mysql databases are supported!">/dev/stderr
 exit 1
fi

DB_NAME=`python -c "from settings import *; print DATABASE_NAME"`
DB_USER=`python -c "from settings import *; print DATABASE_USER"`
DB_PASS=`python -c "from settings import *; print DATABASE_PASSWORD"`
DB_HOST=`python -c "from settings import *; print DATABASE_HOST"`
#TODO find how to use it
DB_PORT=`python -c "from settings import *; print DATABASE_PORT"`

# set optional parameters: host, port
HOST_ARGS=''
if [ -n "$DB_HOST" ]; then
 HOST_ARGS="--host $DB_HOST"
fi

PORT_ARGS=''
if [ -n "$DB_PORT" ]; then
 PORT_ARGS="--port $DB_HOST"
fi
 
SITE_DIR=`dirname "$PROJECT_DIR"`/`basename "$PROJECT_DIR"`
BACKUP_DIR=`dirname "$BACKUP_DIR"`/`basename "$BACKUP_DIR"`

echo -n "dumping database... "
DUMP_NAME=${DB_NAME}-$(date +%Y%m%d).sql.bz2
mysqldump --user=${DB_USER} --password=${DB_PASS} $HOST_ARGS 
 $PORT_ARGS --databases ${DB_NAME} 
 | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}
if (($?)); then
    echo "failed!"
    exit 1
fi
echo "done"
 
PUT_TARBALL_FTP=""
if [ "$DB_ONLY" -eq "0" ]; then
    echo -n "Creating tarball... "
    TAR_NAME=${SITE_DIR##*/}-$(date +%Y%m%d).tar.bz2
    tar -cjf ${BACKUP_DIR}/${SITE_DIR##*/}-$(date +%Y%m%d).tar.bz2 ${SITE_DIR}
    if (($?)); then
        echo "failed!"
        exit 2
    fi
    echo "done"
    PUT_TARBALL_FTP="put "${BACKUP_DIR}/${TAR_NAME}""
fi
 
echo -n "Uploading backup to FTP... "

lftp -u ${FTP_USER},${FTP_PASS} ${FTP_HOST} <<EOF
cd "${FTP_BACKUP_DIR}"
put "${BACKUP_DIR}/${DUMP_NAME}"
${PUT_TARBALL_FTP}

EOF
if (($?)); then
    echo "failed!"
    exit 3
fi
echo "done"

Leave a Reply

Your email address will not be published. Required fields are marked *