#!/bin/bash
# FN: pgsnapshot.
# AB: creates and mounts snapshot of $PGDATA; rsyncs from compa5;
#		reinitializes database on backup server; unmounts and removes snapshot
# DT: 2003-03-05
# AU: S. Murthy Kambhampaty

# An e-mail address for the monitor:
pgMonitor="<DBA e-mail address here>"

# "log" instances of this script as follows:
# (this form is good for unattended scripts; for manually run scripts
# 	you might want to save and restore stdin and stdout; see
#  http://www.tldp.org/LDP/abs/html/x11830.html#REDIR, example 16-3)
LogFile="/var/log/`basename $0 .sh`-`date +%Y%m%d-%H%M`"
exec  > "$LogFile"
exec 2> "$LogFile.err"
echo "$(date +%c): Backup initiated"


pgSRV="$(echo -e `hostname` | awk 'BEGIN{FS="."}{print $1}')"
BackupSRV="host2"

DataFS="/home/db"
DataLV="/dev/VG00/LVdb"

SnapNM="pgsnapshot"
SnapLV="/dev/VG00/$SnapNM"
SnapDir="/mnt/$SnapNM"

RshCMD="ssh -i /home/postgres/.ssh/id_rsa postgres@$BackupSRV"

RsyncSRC="$pgSRV::pgsnapshot/pgsql/"
RsyncDST="/home/db/mirrors/$pgSRV"
RsyncOpts="-avW --stats --exclude='postmaster.pid' --delete"




# First vacuum all databases
echo $'\n\n' "$(date +%c): Vacuuming databases on `hostname`"
# Mark vacuum start and end in the log to ease log navigation
/usr/local/pgsql/bin/psql -U pguser -d template1 -c \
	"select '+++++ Nightly Vacuum About To Commence +++++'" >/dev/null
/usr/local/pgsql/bin/vacuumdb -U postgres -q -a && \
	/usr/local/pgsql/bin/psql -U pguser -d template1 -c \
		"select '+++++ Nightly Vacuum Completed +++++'" >/dev/null && \
	echo $'\n\t' "$(date +%c): Successfully vacuumed databases on `hostname`"


# Now create and mount the snapshot volume
echo $'\n\n' "$(date +%c): Snapshot creation and mounting"
/usr/sbin/xfs_freeze -f "$DataFS"
/sbin/lvcreate -s -l 1536 -n "$SnapNM" "$DataLV"; SnapTest=$? # Record exit status of lvcreate
/usr/sbin/xfs_freeze -u "$DataFS"

# If snapshot creation succeeded, mount the snapshot volume; else bail
if [[ $SnapTest -eq 0 ]]; then
	echo $'\n\t' "$(date +%c): Successfully created snapshot"
	mount "$SnapLV" "$SnapDir" -o nouuid,norecovery,ro
else
	echo $'\n\t' "$(date +%c): Snapshot failed." \
		$'\n\t' "(Is it already mounted?)" \
		$'\n\t' "!!! Terminating backup ..." >&2
	exit
fi


# Do the backup
# 	The backup is controlled from a remote shell on the backup server
#	to keep CPU utilization on the database server low
echo $'\n\n' "$(date +%c): Rsync from snapshot to mirror on $BackupSRV"
$RshCMD rsync "$RsyncOpts" "$RsyncSRC" "$RsyncDST" 2>&1 && \
	echo "$(date +%c): Rsync complete"; echo $'\n\n'

# Remove the snapshot on rsync completion
umount "$SnapLV"
/sbin/lvremove -f "$SnapLV"


# "Reinitialize" the snapshot of the database on the backup server
echo $'\n\n' "$(date +%c): Start postmaster on $BackupSRV, capture output, and shutdown"
$RshCMD /usr/local/pgsql-7.3/bin/postmaster -D "$RsyncDST/data" 2>&1 &
sleep 60 # give the postmaster time for recovery
$RshCMD /usr/local/pgsql-7.3/bin/pg_ctl stop -D "$RsyncDST/data"
echo $'\n\n' "$(date +%c): Attempted to reinitialize backup; check for errors above"

if [ ! -s "$LogFile.err" ]; then
	rm -f "$LogFile.err"
	echo $'\n\n' "$(date +%c): Backup completed (without errors)-ish" # Why does postmaster log to sterr, rather than stdout? :-(
else
	cat "$LogFile.err" | /usr/sbin/postfix.sendmail $pgMonitor
	echo $'\n\n' "$(date +%c): Backup encountered errors" \
		$'\t' "Notified $pgMonitor"
fi