Skip to content

Instantly share code, notes, and snippets.

@jrh-spg
Last active November 17, 2021 03:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrh-spg/87aa0472c431be62d386ece080bda1ec to your computer and use it in GitHub Desktop.
Save jrh-spg/87aa0472c431be62d386ece080bda1ec to your computer and use it in GitHub Desktop.
Disk Cloner Script that uses dd to clone disks
#!/bin/bash
# https://asciinema.org/a/6LZefsbvLjgsuwQHOnQsBEaSj
IF=notset
OF=notset
BS=128M
CURDIR=$(dirname $(realpath -s $0))
SCRIPT=`basename "$0"`
if [[ ! -f /usr/bin/pv ]]; then
echo "pv is not installed. Please install pv and try again"
exit 1
fi
if [[ ! -f /usr/bin/whiptail ]]; then
echo "whiptail is not installed. Please install newt and try again"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
options=':i:o:b:'
while getopts $options option
do
case $option in
i) IF=$OPTARG;;
o) OF=$OPTARG;;
b) BS=$OPTARG;;
*) echo "Unknown option"; exit;;
esac
done
if [ "$IF" == "notset" ]; then
echo "Input file is not set. Bailing out!"
exit
fi
if [ "$OF" == "notset" ]; then
echo "Output file is not set. Bailing out!"
exit
fi
test_mount() {
clear
if [[ ! -d /tmp/test-mount ]]
then
mkdir /tmp/test-mount
fi
mount $OF /tmp/test-mount||exit 1
whiptail --title "$SCRIPT" --msgbox "$OF Mounted!" 8 78
}
test_write() {
touch /tmp/test-mount/.test
if [[ -f /tmp/test-mount/.test ]]
then
whiptail --title "$SCRIPT" --msgbox "Successfully wrote file." 8 78
test_umount
else
whiptail --title "$SCRIPT" --msgbox "Could not complete test write!" 8 78
exit 1
fi
}
test_umount() {
rm /tmp/test-mount/.test
umount /tmp/test-mount
whiptail --title "$SCRIPT" --msgbox "Disk Image Unmounted." 8 78
whiptail --title "$SCRIPT" --msgbox "Disk Clone Validated." 8 78
whiptail --title "$SCRIPT" --msgbox "Image created at $CURDIR/$OF" 8 78
}
(pv -n $IF | dd of=$OF bs=$BS conv=notrunc,noerror) 2>&1 | whiptail --gauge "Cloning $IF to $OF..." 10 70 0
if (whiptail --title "$SCRIPT" --yesno "Would you like to validate $OF" 8 78); then
test_mount
test_write
else
whiptail --title "$SCRIPT" --msgbox "Image created at $CURDIR/$OF" 8 78
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment