Working with the following conditions:
- The original disk to copy is
/dev/sdx
- The original disk is properly partitioned/labeled/flagged
- The filesystem contents of the original disk will be ignored
- The destination disk, to copy to, will be
/dev/sdy
- The swap partition will be
/dev/sdy4
- The boot partition will be
/dev/sdy1 mounted on /boot in the final system with ext3 filesystem
- The root partition will be
/dev/sdy2 mounted on / in the final system with ext4 filesystem
- The users partition will be
/dev/sdy3 mounted on /home in the final system with ext4 filesystem
- The Debian system you want to copy has been tarred and gzipped to
master_system.tar.gz
- All files, including the script, will be stored in the working directory
- The script will be executed from the same working directory
- The script will be run as
root, not sudo but either log in as root, or su in a terminal
- There is a directory
dupe_mnt in the working directory
To "copy" the original disk's partition structure, only needed one time, unless the structure is changed.
sfdisk --dump /dev/sdx > master_table
Since only you, at the system in question can determine what to copy from the Debian system that serves as the master, I'm not going to go into any of that. I know you won't copy the /proc, /dev/, and /sys directories, but there are sure to be others to exclude. Create that archive any way you choose, and name it master_system.tar.gz. This should include the /boot and /home directories in it. That's it, setup is done until you change either the Debian system you're copying, or the partitioning of the disk.
The script to create, called sys_replicate.sh is:
#!/bin/sh
target=$1;
sfdisk /dev/${target} < master_table;
# Format the swap partition
mkswap /dev/${target}4;
# Format the data partitions
mkfs.ext3 /dev/${target}1;
mkfs.ext4 /dev/${target}2;
mkfs.ext4 /dev/${target}3;
# Mount the target root filesystem and its parts
mount /dev/${target}2 dupe_mnt;
mount /dev/${target}1 dupe_mnt/boot;
mount /dev/${target}3 dupe_mnt/home;
# Copy the master system to the target
cd dupe_mnt;
tar -zxvpf ../master_system.tar.gz;
cd ..;
# Unmount the new system
cd ..
umount /dev/${target}3;
umount /dev/${target}1;
umount /dev/${target}2;
#done
The file sys_replicate.sh needs to have the execute bit set. chmod +x sys_replicate.sh
To use the process, once setup, connect the target disk. If it's a USB, make sure the system has recognized that it's available. If it's an internal HDD, obviously it'll require a reboot, and the system should find it automatically. Once connected, be VERY sure you know which /dev it is, since adding disks can rearrange the letters. Once it's ready, in a root shell, execute:
./sys_replicate.sh sdy
The device name /dev/sdy and /dev/sdx obviously will need to be changed to match your operational system. Also, as it turns out, sfdisk can handle GPT disks and extended partitions, so my earlier comment question was not needed. If you use a disk, as the copy, that is larger than the original, everything will still work. You will have wasted space that you can't easily reclaim, however, so take that into account before selecting the master disk to copy.
The creation of the partition structure, and the copying of the Debian system are independent, so changes to one do not require updating the other.