2016-09-15 09:42:10 +02:00
|
|
|
|
#!/bin/bash
|
2016-09-15 14:26:10 +02:00
|
|
|
|
# Script to copy data on usb keys
|
2016-09-15 09:42:10 +02:00
|
|
|
|
|
2016-09-15 14:26:10 +02:00
|
|
|
|
# dependencies : aptitude install ncurses-bin parted dosfstools
|
|
|
|
|
# mlabel is now useless
|
2016-09-15 09:42:10 +02:00
|
|
|
|
|
|
|
|
|
# text color in bash
|
|
|
|
|
normal=$(tput sgr0)
|
|
|
|
|
red=$(tput setaf 1)
|
|
|
|
|
green=$(tput setaf 2)
|
|
|
|
|
|
|
|
|
|
# dependencies check
|
|
|
|
|
i=0
|
|
|
|
|
for arg in "tput" "parted" "mkdosfs"; do
|
2016-09-15 14:26:10 +02:00
|
|
|
|
hash "${arg}"
|
|
|
|
|
if [[ $? -gt 0 ]]; then
|
2016-09-15 14:30:05 +02:00
|
|
|
|
printf "%40s\n" "${red}Error : Could not find \"$arg\" application.${normal}";
|
2016-09-15 14:26:10 +02:00
|
|
|
|
i=$((i+1))
|
|
|
|
|
fi
|
2016-09-15 09:42:10 +02:00
|
|
|
|
done
|
|
|
|
|
[[ $i -eq 0 ]] || exit $i
|
|
|
|
|
|
|
|
|
|
# this part depends of the event
|
2016-09-15 11:16:33 +02:00
|
|
|
|
keylabel="CHOCOLAS" # label of the keys
|
|
|
|
|
wherethefilesare="/ipr/home.ipr/jegardai/chocolas" # where are the files you need to copy
|
2016-09-15 09:42:10 +02:00
|
|
|
|
badkeyid="abcd" # depends of key models (lsusb)
|
|
|
|
|
goodkeyid="058f"
|
|
|
|
|
|
|
|
|
|
# all our usb keys have the same ID, we decided to write only on these keys
|
|
|
|
|
keysearchbadid=$(lsusb | grep $badkeyid | wc -l)
|
|
|
|
|
keysearchgoodid=$(lsusb | grep $goodkeyid | wc -l)
|
|
|
|
|
keysearchid=$(($keysearchbadid+$keysearchgoodid))
|
2016-09-15 11:16:33 +02:00
|
|
|
|
mountpath="/mnt/usbkey"
|
|
|
|
|
|
|
|
|
|
# MD5SUM check
|
|
|
|
|
sourcemd5="/tmp/source_directory.md5"
|
|
|
|
|
destmd5="/tmp/destination_directory.md5"
|
2016-09-15 09:42:10 +02:00
|
|
|
|
|
|
|
|
|
case $keysearchid in
|
2016-09-15 14:30:05 +02:00
|
|
|
|
0)
|
|
|
|
|
printf "%40s\n" "${red}Alert : No usb key detected.${normal}"
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
|
2016-09-15 09:42:10 +02:00
|
|
|
|
1|2) # we only have 1 or 2 usb ports
|
2016-09-15 14:30:05 +02:00
|
|
|
|
printf "%40s\n" "${green}At least one key detected : Here we go${normal}"
|
|
|
|
|
# number of device (except sda)
|
2016-09-15 09:42:10 +02:00
|
|
|
|
nbsd=$(ls /dev/sd* | grep -v sda | grep -v 1 | grep -v 2 | grep -v 3 | wc -l)
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
|
|
|
|
# if $(lsusb) and $(ls /dev/sd*) doesn't give the same number
|
2016-09-15 09:42:10 +02:00
|
|
|
|
if [[ $keysearchid != $nbsd ]]; then
|
2016-09-15 14:30:05 +02:00
|
|
|
|
printf "%40s\n" "${red}Alert : Error 1${normal} Keysearchid : ${keysearchid} and Numberofdevice : ${nbsd}"
|
|
|
|
|
exit 1
|
2016-09-15 09:42:10 +02:00
|
|
|
|
else # if everything is ok
|
2016-09-15 11:16:33 +02:00
|
|
|
|
# Generate the md5sum for source directory
|
|
|
|
|
pushd "${wherethefilesare}"
|
|
|
|
|
find . -type f -exec md5sum {} \; | sort > "${sourcemd5}"
|
|
|
|
|
popd
|
|
|
|
|
|
2016-09-15 09:42:10 +02:00
|
|
|
|
for i in $(seq 1 $keysearchid) # from 1 to $keysearchid by step of 1
|
2016-09-15 14:30:05 +02:00
|
|
|
|
# if $keysearchid=1 : for i in 1
|
|
|
|
|
# if $keysearchid=2 : for i in 1 2
|
2016-09-15 09:42:10 +02:00
|
|
|
|
do
|
2016-09-15 14:26:10 +02:00
|
|
|
|
[[ $i == 1 ]] && device="sdb" || device="sdc" # if 3 or more keys this line has to be adapt
|
|
|
|
|
parted -s /dev/"${device}" mklabel msdos
|
|
|
|
|
parted /dev/"${device}" mkpart -a optimal primary fat32 0% 100%
|
|
|
|
|
mkdosfs -F 32 -I -n $keylabel /dev/"${device}"1
|
|
|
|
|
# fatlabel /dev/${device}1 $keylabel # Cette commande est remplacée par l’option -n de la commande précédente
|
|
|
|
|
mkdir "${mountpath}""${device}"
|
|
|
|
|
mount /dev/"${device}"1 "${mountpath}""${device}"
|
|
|
|
|
cp -r $wherethefilesare/* "${mountpath}""${device}"/
|
2016-09-15 09:42:10 +02:00
|
|
|
|
sync
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
2016-09-15 11:16:33 +02:00
|
|
|
|
# Generate the md5sum for the mountpoint
|
2016-09-15 14:26:10 +02:00
|
|
|
|
pushd "${mountpath}""${device}"
|
2016-09-15 11:16:33 +02:00
|
|
|
|
find . -type f -exec md5sum {} \; | sort > "${destmd5}"
|
|
|
|
|
popd
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
2016-09-15 11:16:33 +02:00
|
|
|
|
# Umount the key
|
2016-09-15 14:26:10 +02:00
|
|
|
|
umount "${mountpath}""${device}"
|
|
|
|
|
rmdir "${mountpath}""${device}"
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
2016-09-15 11:16:33 +02:00
|
|
|
|
# Compare the md5sum
|
|
|
|
|
diff -q "${sourcemd5}" "${destmd5}"
|
|
|
|
|
if [[ $? -gt 0 ]]; then
|
2016-09-15 14:30:05 +02:00
|
|
|
|
printf "%40s\n" "${red}Error : MD5 diff detected.${normal}";
|
|
|
|
|
exit 2
|
2016-09-15 11:16:33 +02:00
|
|
|
|
else
|
2016-09-15 14:26:10 +02:00
|
|
|
|
printf "%40s\n" "${green}MD5 check OK for ${device}.${normal}"
|
2016-09-15 11:16:33 +02:00
|
|
|
|
fi
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
2016-09-15 09:42:10 +02:00
|
|
|
|
done
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
2016-09-15 09:42:10 +02:00
|
|
|
|
printf "%40s\n" "${green}DONE${normal}"
|
|
|
|
|
fi ;;
|
2016-09-15 14:30:05 +02:00
|
|
|
|
|
|
|
|
|
*)
|
|
|
|
|
printf "%40s\n" "${red}Alert : Error in the number of usb key detected.${normal}"
|
|
|
|
|
exit 3
|
|
|
|
|
;;
|
2016-09-15 09:42:10 +02:00
|
|
|
|
esac
|
|
|
|
|
|
2016-09-15 11:16:33 +02:00
|
|
|
|
exit 0
|