#!/bin/bash
ls -al /dev/disk/by-id
read -p "Enter the destination drive to clone to (e.g. sdc): "
echo # Just to move to a new line
orig="/dev/sda"
dest="/dev/${REPLY}"
read -p "Clone system from ${orig} to ${dest}. Press Y to continue: "
echo # Just to move to a new line
if [[ ! "${REPLY}" =~ ^[Yy]$ ]]
then
exit 1
fi
read -p "Copy partition layout from ${orig} to ${dest} (Y/N)? "
echo "When using an already partitioned drive, press 'N'"
echo # Just to move to a new line
if [[ ! "${REPLY}" =~ ^[Nn]$ ]]
then
# Make partition layout
sfdisk -d "${orig}" > "/tmp/part_table"
sfdisk "${dest}" < "/tmp/part_table"
fi
# Grow, Clone, Shrink RAID devices
declare -A raidInfo
raidInfo[0]="1"
raidInfo[1]="2"
for i in "${!raidInfo[@]}"
do
echo "mdadm --grow --force '/dev/md${i}' --raid-devices=2"
mdadm --grow --force "/dev/md${i}" --raid-devices=2
sleep 10
echo "mdadm --zero-superblock '${dest}${raidInfo[$i]}'"
mdadm --zero-superblock "${dest}${raidInfo[$i]}"
sleep 10
echo "mdadm --manage '/dev/md${i}' --add --write-mostly '${dest}${raidInfo[$i]}'"
mdadm --manage "/dev/md${i}" --add --write-mostly "${dest}${raidInfo[$i]}"
sleep 10
echo "mdadm --wait '/dev/md${i}'"
mdadm --wait "/dev/md${i}"
sleep 10
sync
done
sleep 10
sync
sleep 10
# Install GRUB to dest
grub-install "${dest}"
sleep 10
for i in "${!raidInfo[@]}"
do
echo "mdadm '/dev/md${i}' --fail '${dest}${raidInfo[$i]}'"
mdadm "/dev/md${i}" --fail "${dest}${raidInfo[$i]}"
sleep 10
echo "mdadm '/dev/md${i}' --remove '${dest}${raidInfo[$i]}'"
mdadm "/dev/md${i}" --remove "${dest}${raidInfo[$i]}"
sleep 10
echo "mdadm --grow --force '/dev/md${i}' --raid-devices=1"
mdadm --grow --force "/dev/md${i}" --raid-devices=1
sleep 10
echo ""
echo "Finshed syncing /dev/md${i}"
echo ""
echo ""
echo ""
done