#!/bin/bash
#              /media/TailsData/Persistent/.update.sh
#                      /usr/local/bin/tails.update.sh
# http://crystalfaeries.net/posix/bin/tails.update.sh
# celeste:crystalfaery 2015-08-22 16:07:49+00:00
# Update /media/TailsData/Persistent (this USB Flash Drive)

# This script is manually executed to update the encrypted persistent storage:
# Plugin USB stick brings up LUKS mount dialogue, then once mounted...
# cd /media/TailsData/Persistent
# ./.update.sh
# cd
# sudo umount /media/TailsData
# 
# At the same level the file .exclude.txt controls NON-copying.
# At the end of the update we generate a report in .du.txt
# showing us disk usage of what was or was not backed-up,
# in order to help us manually update the .exclude.txt file
# to optimize what is important to us to carry encrypted.

/usr/local/bin/synctxt	# merge .todo lists

# we operate in the cloned home directory	#######	#######	#######	#######
mkdir -p	/media/TailsData/Persistent/home			|| exit 1
cd		/media/TailsData/Persistent/home			|| exit 2

# EXCLUSION IMPLIES DELETE
cat ../.exclude.txt | sudo xargs rm -rf

# sync /home	 ( NOTE: paths in .exclude.txt are relative to source /home )
sudo rsync -auvzH --delete --exclude-from=../.exclude.txt --exclude='.*.swp' --exclude=local --exclude=www/cgi-bin /home ..

# sync /usr/local to /media/TailsData/Persistent/home/local
sudo rsync -auvzH --delete	\
--exclude-from=../.exclude.txt	\
--exclude='.*.swp'		\
--exclude=games/		\
--exclude=iso/			\
--exclude=lib/			\
--exclude=src/			\
     /usr/local .

# sync /usr/lib/cgi-bin/
sudo rsync -auvzH --delete /usr/lib/cgi-bin www

# sync bookmarks BIDIRECTIONALLY
rsync -auvzH	/home/kahealani/.mozilla/firefox/*.default/bookmarks.html \
		      kahealani/.mozilla/firefox/*.default/bookmarks.html
rsync -auvzH	      kahealani/.mozilla/firefox/*.default/bookmarks.html \
		/home/kahealani/.mozilla/firefox/*.default/bookmarks.html

# now we operate in the Persistent directory	#######	#######	#######	#######
cd		/media/TailsData/Persistent			|| exit 2

# calculate disk usage report
cp /dev/null					   /tmp/du.$$.txt
for d in `cat .exclude.txt`
do
	du -s /home/$d | sed 's/\/home\///g'	>> /tmp/du.$$.txt
done
df --full | head -n 1		>  .du.txt	# Captions
df --full | grep /media/Tails	>> .du.txt	# This partition
echo ""				>> .du.txt
echo "	EXCLUDED:"		>> .du.txt
# filter out our overhead files
grep -v .du.txt /tmp/du.$$.txt	|\
grep -v .exclude.txt		|\
grep -v .update.sh		|\
sort -rn			>> .du.txt
echo ""				>> .du.txt
echo "	INCLUDED:"		>> .du.txt
/usr/local/bin/dudir		|\
sed 's/\/media\/TailsData\/Persistent\/home\///' >> .du.txt

# manually review our choice of exclusions
vi .du.txt .exclude.txt .update.sh

# copy from the Flash Drive to local hard disk to allow copying to other Flash Drives
rsync -auvzH .du.txt		 ~/txt/tails.du.txt
rsync -auvzH .exclude.txt	 ~/txt/tails.exclude.txt
rsync -auvzH .update.sh	/usr/local/bin/tails.update.sh


syntax highlighted by Code2HTML, v. 0.9.1