Skip to content

Instantly share code, notes, and snippets.

@kipanshi
Created May 7, 2012 19:07
Show Gist options
  • Save kipanshi/2629750 to your computer and use it in GitHub Desktop.
Save kipanshi/2629750 to your computer and use it in GitHub Desktop.
Django autoloading Postgres sql dump
#!/bin/sh
# This script needs to be run from inside the project folder
# since it needs access to settings.py
#
# DISCLAMER: Use _only_ on local dev installation!
DB_NAME=`python -c "import settings; print settings.DATABASES['default']['NAME']"`
DB_USER=`python -c "import settings; print settings.DATABASES['default']['USER']"`
DB_PASS=`python -c "import settings; print settings.DATABASES['default']['PASSWORD\
']"`
DB_TYPE=`python -c "import settings; print settings.DATABASES['default']['ENGINE']\
"`
LATEST_BACKUP=`ls -t backups/ | head -1`
UNZIP_CMD=`which gunzip`
BACKUP_DIR=`pwd`'/backups'
cd $BACKUP_DIR
if [ $DB_TYPE = "django.db.backends.mysql" ]; then
# For mysql one can make the same steps as with Posgresql
echo "Mysql backend is not supported anymore"
else
export PGPASSWORD=$DB_PASS
# Drop database if exists
echo 'Dropping '$DB_NAME'...'
dropdb --username=$DB_USER -w $DB_NAME
echo 'OK'
# Create database
echo 'Creating '$DB_NAME'...'
createdb --username=$DB_USER -w -E utf8 -O $DB_USER $DB_NAME
echo 'OK'
# Load dump
echo 'Loading dump '$LATEST_BACKUP'...'
$UNZIP_CMD < $LATEST_BACKUP | psql -1 -q --username $DB_USER $DB_NAME
echo 'OK'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment