Skip to content

Instantly share code, notes, and snippets.

@bumbummen99
Last active January 28, 2023 12:20
Show Gist options
  • Save bumbummen99/f129607ab6b8737ccea059b16ceafdff to your computer and use it in GitHub Desktop.
Save bumbummen99/f129607ab6b8737ccea059b16ceafdff to your computer and use it in GitHub Desktop.
Simple bash script to clone disks/drives FAST with visual feedback such as size of copied data, elapsed time, current speed, a progress bar, progress percentage, and ETA.
#!/usr/bin/env bash
# Simple script to copy disks FAST using pv. It does include
# rudimentary error checking as well as a progress bar
# and pretty colored outputs.
#
# WARNING:
# I am not responsible for any data loss or damage
# to your system and hardware, use at your own risk!
#
# Author: Patrick Henninger <[email protected]>
# License: GPLv3
# Examples:
# ./clone-disk.sh SOURCE DESTINATION
# ...
#
# ./clone-disk.sh sda sdb
# ...
#
# Statics
NC="\e[0m"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
DARK_GRAY='\e[90m'
LIGHT_GRAY="\033[0;37m"
# Configure
SOURCE="$1"
DESTINATION="$2"
PARAMETERS="conv=sync,noerror,notrunc iflag=fullblock status=progress"
COMMAND="sudo dd if=/dev/$SOURCE of=/dev/$DESTINATION $PARAMETERS"
QUESTION=$(echo -e "${YELLOW} Do you really want to continue? (y/n)")
# Verify SOURCE exists
if [ -z "$SOURCE" ]; then
echo -e "${RED}Source is not defined!${NC}"
exit 1
fi
if ! test -e "/dev/$SOURCE"; then
echo -e "${RED}Source \"/dev/$SOURCE\" does not exist."
exit 1
fi
# Verify DESTINATION exists
if [ -z "$DESTINATION" ]; then
echo -e "${RED}Destination is not defined!${NC}"
exit 1
fi
if ! test -e "/dev/$DESTINATION"; then
echo -e "${RED}Destination \"/dev/$DESTINATION\" does not exist."
exit 1
fi
# Verify required software is installed
if ! command -v pv &> /dev/null
then
echo -e "${RED}pv could not be found! Please install it using ${NC}\"sudo apt-get -y install pv${RED}\""
exit
fi
# Ask for final permission to do potentially harmful operations
echo -e "${NC}Will attempt to clone: "
echo -e "${DARK_GRAY}/dev/${LIGHT_GRAY}$SOURCE ${NC}=> ${DARK_GRAY}/dev/${LIGHT_GRAY}$DESTINATION"
read -p "$QUESTION " -n 1 -r
# move to a new line
echo
# Only proceed with dangerous opertions if the user permitted it
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${NC}Starting cloning process..."
pv /dev/$SOURCE > /dev/$DESTINATION
echo -e "${GREEN}Success! Cloned \"/dev/$SOURCE\" to \"/dev/$DESTINATION\"."
else
echo -e "${YELLOW}Cancelled operation."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment