#!/bin/sh
#
# Copyright (c) 2009 Sakura Yamamoto
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

#
# $B$3$C$+$i%P%C%/%"%C%W$N@_Dj$G$C$9!#(B
# $B%5!<%P$N@_Dj$K9g$o$;$FJT=8$7$F$A$g!<$@$$!*(B
#

# $B%P%C%/%"%C%W%U%!%$%kL>!#(B
# $B%G%U%)%k%H$O(B backup-YYYY-MM-DD.tar.gz $B$K$J$C$F$^$9!#(B
BACKUP_FILENAME="backup-`date '+%Y-%m-%d'`.tar.gz"

# $B%P%C%/%"%C%WBP>]$N%G%#%l%/%H%j$H$+%U%!%$%k$H$+!#(B
BACKUP_TARGETS="www"

# MySQL $B$N%P%C%/%"%C%W$r$9$k$J$i(B yes$B!"$7$J$$$J$i(B no$B!#(B
MYSQLDUMP="yes"

# MySQL $B%"%+%&%s%H!#>e$+$i%f!<%6L>!"%Q%9%o!<%I!"%[%9%HL>!"%G!<%?%Y!<%9L>$l$9!#(B
# $BBgJ8;zItJ,$r<+J,$N%"%+%&%s%H$N$KJQ$($F$M!#(B
MYSQLDUMP_USER="USERNAME"
MYSQLDUMP_PASSWORD="PASSWORD"
MYSQLDUMP_HOST="HOSTNAME"
MYSQLDUMP_DBNAME="DBNAME"

# MySQL $B$N%P%C%/%"%C%W%U%!%$%kL>!#(B
# $B:G=*E*$K$3$N%U%!%$%k$b(B BACKUP_FILENAME $B$K%P%C%/%"%C%W$5$l$^$9$N$G(B
# $B0l;~%U%!%$%kL>$_$?$$$J$b$N$H;W$C$F$/$@$5$$!#(B
MYSQLDUMP_FILENAME="mysql.dump"

# mysqldump $B%3%^%s%I!#(B
# MySQL4 $B$r;H$C$F$k>l9g$O(B mysqldump-4.0 $B$KJQ99$9$k$3$H!#(B
MYSQLDUMP_COMMAND="/usr/local/bin/mysqldump"

#
# $B@_Dj=*$o$j!#(B
# $B0J2<$N%3!<%IJQ$($F$b$i$C$F$b$$$$$1$I!"$V$C2u$l$F$bCN$i$J$$$s$@$+$i!*(B
#

dobackup=yes
verbose=no
usage="Usage: ${0##*/} [-hvn]"

while getopts 'vnh' ch; do
	case $ch in
	v)	verbose=yes;;
	n)	dobackup=no;;
	h)	echo "$usage"; exit 0;;
	*)	echo "$usage"; exit 1;;
	esac
done

cd $HOME || exit 1

if [ "$MYSQLDUMP" = 'yes' ]; then
	BACKUP_TARGETS="$BACKUP_TARGETS $MYSQLDUMP_FILENAME"
fi

if [ "$verbose" = 'yes' ]; then
	cat <<__EOF__
BACKUP_TARGETS		$BACKUP_TARGETS
MYSQLDUMP		$MYSQLDUMP
MYSQLDUMP_COMMAND	$MYSQLDUMP_COMMAND
MYSQLDUMP_FILENAME	$MYSQLDUMP_FILENAME
MYSQLDUMP_USER		$MYSQLDUMP_USER
MYSQLDUMP_HOST		$MYSQLDUMP_HOST
MYSQLDUMP_DBNAME	$MYSQLDUMP_DBNAME

__EOF__
fi

if [ "$dobackup" = 'no' ]; then
	exit 0
fi

echo "BACKUP started at "`date`

if [ "$MYSQLDUMP" = 'yes' ]; then
	MY_CNF=`mktemp "$HOME/backupXXXXXX"`
	cat <<__EOF__ > "$MY_CNF"
[client]
user=$MYSQLDUMP_USER
password=$MYSQLDUMP_PASSWORD
host=$MYSQLDUMP_HOST
__EOF__

	echo "mysqldump started at "`date`

	$MYSQLDUMP_COMMAND "--defaults-extra-file=$MY_CNF" \
		"$MYSQLDUMP_DBNAME" > "$MYSQLDUMP_FILENAME" || {
		rm -f "$MY_CNF"
		exit 1
	}
	rm -f "$MY_CNF"

	echo "mysqldump finished at "`date`
fi

echo "tar started at "`date`
tar chopfz "$BACKUP_FILENAME" $BACKUP_TARGETS || exit 1
echo "tar finished at "`date`

if [ "$MYSQLDUMP" = 'yes' ]; then
	echo "Cleanup temporary files"
	rm -f "$MYSQLDUMP_FILENAME"
fi

echo "BACKUP finished at "`date`

echo "Backup files $BACKUP_FILENAME created"
