#!/bin/sh

dir=/data/bpc-sql-backup

if [ "`id -un`" != 'backuppc' ] ; then
	echo "Need to run this as backuppc user!"
	exit
fi

function ssh_dump () {
	host=$1
	type=$2
	db_name=$3

	echo -n `date +"%Y-%m-%d %H:%M:%S"` " "
	#psql -q -c "vacuum full analyze" $1

	file="$host-$db_name-`date +%d`.$type.sql.bz2"
	tmp="$file.tmp"
	if [ ! -e "$file" ] ; then
		if [ "$type" == 'pg' ] ; then
			ssh -C root@$host "sudo -u postgres pg_dump $db_name" | bzip2 > $tmp && mv $tmp $file || rm $tmp
		elif [ "$type" == 'my' ] ; then
			ssh -C root@$host "mysqldump $db_name" | bzip2 > $tmp && mv $tmp $file || rm $tmp
		else
			echo "Unknown database type: $type, SKIPPED"
		fi
	else
		echo -n "SKIPPED, exists: "
	fi
	du -h $file
}

ssh_dump jglcrm.pliva.hr pg jglcrm-prod
ssh_dump karaka.pliva.hr pg dotproject
ssh_dump stwiki.pliva.hr pg NLW
ssh_dump swaf.pliva.hr my ac
ssh_dump swaf.pliva.hr pg humanusnet
ssh_dump tmc.pliva.hr pg tmc

