2

I have Ubuntu 18.04.3 on a mechanical 500 GB drive. I have added an SSD. I installed Ubuntu 18.04.3 on the SSD. The SSD is sda and the mechanical disk is sdb. Both disks have the same number of sectors.

I have more than a year of changes to the system installed on the mechanical disk.

I want to duplicate sdb on to sda. Is this possible? If I boot sdb, will sudo dd if=/dev/sdb of=/dev/sda bs=2G work?

Is there a simpler way to duplicate the installation? I mostly want the apt and python packages to be the same.

sudo fdisk -l /dev/sd[ab]
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x87f0c3fe
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1          409600   2506751   2097152     1G 83 Linux
/dev/sda2  *      2506752 212221951 209715200   100G 83 Linux
/dev/sda3       212221952 279330815  67108864    32G 82 Linux swap / Solaris
/dev/sda4       279330816 976773119 697442304 332.6G 83 Linux

Disk /dev/sdb: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x49598911
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sdb1            2048    976656    974609 475.9M  b W95 FAT32
/dev/sdb2  *       976896 352538623 351561728 167.7G 83 Linux
/dev/sdb3       352538624 391600127  39061504  18.6G 82 Linux swap / Solaris
/dev/sdb4       391600128 976771071 585170944   279G 83 Linux
2
  • Should be as simple as restoring from your backup. Backup should let you do a new install & restore list of installed apps & /home. And perhap some settings in /etc. Only if server apps (Web, database, etc) installed may you also need those from /. askubuntu.com/questions/545655/… & askubuntu.com/questions/569679/… If you do an image copy, you cannot reboot with both drives connected as duplicate UUIDs not allowed.
    – oldfred
    Aug 25, 2019 at 3:31
  • Thanks. Try that tomorrow. Aug 25, 2019 at 7:44

1 Answer 1

3

The answer was in this Stack Overflow question by starry-eyed-sysadmin.

How to use dump and restore to 'clone' a Linux OS drive

You can find plenty of info online about using dd to clone OS drives.

Don't listen! It's MUCH faster to use dump and restore because you only copy the data, rather than copying all the blocks (empty or not).

Part 1: Know where your source and destination drives are in the device list

lsscsi | grep sd*

will show you a list of scsi devices and their associated letters. If you are lucky enough to be working with a hot-swapable box, then you can simply run that command before and then after you insert the drive--the newest device to show up is, of course, the drive you just inserted.

Part 2: Prepare to dump

  1. Safety tip: assign variables for the destination and source devices. (Also, if you are doing this more than once, variablization allows you to reuse the commands.)

    SOURCE=/dev/sdx DEST=/dev/sdy
    
  2. A note on the source drive (the one you are copying from). If you are copying the current OS drive, it will (duh) already be mounted. If you are copying a different drive, it actually doesn't need to be mounted.

  3. Copy the partition table of your source drive out to a file:

    sfdisk -d $SOURCE > part_table
    
  4. Copy the part_table already stored in a file

    sfdisk --force $DEST < part_table
    
  5. Zero out the boot sector:

    dd if=/dev/zero of=${DEST}1 bs=512 count=1
    
  6. Make your filesystem (one partition at a time):

    mkfs -t ext4 ${DEST}1
    mkswap ${DEST}2
    
  7. Take a look:

    parted $DEST --script print
    
  8. Copy the label of all non-swap partitions. Example:

    tune2fs -L "/" /${DEST}1
    

Part 3: The dump | restore moment

  1. Make a directory to mount the destination device of the dump | restore. (As mentioned above, source device need not be mounted.)

    mkdir -p /mnt/${DEST}1
    
  2. Mount the destination device:

    mount -t ext4 ${DEST}1 /mnt/${DEST}1
    
  3. cd into the mount point:

    cd /mnt/${DEST}1
    
  4. Dump and restore:

    dump -a0f - /dev/${SOURCE}1 | restore -rf -
    

    (dump flags: a=autosize; 0 (zero)= start at block 0; f = file, - = stdout; restore flags: r=rebuild; f=file; - = stdout)

  5. The dump | restore should take just a few minutes.

Part 4: use grub to install a boot loader onto the cloned drive

  1. Assuming you are copying an OS drive (i.e., a drive off which you boot a box), you need to install a boot loader.

  2. Grub identifies disks as hd#, starting from 0 (NOT 1). The correspondence is easy: /dev/sda = hd0, /dev/sdb = hd1, etc.

     [root@drive-toaster /]# grub
    
     grub> root (hd1,0)  # use correct number for your disk!  
     root (hd1,0)   
     Filesystem type is ext2fs, partition type 0x83
    
     grub> setup (hd1) # use correct number for your disk!  
     setup (hd1)   
     Checking if "/boot/grub/stage1" exists... yes
     Checking if "/boot/grub/stage2" exists... yes
     Checking if "/boot/grub/e2fs_stage1_5" exists... yes
     Running "embed /boot/grub/e2fs_stage1_5 (hd1)"...  27 sectors are embedded.
     succeeded
     Running "install /boot/grub/stage1 (hd1) (hd1)1+27 p
     (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
     Done.
    
     grub> quit
    
1
  • This resulted in a not-bootable disk. After an hour or so, I installed from the iso image. Now, I have to dup the package installs. I know about apt-clone. That may work. But, my experience to date is the manual method works better. Aug 26, 2019 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .