#!/bin/sh
# ~jhs/public_html/bin/.sh/majordomo_transmit.sh
# See Also:
#	~jhs/bin/.sh/majordomo_receive.sh
#	/site/domain/berklix/etc/Distfile.master
# Backs up mail list subscription lists to $bak, & mails tar of lists to home.
# Does not do mail list archives as Big, just reminds to ftp.
# Called by /etc/crontab with eg:
#	#minute hour    mday    month   wday    who     command
#	7 20 * * 7 majordomo /home/jhs/public_html/bin/.sh/majordomo_transmit.sh

date=`date -u +%Y-%m-%dT%H:%M:%SZ`
name=`basename $0`
bak=/usr/backup/host/list/usr/local/majordomo.tars
dirs="/usr/local/majordomo.lists /usr/local/majordomo.archives /usr/local/majordomo.digests"

recipients=jhs-list
# recipients=jhs-list,list_backup@list2
#	Uncomment line above if I want a copy in remote backup server mbox
# recipients=jhs-list,list_backup@list2,list_backup@list3
#	Uncomment line above if I want a copy in both remote backup server
#	mbox after host flat comes back into service.
#	Currently list3 is currently same as list1 = master, so omit it.

# Check Access
for i in $bak $dirs ; do
	cd $i || echo "$0 failed to cd $i" | \
		mail -s "${name} cd ${date} " jhs-list
	cd $i || exit 1
	done

# Create, then remove read permission for others, before writing data.
tarsf="$bak/majordomo.${date}.lists.tgz $bak/majordomo.${date}.archives.tgz $bak/majordomo.${date}.digests.tgz"
rm -f		$tarsf $bak/uu.tmp
touch		$tarsf $bak/uu.tmp
chmod 640  	$tarsf $bak/uu.tmp

# chown jhs	$tarsf $bak/uu.tmp	
#	chown fails when called from /etc/crontab with caller=majordom
#	but not necessary, as for ftp, jhs is in group majordom

cd /usr/local/majordomo.lists && \
	nice tar zcf $bak/majordomo.${date}.lists.tgz .
uuencode majordomo.${date}.lists.tgz < $bak/majordomo.${date}.lists.tgz	\
				> $bak/uu.tmp
# Mail a copy to other remote servers + home for backup
#	JJLATER @list2 & @list3 are redundant If
#	/etc/Distfile gets run regularly.
# mail -s "${name} names ${date}" \
#	jhs-list,list_backup@list2,list_backup@list3 < $bak/uu.tmp
# rdist6 (manually called) now does inter server backup
mail -s "${name} names ${date}" jhs-list < $bak/uu.tmp
# The name of this shell is know to ~jhs/.procmailrc
rm -f	$bak/uu.tmp
# The lists are useful both for backup away from servers, & for autopsy
# when people joined & left lists.

# /etc/Distfile also copies trees (not tars) to other remote servers,

# Archives & digests are Big, so regularly emailing home is burdensome,
# I used to think :
#	just rdist to other server(s), &
#	manualy sftp them to home when desired,
# However I forgot between 2009-08-18 & 2011-04-11,
# so as I now also have faster DSL, recommence mail.

cd /usr/local/majordomo.digests && \
	nice tar zcf $bak/majordomo.${date}.digests.tgz .

# Do digests first cos only just 13M, & gives some warning
# before the horrific 100 Meg hits recipients

cd /usr/local/majordomo.archives && \
	nice tar zcf $bak/majordomo.${date}.archives.tgz .

hostname=`hostname`
echo "${name} ${date} Please fetch ftp://`hostname`$bak/majordomo.${date}.digests.tgz" | \
	mail -s "${name} fetch ${date}" ${recipients}
echo "${name} ${date} Please fetch ftp://`hostname`$bak/majordomo.${date}.archives.tgz" | \
	mail -s "${name} fetch ${date}" ${recipients}

# WARNING !
# Only comment out the exit below if you are prepared for the gate
# & internal procmail host & remote servers to receive a 13 Meg Mail
# & then a 100 Meg mail.
# You will be in Deep trouble if you do not have:
#	a fast email connection
#	a big file system for queue , on
#		sender,
#		remote server recipients
#		home gate
#		home mailbox hist
#	a divert rule before all the spam filter rules, on home mbox host.
# remember also this will get copied to /var/mail/jhs.cp on server,
# so it will double the space wasted.

exit 0

# uuencode majordomo.${date}.digests.tgz < $bak/majordomo.${date}.digests.tgz \
# 				> $bak/uu.tmp
# mail -s "${name} digests ${date}" $recipients < $bak/uu.tmp
# rm -f	$bak/uu.tmp
# 
# # $bak/majordomo.${date}.archives.tgz	73 Meg at 2011-04
# uuencode majordomo.${date}.archives.tgz < $bak/majordomo.${date}.archives.tgz \
# 				> $bak/uu.tmp
# # $bak/uu.tmp	100 Meg at 2011-04
# mail -s "${name} archives ${date}" $recipients < $bak/uu.tmp
# rm -f	$bak/uu.tmp
