<commit_before>#! /bin/bash
# make a backup of my home directory on the raspberripi at pcextreme in the Netherlands box by pushing the git repo.
# Ideas from:
# http://www.linuxquestions.org/questions/linux-newbie-8/remote-encrypted-unattended-file-server-650280/
# http://www.vectorspace.dk/2009/06/encrypted-partition-on-debian-5-0-with-secret-key-on-usb-stick/
# http://www.vidarholen.net/contents/blog/?p=8

set -e

# the following environment variables must be exported prior to calling this script:
#extHOST=
#extPath=
#extImgName=
#extUser=
#imageSize=
#loopDevice=

mkdir -p /tmp/${extHOST}_hd/A
chmod 750 /tmp/${extHOST}_hd/A
sshfs ${extUser}@${extHOST}:${extPath} /tmp/${extHOST}_hd/A -o allow_root

if [ ! -e /tmp/${extHOST}_hd/A/${extImgName}.img ]; then
	# prepare the disk image on the remote machine
	ssh ${extUser}@${extHOST} "(dd if=/dev/urandom of=${extPath}/${extImgName}.img bs=1M count=${imageSize}; chmod 700 ${extPath}/${extImgName}.img)"
	sudo modprobe dm-crypt
#	sudo modprobe aes
#	sudo modprobe sha256
	# prepare a key file and encrypt it
	dd if=/dev/urandom | tr -d '\n' | dd bs=1 count=64 of=key_${extHOST}_${extImgName}.txt
	gpg -e key_${extHOST}_${extImgName}.txt
	git add key_${extHOST}_${extImgName}.txt.gpg
	sudo cryptsetup -c aes-xts-plain -s 512 luksFormat /tmp/${extHOST}_hd/A/${extImgName}.img key_${extHOST}_${extImgName}.txt
	
	sudo losetup /dev/${loopDevice} /tmp/${extHOST}_hd/A/${extImgName}.img

	sudo cryptsetup luksOpen /dev/${loopDevice} ${extHOST} < key_${extHOST}_${extImgName}.txt

	sudo mke2fs /dev/mapper/${extHOST}
else
	sudo losetup /dev/${loopDevice} /tmp/${extHOST}_hd/A/${extImgName}.img

	gpg -d key_${extHOST}_${extImgName}.txt.gpg > key_${extHOST}_${extImgName}.txt
	sudo cryptsetup luksOpen /dev/${loopDevice} ${extHOST} < key_${extHOST}_${extImgName}.txt
fi

shred key_${extHOST}_${extImgName}.txt
rm key_${extHOST}_${extImgName}.txt

sudo mkdir -p /tmp/${extHOST}_hd/C
sudo chmod 700 /tmp/${extHOST}_hd/C
sudo mount /dev/mapper/${extHOST} /tmp/${extHOST}_hd/C
echo "the locally encrypted remote storage is now mounted at /tmp/${extHOST}_hd/C"

<commit_msg>ignoring gpg error for the case that the first smartcard tried is not present, but the second succeeds.<commit_after>#! /bin/bash
# make a backup of my home directory on the raspberripi at pcextreme in the Netherlands box by pushing the git repo.
# Ideas from:
# http://www.linuxquestions.org/questions/linux-newbie-8/remote-encrypted-unattended-file-server-650280/
# http://www.vectorspace.dk/2009/06/encrypted-partition-on-debian-5-0-with-secret-key-on-usb-stick/
# http://www.vidarholen.net/contents/blog/?p=8

set -e

# the following environment variables must be exported prior to calling this script:
#extHOST=
#extPath=
#extImgName=
#extUser=
#imageSize=
#loopDevice=

mkdir -p /tmp/${extHOST}_hd/A
chmod 750 /tmp/${extHOST}_hd/A
sshfs ${extUser}@${extHOST}:${extPath} /tmp/${extHOST}_hd/A -o allow_root

if [ ! -e /tmp/${extHOST}_hd/A/${extImgName}.img ]; then
	# prepare the disk image on the remote machine
	ssh ${extUser}@${extHOST} "(dd if=/dev/urandom of=${extPath}/${extImgName}.img bs=1M count=${imageSize}; chmod 700 ${extPath}/${extImgName}.img)"
	sudo modprobe dm-crypt
#	sudo modprobe aes
#	sudo modprobe sha256
	# prepare a key file and encrypt it
	dd if=/dev/urandom | tr -d '\n' | dd bs=1 count=64 of=key_${extHOST}_${extImgName}.txt
	gpg -e key_${extHOST}_${extImgName}.txt
	git add key_${extHOST}_${extImgName}.txt.gpg
	sudo cryptsetup -c aes-xts-plain -s 512 luksFormat /tmp/${extHOST}_hd/A/${extImgName}.img key_${extHOST}_${extImgName}.txt
	
	sudo losetup /dev/${loopDevice} /tmp/${extHOST}_hd/A/${extImgName}.img

	sudo cryptsetup luksOpen /dev/${loopDevice} ${extHOST} < key_${extHOST}_${extImgName}.txt

	sudo mke2fs /dev/mapper/${extHOST}
else
	sudo losetup /dev/${loopDevice} /tmp/${extHOST}_hd/A/${extImgName}.img

	gpg -d key_${extHOST}_${extImgName}.txt.gpg > key_${extHOST}_${extImgName}.txt || true
    sleep 1
	sudo cryptsetup luksOpen /dev/${loopDevice} ${extHOST} < key_${extHOST}_${extImgName}.txt
fi

shred -u key_${extHOST}_${extImgName}.txt

sudo mkdir -p /tmp/${extHOST}_hd/C
sudo chmod 700 /tmp/${extHOST}_hd/C
sudo mount /dev/mapper/${extHOST} /tmp/${extHOST}_hd/C
echo "the locally encrypted remote storage is now mounted at /tmp/${extHOST}_hd/C"


{'fraction_non_alphanumeric': 0.16407879490150637, 'fraction_numerical': 0.016917728852838935, 'mean_word_length': 4.880108991825613, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7378471', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1606, 'n_words': 373}