Tuesday, May 24, 2016

Wrote this shell script today for Ubuntu.

#!/bin/bash
#this script autobacks up encrypted and nonencrypted files to a USB flash drive.
#it assumes dirs exist
#and files filearrived and file2arrived exist on unencrypted and encrypted respectively
#and your USB is plugged into the first port
#personalized config:
#subsitute anybody for your username
#substitute encrypt64 for the folder you want your USB mounted at on your Home dir
#subsitute Documents for the folder you want encrypted
#substitute "files" for the folder you want unencrypted
#When you want to view your encrypted files run this command alone
# sudo mount -t ecryptfs  /home/anybody/encrypt64 /home/anybody/encrypt64

echo
read -p "is ecryptfs installed?" -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
sudo apt-get install ecryptfs-utils
fi
echo
echo "Headed to home dir"
cd
echo "making sure ecrypt is unmounted"
sudo umount /home/anybody/encrypt64
sudo umount /dev/sdb1
echo
echo "mounting usb"
sudo mount /dev/sdb1 /home/anybody/encrypt64
echo
echo "checking mount point"
df -h | grep sdb1
echo

echo
read -p "Copy unencrypted files?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo "copying unencrypted"
sudo cp -r files encrypt64
echo
echo "checking unencrypted arrived, user should check date modified"
ls -l --sort=time encrypt64/files | grep filearrived
echo
fi

read -p "Copy and encrypt files?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "starting encryption"
sudo mount -t ecryptfs  /home/anybody/encrypt64 /home/anybody/encrypt64
echo
echo "copying encrypted documents"
echo
sudo cp -fr Documents /home/anybody/encrypt64
echo "checking encrypted arrived, user should check date modified"
ls -l --sort=time /home/anybody/encrypt64/Documents | grep file2arrived
echo
echo "stopping encryption"
sudo umount /home/anybody/encrypt64
echo "checking if encrypted success, you should see random characters"
tail -n1 /home/anybody/encrypt64/Documents/file2arrived
echo
fi
date
who
echo
read -p "Run last check?(optional)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
sudo mount -t ecryptfs  /home/anybody/encrypt64 /home/anybody/encrypt64
cat encrypt64/Documents/file2arrived
sudo umount /home/anybody/encrypt64
echo "you should be able to read the file above"
fi
echo "ecryptfs has been unmounted."
echo
echo "operation complete"