File:  [Local Repository] / badi / public_scripts / parallelstarter / db_backup / generate_arglist
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Mon Jul 18 11:49:04 2011 UTC (12 years, 11 months ago) by adi
Branches: MAIN
CVS tags: HEAD
Initial revision

# 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

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}')`"

	echo "-h $curhost -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:$PGPORT:$curdb:$curuser" "$HOME/.pgpass") -eq 0 ]; then
			# add the line to .pgpass
			echo "$curhost:$PGPORT:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
		elif [ $(grep -c "$curhost:$PGPORT:$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:$PGPORT:$curdb:$curuser" "$HOME/.pgpass.old" >> "$HOME/.pgpass"
			echo "$curhost:$PGPORT:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
		fi
	fi

done < "$DBLIST"

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