#!/bin/bash
set +x
# (No suffix in the spirit of this being a bin directory)

# 20050428
# Changed MYSQL dump syntax to --opt
# Added block for DOMYSQLHACK
# 20050426
# Added another permissions hack, DOPERMFIX

# 20050423
#
# Created to push my Gallery2 g2data stuff up to example.com
# so I can do whatever I want on my local machine and then
# publish the stuff to example.com whenever I feel like it.
# This makes it much faster/easier to deal with organizing
# my photos since I need not upload 100MB of JPGs at 15KB/s.

# For this to work, we need to do a number of things.
#
# First, upload any local file changes, such as from
# upgrades, to TrekWeb.  Generally an upgrade script
# must be run on the database for G2 upgrades, so
# nothing will work until the next step.
#
# Second, the local MySQL database for G2 must be dumped
# and uploaded to TrekWeb, where it can be imported,
# overwritting anything that's in the G2 database there.
# This is fine, since the critical settings are in
# config.php which we will --exclude.
#
# Third, we will lose all our g2_viewCount from g_viewCount,
# so we explicitly create a new table with this data, 
# then restore it after we have completed the drop / create
# sequence for refreshing the G2 database on TrekWeb.
#
# Fourth, check filesystem permissions since TrekWeb has
# a different group setup than I have locally.
#
# All of this is done using ssh with RSA keys and
# rsync over ssh.  There is no passphrase (bad me bad) so
# it can be cron'd or whatever without incident.

RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
SCP=/usr/bin/scp
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
GZIP=/bin/gzip
RM=/bin/rm

RMYSQL=/usr/bin/mysql
RGUNZIP=/bin/gunzip
RCHMOD=/bin/chmod
RCHOWN=/bin/chown
X=/usr/bin/xargs
F=/usr/bin/find

# Target host

HOST=example.com

# MySQL

LU=myg2info
LP=myg2info
LD=myg2info
RU=$LU
RP=$LP
RD=$LD

# Paths
# $SRC must end with / for $RSYNC or mess up happens!

SRC=/var/www/gallery2/
TARGET=/home/httpd/example.com/gallery2
# Uncomment if you want $RSYNC to delete files on $TARGET
# that do NOT exist on $SRC anymore.
DELETE=--delete
# What's more, you can uncomment this if you need a bandwidth
# limit for, say, ADSL or something.  The value is in KB/s.
BWLIMIT=--bwlimit=15

# Hack so --delete can remove files in cache/ and templates_c/
# or consistency errors crop up causing G2 to bail.

DOPERMFIX=1
PERMFIXSH=/usr/local/bin/fixg2perms

# Hack so MySQL is updated properly.  If you're using a modern
# version of MySQL, such as 4.x, you can disable this.
DOMYSQLHACK=1
MYSQLHACKPL='$HOME/bin/mysqlg2hack'

# User to fix permissions for for $F

USER=foo

### Error checking at some point with $? perhaps?

# Transfer any files changes, including JPGs and G2 updates

if [ $DOPERMFIX -ne 0 ] ; then
	$SSH $HOST $PERMFIXSH || exit 1
fi

$RSYNC -a $DELETE $BWLIMIT --exclude 'config.php' \
  --exclude 'cache/*' --exclude='sessions/*' \
  --exclude 'smarty/templates_c/*' \
  $SRC $HOST:$TARGET 1>/dev/null

[ $? -ne 0 ] && { echo "$RSYNC failed"; exit 1; }

# Dump the local database, compress it, copy it, and clean up

if [ -f /bin/mktemp ] ; then
	TMPFILE=$(mktemp /tmp/gallery2.XXXXXX) || exit 1
else
	TMPFILE=/tmp/gallery2.$$
	touch $TMPFILE || exit 1
fi

if $MYSQLDUMP --opt -u$LU -p$LP $LD > $TMPFILE ; then
	$GZIP -f --best $TMPFILE || exit 1
	$SCP $TMPFILE.gz $HOST:gallery2.sql.gz 1>/dev/null || exit 1
	$SSH $HOST "$RGUNZIP -f gallery2.sql.gz" || exit 1
	$RM $TMPFILE.gz
else
	echo "$MYSQLDUMP seems to have failed!"
	exit 1
fi

# Drop our temp_views, then create it with current values

SQL='
DROP TABLE IF EXISTS temp_views;
CREATE TABLE temp_views SELECT g_itemID,g_viewCount from g2_ItemAttributesMap;
'
ssh $HOST "$RMYSQL -u$RU -p$RP $RD <<EOF
$SQL
EOF
"
[ $? -ne 0 ] && { echo "$RMYSQL CREATE failed"; exit 1; }

# Finally, import database dump which has --add-drop-table

$SSH $HOST "$RMYSQL -u$RU -p$RP $RD < gallery2.sql"
[ $? -ne 0 ] && { echo "$RMYSQL IMPORT failed"; exit 1; }

if [ $DOMYSQLHACK -ne 0 ] ; then

	# Fix our g_viewCount using a Perl script hack

	$SSH $HOST '$HOME/bin/mysqlg2hack'
	[ $? -ne 0 ] && { echo "mysqlg2hack script failed"; exit 1; }

else
	# MySQL 3.23.58 doesn't support doing multiple tables on UPDATE

	SQL='
	UPDATE g2_ItemAttributesMap, temp_views SET \
	g2_ItemAttributesMap.g_viewCount = temp_views.g_viewCount \
	WHERE g2_ItemAttributesMap.g_itemID = temp_views.g_itemID
	'

	# EOF likes to be on a line by itself
	ssh $HOST "$RMYSQL -u$RU -p$RP $RD <<EOF
$SQL
EOF
"
	[ $? -ne 0 ] && { echo "$RMYSQL UPDATE failed"; exit 1; }
fi

# Fix permissions

$SSH $HOST \
"$F $TARGET -user $USER | $X $RCHOWN -R :www"
$SSH $HOST \
"$F $TARGET -type d -user $USER | $X $RCHMOD g=rwxs"
$SSH $HOST \
"$F $TARGET -type f -user $USER | $X $RCHMOD g=rw"

exit 0


