Skip to content

Instantly share code, notes, and snippets.

@scegg

scegg/sdcopy.sh

Last active Oct 23, 2020
Embed
What would you like to do?
Copy from SD cards to another USB disk. https://wp.secretnest.info/archives/12140
#!/bin/sh
# test and mount
function Test_n_Mount() {
sudo mkdir $MountPointTest
for SDDEV in /dev/sd?
do
for PARTITION in $SDDEV*
do
if [ "$PARTITION" != "$SDDEV" ]
then
if [ "$TargetFound" = false ] || [ "$SourceFound" = false ]
then
sudo mount $PARTITION $MountPointTest > /dev/null 2>&1
if [ "$TargetFound" = false ] && test -d "$MountPointTest$TargetFolder" > /dev/null 2>&1
then
sudo mount $PARTITION $MountPointTarget
TargetFound=true
echo "Target Device: $PARTITION"
elif [ "$SourceFound" = false ] && test -d "$MountPointTest$SourceTestFolder" > /dev/null 2>&1
then
sudo mount $PARTITION $MountPointSource
SourceFound=true
echo "Source Device: $PARTITION"
fi
sudo umount $MountPointTest > /dev/null 2>&1
fi
fi
done
done
sudo rm -fr $MountPointTest
}
# sub folder
function CreateSubFolder() {
if [ -z "$SubFolder" ]
then
# interactive
while [ "$SubFolderCreated" = false ]
do
echo -n "Please input a folder name and press [ENTER]: "
read TextInput
if [ -z "$TextInput" ]
then
break
fi
SubFolder="$MountPointTarget$TargetFolder/$TextInput"
if test -d $SubFolder > /dev/null 2>&1
then
AskYN "Folder exists. Resync?"
if [ "$AskYNResult" = true ]
then
SubFolderCreated=true
fi
else
AskYN "Folder doesn't exist. Create?"
if [ "$AskYNResult" = true ]
then
sudo mkdir $SubFolder
if test -d $SubFolder
then
SubFolderCreated=true
else
echo "Cannot create folder $SubFolder."
fi
fi
fi
done
else
# from argument
SubFolder="$MountPointTarget$TargetFolder/$SubFolder"
if test -d $SubFolder > /dev/null 2>&1
then
SubFolderCreated=true
elif test -e $SubFolder > /dev/null 2>&1
then
echo "Cannot create folder $SubFolder. A file with the same name exists."
else
sudo mkdir $SubFolder
if test -d $SubFolder
then
SubFolderCreated=true
else
echo "Cannot create folder $SubFolder."
fi
fi
fi
}
# copy
function CopyData() {
echo "Copy data to $SubFolder"
sudo rsync -ah --progress $MountPointSource $SubFolder
echo "done."
}
# AskYN
function AskYN() {
echo -n "$1 (Press Y to continue, or any other key to quit): "
read -n1 AskYNReply
echo ""
if [ "$AskYNReply" = "y" ] || [ "$AskYNReply" = "Y" ]
then
AskYNResult=true
else
AskYNResult=false
fi
}
# define
MountPointTarget="/tmp/MyTargetDisk"
MountPointSource="/tmp/MySourceDisk"
MountPointTest="/tmp/MyTestDisk"
TargetFolder="/Target"
SourceTestFolder="/DCIM"
# main
TargetFound=false
SourceFound=false
SubFolderCreated=false
SubFolder=$1
sudo mkdir $MountPointTarget
sudo mkdir $MountPointSource
Test_n_Mount
if [ "$TargetFound" = true ] && [ "$SourceFound" = true ]
then
CreateSubFolder
if [ "$SubFolderCreated" = true ]
then
CopyData
fi
else
echo "Error: Some device is not located."
fi
# final
sudo umount $MountPointTarget
sudo umount $MountPointSource
sudo rm -fr $MountPointTarget
sudo rm -fr $MountPointSource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.