#!/bin/sh
#
# mkimage.sh - make an tar archive image of a disk
#
# Author: Niek Linnenbank
#

if [ $# -lt 1 ]; then
    echo "usage: mkimage.sh <target> [ <crypto key> ]"
    exit
fi

if [ ! -e $1 ]; then
    echo "$1 does not exist!"
    exit
fi

volume="$1"
sedcmd="sed s/\//_/g"
cipher="aes-256-ecb"

filename="`hostname``echo $volume|$sedcmd``date +%d_%b_%Y_%H_%M_%S`.tar.gz"

if [ -n "$2" ] ; then
	filename="$filename"".""$cipher"
fi

echo "[*] performing backup of $1"
echo "[*] creating archive $filename ..."

# tar			- the gnu archiver
# c			- create
# l, --one-file-system	- stay in local filesystem
# S			- handle sparse files effeciently
# p			- same permissions
# M			- multi volume (not used yet)
# z			- gzip the archive
# f			- use archive file
# v			- verbose

if [ -n "$2" ] ; then
	tar -cSpzf - --one-file-system $volume | openssl $cipher -k "$2" | dd of=$filename
else
	tar -cSpzf $filename --one-file-system $volume
fi

echo "[*] backup saved in $filename"

