#!/bin/bash

# 45Drives simple external copy script Mitchell Hall 2021
# This script sets all copied datas permissions to ADMIN:IC1 in the function below. This must be changed to the permissions you'd like for your environment if they are different.


function finish {
        chmod -R 770 $poolvar/$foldervar
        chown -R ADMIN:IC1 $poolvar/$foldervar
        sleep 2
        umount /mnt/external_$(date +%F)
        sleep 2
        rmdir /mnt/external_$(date +%F)
        echo -e "\e[1;31m Permissions set! \e[0m"
}


logfile="/tmp/rsynctasks.tmp" # If you want a new path for your logfile, change this variable

echo -e  "\e[1;31m Here is a list of USB drives currently plugged in with all available partitions. \e[0m"

lsblk -oname,tran,size,kname -np | grep usb > /tmp/usbdisks.txt

awk '{ print $1 }' /tmp/usbdisks.txt > /tmp/devname.txt

: > /tmp/output.txt

while IFS= read -r LINE; do
        lsblk -oname,tran,size,kname -np  "$LINE" >> /tmp/output.txt
done < /tmp/devname.txt

cat /tmp/output.txt

echo -e "\e[1;31m Here is a list of currently mounted ZFS storage arrays \e[0m"
zfs list | perl -p -e 's/(\S+)$/\e[1;32m\1\e[0m/g' # lists the zfs datasets they have and where they are mounted

read -p ' Enter the path of the USB disk you want to copy (format is /dev/sd* replace * with drive letter and partition): ' diskvar # asks the user to input the sd name of the disk. The user should input /dev/sd* where star is replaced by the correct disk path

read -p 'Enter the path of your storage pool you want to copy it to: ' poolvar   # asks the user which ZFS dataset they want to mount to. To find this, the script outputs zfs list to help the user.

read -p 'Enter the name of the folder you want this copy to be stored in: ' foldervar # asks the user where they want to store their copied data


mkdir /mnt/external_$(date +%F)                 # creates a directory to map the disk drive

if [[ $(blkid -o value -s TYPE $diskvar | grep 'apfs') = *apfs* ]]; then
        fsapfsmount $diskvar /mnt/external_$(date +%F)

else
        mount $diskvar /mnt/external_$(date +%F)        # mounts the disk according to the disk variable given by the user
        
fi || exit 1

trap finish EXIT

ls -1 /mnt/external_$(date +%F)/ | xargs -I {} -P 4 -n 1 rsync -avh /mnt/external_$(date +%F)/{} $poolvar/$foldervar/ --log-file=${logfile} --progress && complete=1 ;  # multi-threaded RSYNC job


# For single threaded RSYNC job, comment out the above RSYNC job, and uncomment the one below

#rsync /mnt/external_$(date +%F)/ -avh  $poolvar/$foldervar/ --log-file=${logfile};  # Runs an RSYNC task that copies all data from the external drive to to the ZFS dataset inside a folder named copy_job appended with todays date/time.

if  [ $complete -eq 1 ]; then
        echo -e "\e[1;31m Rsync completed successfully - files copied to destination. The external drive is being unmounted as well. \e[0m"
        echo -e "\e[1;31m Check /tmp/rsynctasks.tmp to get a detailed overview of the copy job \e[0m"
        echo -e "\e[1;31m Please wait while permissions are set... \e[0m"
fi

