104 lines
3.1 KiB
Bash
Executable File
104 lines
3.1 KiB
Bash
Executable File
#!/bin/bash
|
||
# Script to copy data on usb keys
|
||
|
||
# dependencies : aptitude install ncurses-bin parted dosfstools
|
||
# mlabel is now useless
|
||
|
||
# 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
|
||
hash "${arg}"
|
||
if [[ $? -gt 0 ]]; then
|
||
printf "%40s\n" "${red}Error : Could not find \"$arg\" application.${normal}";
|
||
i=$((i+1))
|
||
fi
|
||
done
|
||
[[ $i -eq 0 ]] || exit $i
|
||
|
||
# this part depends of the event
|
||
keylabel="CHOCOLAS" # label of the keys
|
||
wherethefilesare="/ipr/home.ipr/jegardai/chocolas" # where are the files you need to copy
|
||
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))
|
||
mountpath="/mnt/usbkey"
|
||
|
||
# MD5SUM check
|
||
sourcemd5="/tmp/source_directory.md5"
|
||
destmd5="/tmp/destination_directory.md5"
|
||
|
||
case $keysearchid in
|
||
0)
|
||
printf "%40s\n" "${red}Alert : No usb key detected.${normal}"
|
||
exit 1
|
||
;;
|
||
|
||
1|2) # we only have 1 or 2 usb ports
|
||
printf "%40s\n" "${green}At least one key detected : Here we go${normal}"
|
||
# number of device (except sda)
|
||
nbsd=$(ls /dev/sd* | grep -v sda | grep -v 1 | grep -v 2 | grep -v 3 | wc -l)
|
||
|
||
# if $(lsusb) and $(ls /dev/sd*) doesn't give the same number
|
||
if [[ $keysearchid != $nbsd ]]; then
|
||
printf "%40s\n" "${red}Alert : Error 1${normal} Keysearchid : ${keysearchid} and Numberofdevice : ${nbsd}"
|
||
exit 1
|
||
else # if everything is ok
|
||
# Generate the md5sum for source directory
|
||
pushd "${wherethefilesare}"
|
||
find . -type f -exec md5sum {} \; | sort > "${sourcemd5}"
|
||
popd
|
||
|
||
for i in $(seq 1 $keysearchid) # from 1 to $keysearchid by step of 1
|
||
# if $keysearchid=1 : for i in 1
|
||
# if $keysearchid=2 : for i in 1 2
|
||
do
|
||
[[ $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}"/
|
||
sync
|
||
|
||
# Generate the md5sum for the mountpoint
|
||
pushd "${mountpath}""${device}"
|
||
find . -type f -exec md5sum {} \; | sort > "${destmd5}"
|
||
popd
|
||
|
||
# Umount the key
|
||
umount "${mountpath}""${device}"
|
||
rmdir "${mountpath}""${device}"
|
||
|
||
# Compare the md5sum
|
||
diff -q "${sourcemd5}" "${destmd5}"
|
||
if [[ $? -gt 0 ]]; then
|
||
printf "%40s\n" "${red}Error : MD5 diff detected.${normal}";
|
||
exit 2
|
||
else
|
||
printf "%40s\n" "${green}MD5 check OK for ${device}.${normal}"
|
||
fi
|
||
|
||
done
|
||
|
||
printf "%40s\n" "${green}DONE${normal}"
|
||
fi ;;
|
||
|
||
*)
|
||
printf "%40s\n" "${red}Alert : Error in the number of usb key detected.${normal}"
|
||
exit 3
|
||
;;
|
||
esac
|
||
|
||
exit 0
|