File:  [Local Repository] / badi / public_scripts / parallelstarter / db_backup / generate_arglist
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Thu Sep 6 21:16:42 2012 UTC (11 years, 8 months ago) by adi
Branches: MAIN
CVS tags: HEAD
Correct default port variable.

# Prepare db_backup to be run with parallelstarter:
# generate arglist from password file

# step through DBLIST and convert each line 
# into a set of command line args for db_backup


# the default ports
function getdefault_dbport { 
	if [ "$curtype" = "mysql" ]; then
		echo -n "3306"
	elif [ "$curtype" = "postgresql" ]; then
		echo -n "5432"
	fi
}


if [ -e "$ARGLIST_FILE" ]; then
	rm "$ARGLIST_FILE"
fi

while read dbinfo; do

	# ignore comment and empty lines
	if [ $(echo "$dbinfo" | egrep -c "^[ \t]*#.*$") -gt 0 ]; then
 	       continue
	fi
	
	# ignore empty lines or malformed lines
	if [ $(echo "$dbinfo" | sed 's/[^:]//g' | wc -m) -lt 4 ]; then
		continue
	fi

	curhost="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $1}')`"
    curdb="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $3}')`"
    curuser="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $4}')`"
    curtype="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $2}')`"
	curpw="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $5}')`"

	curport="$(echo "$curhost" | sed -e "s/.*:\([0-9]\{1,\}\) *$/\1/")"
	if [ "$curhost" = "$curport" ]; then
		curport="$(getdefault_dbport)"
	else
		# remove port information from host string
		curhost="$(echo "$curhost" | sed -e "s/\(.*\):[0-9]\{1,\} *$/\1/")"		
	fi
	echo "-h $curhost -p $curport -D $curdb -u $curuser -c $DBLIST -t $curtype" >> "$ARGLIST_FILE"
	
	# write .pgpass in case there is a PostgreSQL DB involved
	if [ "$curtype" = "postgresql" ]; then
		# see if password is already registered
		if [ ! -e "$HOME/.pgpass" ]; then
			touch "$HOME/.pgpass"
			chmod 600 "$HOME/.pgpass"
		fi
		if [ $(grep -c "$curhost:$curport:$curdb:$curuser" "$HOME/.pgpass") -eq 0 ]; then
			# add the line to .pgpass
			echo "$curhost:$curport:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
		elif [ $(grep -c "$curhost:$curport:$curdb:$curuser:$curpw" "$HOME/.pgpass") -eq 0 ]; then
			# the pw has changed; replace the password with new value
			mv "$HOME/.pgpass" "$HOME/.pgpass.old"
			touch "$HOME/.pgpass"
			chmod 600 "$HOME/.pgpass"
			grep -v "$curhost:$curport:$curdb:$curuser" "$HOME/.pgpass.old" >> "$HOME/.pgpass"
			echo "$curhost:$curport:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
		fi
	fi

done < "$DBLIST"

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>