#!/bin/bash
#Backupscript für SDCards
#made bei STP - tux-mania Support
#Licence GPL
#Date: 14.02.2017
#Important: not tested!
set -e
DEV=$1
COUNT=1

if [[ $(which test > /dev/null) == true ]]; then
	echo "Do yoe need test for this script!" 
	exit 1
fi

if [[ $USERNAME != root ]]; then 
	echo "You have no root access. Please logon as root and retry!" 
	exit 1
fi

if [[ $# -eq 0 ]]; then
	echo "Please add the SDCard Device the command line to restore the SDCard!" 
	exit 1
fi

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

if [ -d $PWD/backup ]; then
	echo "Backup directory exist. Please move or delete!" 
	exit 1
fi

mkdir backup
mkdir mnt
sfdisk -d $DEV > $PWD/backup/partitiontables

while [[ $COUNT -lt 3 ]]; do
  if [ ! -b $DEV$COUNT ]; then
    DEV=$DEV"p"
  fi
  mount $DEV$COUNT $PWD/mnt
  tar cvfz $PWD/backup/thinpi$COUNT.tar.gz  --directory="$PWD/mnt/" .
  umount $PWD/mnt
  let COUNT=COUNT+1
done

rm -rf $PWD/mnt

exit 0

