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, 9 months ago) by adi
Branches: MAIN
CVS tags: HEAD
Correct default port variable.

    1: # Prepare db_backup to be run with parallelstarter:
    2: # generate arglist from password file
    3: 
    4: # step through DBLIST and convert each line 
    5: # into a set of command line args for db_backup
    6: 
    7: 
    8: # the default ports
    9: function getdefault_dbport { 
   10: 	if [ "$curtype" = "mysql" ]; then
   11: 		echo -n "3306"
   12: 	elif [ "$curtype" = "postgresql" ]; then
   13: 		echo -n "5432"
   14: 	fi
   15: }
   16: 
   17: 
   18: if [ -e "$ARGLIST_FILE" ]; then
   19: 	rm "$ARGLIST_FILE"
   20: fi
   21: 
   22: while read dbinfo; do
   23: 
   24: 	# ignore comment and empty lines
   25: 	if [ $(echo "$dbinfo" | egrep -c "^[ \t]*#.*$") -gt 0 ]; then
   26:  	       continue
   27: 	fi
   28: 	
   29: 	# ignore empty lines or malformed lines
   30: 	if [ $(echo "$dbinfo" | sed 's/[^:]//g' | wc -m) -lt 4 ]; then
   31: 		continue
   32: 	fi
   33: 
   34: 	curhost="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $1}')`"
   35:     curdb="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $3}')`"
   36:     curuser="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $4}')`"
   37:     curtype="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $2}')`"
   38: 	curpw="`echo $(echo "$dbinfo" | awk -F ' : ' '{print $5}')`"
   39: 
   40: 	curport="$(echo "$curhost" | sed -e "s/.*:\([0-9]\{1,\}\) *$/\1/")"
   41: 	if [ "$curhost" = "$curport" ]; then
   42: 		curport="$(getdefault_dbport)"
   43: 	else
   44: 		# remove port information from host string
   45: 		curhost="$(echo "$curhost" | sed -e "s/\(.*\):[0-9]\{1,\} *$/\1/")"		
   46: 	fi
   47: 	echo "-h $curhost -p $curport -D $curdb -u $curuser -c $DBLIST -t $curtype" >> "$ARGLIST_FILE"
   48: 	
   49: 	# write .pgpass in case there is a PostgreSQL DB involved
   50: 	if [ "$curtype" = "postgresql" ]; then
   51: 		# see if password is already registered
   52: 		if [ ! -e "$HOME/.pgpass" ]; then
   53: 			touch "$HOME/.pgpass"
   54: 			chmod 600 "$HOME/.pgpass"
   55: 		fi
   56: 		if [ $(grep -c "$curhost:$curport:$curdb:$curuser" "$HOME/.pgpass") -eq 0 ]; then
   57: 			# add the line to .pgpass
   58: 			echo "$curhost:$curport:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
   59: 		elif [ $(grep -c "$curhost:$curport:$curdb:$curuser:$curpw" "$HOME/.pgpass") -eq 0 ]; then
   60: 			# the pw has changed; replace the password with new value
   61: 			mv "$HOME/.pgpass" "$HOME/.pgpass.old"
   62: 			touch "$HOME/.pgpass"
   63: 			chmod 600 "$HOME/.pgpass"
   64: 			grep -v "$curhost:$curport:$curdb:$curuser" "$HOME/.pgpass.old" >> "$HOME/.pgpass"
   65: 			echo "$curhost:$curport:$curdb:$curuser:$curpw" >> "$HOME/.pgpass"
   66: 		fi
   67: 	fi
   68: 
   69: done < "$DBLIST"

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