Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. omarmciver created this gist Jul 18, 2022.
    30 changes: 30 additions & 0 deletions Replicate Linux disk to new disk - single partition
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # 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