Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omarmciver/596b110b0868b3f0ced847f59d2c8421 to your computer and use it in GitHub Desktop.
Save omarmciver/596b110b0868b3f0ced847f59d2c8421 to your computer and use it in GitHub Desktop.
Replicate Linux disk to new disk - single partition #bash
# Usage copydisk.sh <newdiskdev> <olddiskmountpoint> <temporary mountpoint>
# e.g. copydisk.sh /dev/sdc /datadisk /tmpdatadisk
echo New disk attached is at $1
echo Existing disk mount point is at $2
echo Temporary mount point for data copy is at $3
## Add partition table
parted $1 mklabel gpt
## Create partition ext4 100% of the available space
parted $1 mkpart primary ext4 0% 100%
## Format the ext4 partition
mkfs.ext4 $11
## Create a mount point
mkdir $3
## Mount the partition
mount $11 $3
## Copy the data from the old partition to the new partition
cp -p $2 $3
## Unmount the temporary partition
umount $3
## Unmount the old partition
umount $2
## Backup existing fstab
cp /etc/fstab /etc/fstab.bak
## Remove the old fstab partition
grep -v $2 /etc/fstab > tmpfile && mv tmpfile m/etc/fstab
## Add the new fstab entry
echo "$1 /$2 ext4 defaults 0 0" | sudo tee /etc/fstab -a
## Remove the temporary mount point
rmdir $3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment