#!/bin/sh
# ~jhs/public_html/bin/.sh/majordomo.sh
# See Also:
#	~jhs/bin/.sh/majordomo_rx.sh
#	/site/domain/berklix/etc/Distfile.master
# backup mail list subscription lists & archives
# called by /etc/crontab with eg:
#	#minute hour    mday    month   wday    who     command
#	7 20 * * 7 majordomo /home/jhs/public_html/bin/.sh/majordomo.sh
# Run this in the evening, not in the night, when I am still likely connected
# after, as I dont want to wait for big backup before important
# real mail from humans in the morning.

xx=`date +%Y_%m_%d_%H_%M`
yy="majordomo_backup ${xx}"
bak=/usr/backup/host/list/usr/local/majordomo.tars
dirs="/usr/local/majordomo.lists /usr/local/majordomo.archives /usr/local/majordomo.digests"
# Check Access
for i in $bak $dirs ; do
	cd $i || echo "majordomo.sh failed to cd $i" | \
		mail -s "${yy} cd" jhs-list
	cd $i || exit 1
	done

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

cd /usr/local/majordomo.lists && nice tar zcf $bak/majordomo.${xx}.lists.tgz .
uuencode majordomo.${xx}.lists.tgz < $bak/majordomo.${xx}.lists.tgz	\
				> $bak/uu.tmp
# Mail a copy to other remote servers + home for backup
#	JJLATER @list2 & @list3 are redundant Iff
#	/etc/Distfile gets run regularly, but I dont think it does- yet.
# mail -s "${yy} names" jhs-list,list_backup@list2,list_backup@list3 <	\
#	$bak/uu.tmp
# rdist6 now does inter server backup
mail -s "${yy} names" jhs-list < $bak/uu.tmp
# The keyword Subject:.*majordomo_backup above 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 emailin home is burdensomea
# I used to think :
#	just rdist to other server(s), &
#	manually 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.

exit 0

# WARNING WARNING !
# Only comment out the exit above 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 crap will get copied to /var/mail/jhs.cp
# so it will Double the space wasted.

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.

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

cd /usr/local/majordomo.digests && \
	nice tar zcf $bak/majordomo.${xx}.digests.tgz .
# $bak/majordomo.${xx}.digests.tgz	9 M at 2011_04_11
uuencode majordomo.${xx}.digests.tgz < $bak/majordomo.${xx}.digests.tgz \
				> $bak/uu.tmp
#  $bak/uu.tmp				12 M at 2011_04_11
mail -s "${yy} digests" $recipients < $bak/uu.tmp
rm -f	$bak/uu.tmp

cd /usr/local/majordomo.archives && \
	nice tar zcf $bak/majordomo.${xx}.archives.tgz .
# $bak/majordomo.${xx}.archives.tgz	73 Meg at 2011_04
uuencode majordomo.${xx}.archives.tgz < $bak/majordomo.${xx}.archives.tgz \
				> $bak/uu.tmp
# $bak/uu.tmp	100 Meg at 2011_04
mail -s "${yy} archives" $recipients < $bak/uu.tmp
rm -f	$bak/uu.tmp

