#!/bin/bash
#
# dumps all DBs to the given directory, defaulting to /var/www/postgres.dumps

TARGETDIR="$1"
if [ -z "$TARGETDIR" ] ; then
  #echo "Defaulting target dir to /var/www/postgres.dumps"
  #TARGETDIR="/var/www/postgres.dumps"
  echo "$(date +%Y-%m-%d\ %H:%M:%S): FATAL: No directory name given as \$1"
  echo "You might want to give /var/www/postgres.dumps"
  echo ""
  echo "Syntax: $0 targetDirectory [--gzip-early] [-x db2exclude ...]"
  echo ""
  echo "  --gzip-early   does the gzip-ing directly on mysqldumps's output,"
  echo "                 saves disc space but increases the risk of damaged dumps"
  echo "                 By default we now do the gzip after the dump."
  echo "  -x             excludes the specified databases from dumping;"
  echo "                 template0 and template1 are never dumped."
  echo ""
  exit 1
fi

if [ ! -d $TARGETDIR ] ; then
  echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Auto-creating target dir $TARGETDIR"
  mkdir -pv $TARGETDIR || exit 2
fi

if [ ! -d $TARGETDIR ] ; then
  echo "$(date +%Y-%m-%d\ %H:%M:%S): FATAL: Target dir not existent: $TARGETDIR"
  exit 3
fi

shift

export GZIPEARLY="no"
if [ "$1" = "--gzip-early" ] ; then
  GZIPEARLY="yes"
  shift
fi

EXCLUDELIST=""
if [ "$1" = "-x" ] ; then
 shift
 EXCLUDELIST="$*"
 echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Found -x, take remaining arguments as exclude list: $EXCLUDELIST"
fi

DBLIST=$(LANG="C" sudo -u postgres psql -l |cut -f2 -d' ' |egrep '^[a-z0-9_]+$' |egrep -v '^(template0|template1)$')
if [ -z "$DBLIST" ] ; then
  echo "$(date +%Y-%m-%d\ %H:%M:%S): FATAL: Could not detect list of databases to save."
  exit 11
fi

#echo "DBs to save are:"
#echo "$DBLIST"

export FAILEDDBS=""

for DBNAME in $DBLIST
do
  DUMPTHISDB="yes"
  if [ -n "$EXCLUDELIST" ] ; then
    for EXCLUDEDB in $EXCLUDELIST 
    do
      if [ "$EXCLUDEDB" = "$DBNAME" ] ; then
        echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Skipping excluded DB $DBNAME."
        DUMPTHISDB="no"
      fi
    done
  fi
  if [ "$DUMPTHISDB" = "yes" ] ; then 
    if [ "$GZIPEARLY" = "yes" ] ; then 
      echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Dumping DB $DBNAME to $TARGETDIR/$DBNAME.sql.gz"
      /usr/lib/bsclient/postgres_dumpdb2file $DBNAME $TARGETDIR/$DBNAME.sql.gz || FAILEDDBS=$( echo "${FAILEDDBS}" ; echo "${DBNAME} (dump failed)" )
    else
      if [ -f $TARGETDIR/$DBNAME.sql ] ; then
        echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Removing old $TARGETDIR/$DBNAME.sql"
        rm -vf $TARGETDIR/$DBNAME.sql
      fi
      echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Dumping DB $DBNAME to $TARGETDIR/$DBNAME.sql"
      if /usr/lib/bsclient/postgres_dumpdb2file $DBNAME $TARGETDIR/$DBNAME.sql ; then
        echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Removing eventual old $TARGETDIR/$DBNAME.sql.gz"
        rm -vf $TARGETDIR/$DBNAME.sql.gz || FAILEDDBS=$( echo "${FAILEDDBS}" ; echo "${DBNAME} (rm-ing old .sql.gz failed)" )
        echo "$(date +%Y-%m-%d\ %H:%M:%S): INFO: Gzipping new $TARGETDIR/$DBNAME.sql"
        gzip $TARGETDIR/$DBNAME.sql || FAILEDDBS=$( echo "${FAILEDDBS}" ; echo "${DBNAME} (gzipping .sql failed)" )
      else
        echo "$(date +%Y-%m-%d\ %H:%M:%S): ERROR: FAILED to dump ${DBNAME} to $TARGETDIR/$DBNAME.sql.gz, remembering this but continuing with eventual next DB"
        FAILEDDBS=$( echo "${FAILEDDBS}" ; echo "${DBNAME} (dump failed)" )
      fi
    fi
  fi
done

echo ""

if [ -z "${FAILEDDBS}" ] ; then
  echo "$(date +%Y-%m-%d\ %H:%M:%S): GOOD: Done dumping databases to $TARGETDIR"
  echo ""
  exit 0
fi

echo "$(date +%Y-%m-%d\ %H:%M:%S): WARNING: Done dumping databases from container ${DOCKERCT} to $TARGETDIR, but some databases could not be dumped:${FAILEDDBS}"
echo ""

exit 1

