November 17, 2019

OpenBSD: Backup script

I use four external encrypted USB drives to backup my laptop. This is the script I wrote to do it. If you find it useful, use it.

#!/bin/sh

# This script mounts an external USB drive which is encrypted as RAID
# and then backs up some folders from the laptop to it using rsync
# excluding "junk" files.

# Since the drive is encrypted, we need to pass as an argument
# the uid of the drive, which is unique. My drives have different
# names on a sticker.
#
# I am assuming that:
#
# (1) the drive is mounted in /mnt/BackupDrive
# (2) it contains a single RAID partition, named "a"
# (3) after decrypt there's a single partition, named "i"
# (4) your doas config file contains
# permit nopass :pau cmd mount
# permit nopass :pau cmd umount
# permit nopass :pau cmd fsck
# permit nopass pau as root cmd bioctl args -c C -l
# permit nopass pau as root cmd bioctl args -d
#
# Pau Amaro Seoane, 16 Nov 2019, Berlin

# ******************** Mount the external drive ******************** #

# Ask for the name of the drive

echo ""
echo "What is the name of this drive?"
echo ""

select name in Seagate WhiteDwarf_01 WhiteDwarf_02 WhiteDwarf_03 ;
do
case "$name" in
Seagate)
UID=382e0c1adfc0761b
CRYPTUID=289dd3a5b330a65a
break
;;
WhiteDwarf_01)
UID=9ec25154fac1302c
CRYPTUID=24165dff8ccbcdc8
break
;;
WhiteDwarf_02)
UID=a9b9006805be6c32
CRYPTUID=b963e0804a939ff6
break
;;
WhiteDwarf_03)
UID=aa76aab583e0206c
CRYPTUID=b4dd118ba87fa5fe
break
;;
esac
done

# Mount it, and do some sanity checks

# (1) Make sure /mnt/BackupDrive exists

if [ -d "/mnt/BackupDrive" ]; then
echo "/mnt/BackupDrive exists, we are good to go."
else
echo "Folder /mnt/BackupDrive does not exist, creating it now."
doas mkdir /mnt/BackupDrive
fi

# Mount softraid

echo ""
echo "bioctl'ing now... enter the passphrase..."
doas bioctl -c C -l $UID.a softraid0

if [[ $? -ne 0 ]] ; then
echo "Wrong password, script terminates now."
exit 1
fi

# (2) Run fsck first

echo ""
echo "Running fsck..."
doas fsck $CRYPTUID.i

# Mount it

doas mount $CRYPTUID.i /mnt/BackupDrive

echo ""
echo "Drive mounted on /mnt/BackupDrive, backup'ing now..."
sleep 2

# ******************** backup folders ******************** #

# First define our rsync
# try first "--dry-run" after you modify this

myrsync="rsync \
--delete --archive --verbose --compress \
--human-readable --progress --times \
--exclude "*.o" --exclude "*.log" --exclude "*.dvi" \
--exclude "*.aux" --exclude "*.out" --exclude "*.blg" \
--exclude "*.tns" --exclude "*.toc" --exclude "*.nav" \
--exclude "*.bak" --exclude "*.swp" --exclude "*.swo" \
--exclude "*.tmp" --exclude "*.tui" --exclude "*.tuo" \
--exclude "*~" --exclude "Atlas" "

# backup all wireless passwords and rc.conf.local

$myrsync /etc/hostname.iwm0 $HOME/fitx_confg/connexions_sense_fil
$myrsync /etc/hostname.if $HOME/fitx_confg/connexions_sense_fil
$myrsync /etc/rc.conf.local $HOME/fitx_confg/conf_local

# Ask to backup sm and detector curves from the linux partition
# (I will not check that the partition is mounted, must rely on
# the user's intelligence sometimes)

echo ""
echo "Do you want to backup supermongo and the detector curves from the linux partition? (you should) "
read answer
case $answer in
yes|Yes|y|Y|S|s)
doas mount /dev/sd0j /mnt/ubuntu
$myrsync /mnt/ubuntu/home/pau/treball/documents/corbes_detectors/ \
/home/pau/treball/documents/

$myrsync /mnt/ubuntu/home/pau/treball/supermongoloideu/ \
/home/pau/treball/

$myrsync /mnt/ubuntu/home/pau/smbin \
/home/pau

umount /mnt/ubuntu
echo "SM backup'ed... Running rsync now..."
echo ""
sleep 2
;;
no|n|N)
echo ""
echo "Not backing up supermongo and the detector curves. Running rsync now..."
;;
esac

# backup all important folders to /mnt/BackupDrive

$myrsync \
$HOME/andromina \
$HOME/bin \
$HOME/smbin \
$HOME/escriptori \
$HOME/treball \
$HOME/correu \
$HOME/fitx_confg \
$HOME/temporal \
$HOME/ejcaip \
$HOME/include \
$HOME/lib \
$HOME/grafia \
/mnt/BackupDrive

# ******************** Unmount the drive and bioctl -d it as root ******************** #

echo ""
echo "rsync finished, unmounting folder..."
doas umount /mnt/BackupDrive

echo ""
echo "... unmounted... bioctl'ing -d now..."
doas bioctl -d $CRYPTUID

if [[ $? -ne 0 ]] ; then
echo "Wrong password, script terminates now."
exit 1
fi

# Goodbye
echo ""
echo "Laptop backup'ed, folder unmounted, drive encrypted... Aweeeeesome!"