Skip to content

Instantly share code, notes, and snippets.

@Suor
Created March 10, 2014 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suor/9460464 to your computer and use it in GitHub Desktop.
Save Suor/9460464 to your computer and use it in GitHub Desktop.
Pull DB to development environment script
#!/bin/bash
BACKUP_SERVER=kronos.metadesign.ru
BACKUP_DIR=/backup/zeus/all/
RUAUTO_BACKUP_PATH=/db/dumps/pgsql/ruauto.sql.gz
DUMP_TMP=tmp/db.sql
DUMP_TMP_GZ="$DUMP_TMP.gz"
# Text color variables
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtpur=$(tput setaf 5) # Purple
txtcyn=$(tput setaf 6) # Cyan
txtwht=$(tput setaf 7) # White
txtrst=$(tput sgr0) # Text reset
bldred=${txtbld}$(tput setaf 1) # red
progress()
{
local width=50
local max=$1 curr=$2 pos=$width perc=100
if [ $curr -lt $max ]; then
pos=$((curr*width/max))
perc=$((curr*100/max))
fi
local str="\r[$(printf "%${pos}s" \
| sed 's/ /#/g')$(printf "%$((width-$pos))s" \
| sed 's/ /-/g')]"
[ -z "$PB_NOPERC" ] && str="$str $perc%"
[ -z "$PB_NOITEM" ] && str="$str ($curr/$max $3)"
echo -ne $str
[ $perc -eq 100 ] && echo
}
# Platform independent get_size
if [ `uname` = Linux ]; then
get_size() {
stat -c%s $1
}
else
get_size() {
stat -f %z $1
}
fi
# Check environment
eval `echo "
from project import settings;
db = settings.DATABASES['default']
print 'LOCAL=%s' % settings.LOCAL
print 'DEBUG=%s' % settings.DEBUG
print 'HOME_DIR=%s' % settings.HOME_DIR
print 'DB_HOST=%s' % (db['HOST'] or 'localhost')
print 'DB_PORT=%s' % (db['PORT'] or '5432')
print 'DB_NAME=%s' % db['NAME']
print 'DB_USER=%s' % db['USER']
print 'DB_PASSWORD=%s' % db['PASSWORD']
" | python`
if [ "$LOCAL" != True ]; then
echo "${bldred}Looks like this copy is not LOCAL. Stop.${txtrst}"
exit 1
fi
if [ "$DEBUG" != True ]; then
echo "${bldred}Looks like this copy is not DEBUG. Stop.${txtrst}"
exit 1
fi
echo "${txtbld}${txtpur}Going to drop and recreate \"$DB_NAME\" on $DB_HOST:$DB_PORT${txtrst}"
### Download file
if [ "$1" != "local" ]; then
echo "Downloading database dump..."
# Remove old files
rm -f $DUMP_TMP 2>/dev/null
rm -f $DUMP_TMP_GZ 2>/dev/null
# Get filename
DATE=`ssh $BACKUP_SERVER "sudo ls $BACKUP_DIR | sort -r | head -1"`
FILENAME=$BACKUP_DIR$DATE$RUAUTO_BACKUP_PATH
# Get file size
SIZE=`ssh $BACKUP_SERVER "sudo stat -f %z $FILENAME"`
SIZE_MB=$(( $SIZE / 1024 / 1024 ))
# Start downloading, stop if this process exit
trap 'kill $(jobs -p) 2>/dev/null' EXIT
ssh $BACKUP_SERVER "sudo cat $FILENAME" > $DUMP_TMP_GZ &
# Update progress bar
while true; do
if [ -e $DUMP_TMP_GZ ]; then
DONE=$(get_size $DUMP_TMP_GZ)
else
DONE=0
fi
DONE_MB=$(( $DONE / 1024 / 1024 ))
progress $SIZE_MB $DONE_MB "MB"
kill -0 $! 2&>/dev/null
DOWNLOAD_STOPPED=$?
if [ "$DOWNLOAD_STOPPED" = 1 ]; then
break
fi
sleep 1
done
echo
# Check file size
DONE=$(get_size $DUMP_TMP_GZ)
if [ $DONE != $SIZE ]; then
echo "${bldred}Download failed.${txtrst}";
exit 1;
fi
echo "Unpacking dump..."
gzip -fd $DUMP_TMP_GZ
echo "Patching dump..."
perl -i -n - $DUMP_TMP <<PERL
s/(DATABASE|connect) ruauto/\$1 $DB_NAME/g;
s/(OWNER TO) ruauto/\$1 $DB_USER/g;
s/CREATE OR REPLACE PROCEDURAL LANGUAGE/CREATE LANGUAGE/g;
s~/usr/home/ruauto\.ru~$HOME_DIR~g;
\$new_chunk = /^-- (Name|Data for Name):[^;]+; Type:[^;]+; Schema:[^;]+; Owner:/;
\$junk_start = \$new_chunk && /_replication|slony/ || /search_path = _replication/;
\$other_start = \$new_chunk && !\$junk_start;
print unless \$junk_start ... \$other_start;
PERL
fi
# Drop database
PSQL="psql -d postgres -h $DB_HOST -p $DB_PORT -w"
DB_SUPERUSER=`echo '\du' | $PSQL -U $DB_USER | grep Superuser | head -1 | awk '{print$1}'`
if [ "$DB_SUPERUSER" == "" ]; then
echo "Create role $DB_USER, please";
exit 1;
fi
PSQL_SUPER="$PSQL -U $DB_SUPERUSER"
echo "Dropping $DB_NAME..."
echo "drop database if exists $DB_NAME" | $PSQL_SUPER \
2> >(while read line; do echo -e "$bldred$line$txtrst" 1>&2; done)
# Checking if database is deleted
echo "" | $PSQL_SUPER -d $DB_NAME 2>/dev/null \
&& (echo "Db not dropped, you could retry loading same dump with ./pull_db local") && exit
# echo "Loading JSON-functions..."
# cd stuff/flex_data; ./build; cd ../..;
echo "Loading dump..."
$PSQL_SUPER -f $DUMP_TMP >/dev/null 2> >(while read line; do echo -e "$bldred$line$txtrst" 1>&2; done)
echo "Fixing triggers..."
./project/manage.py boardfixtriggers
echo "Creating nonlocal user..."
$PSQL_SUPER -d $DB_NAME -f stuff/nonlocal.sql >/dev/null 2> >(while read line; do echo -e "$bldred$line$txtrst" 1>&2; done)
./project/manage.py createpartitions schema --action=set_nonlocal_permissions
echo "Invalidating all cache..."
./project/manage.py invalidate all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment