Linux: Clone a Operating System disk, without blank space, allowing data to be restored later

0

I've got an SSD with a linux OS on it which I'm not currently using. The SSD usually sits in my desktop computer, but I won't be using it for a while.

I would like to use the SSD in my laptop for a while. Since they're pretty expensive still I don't really want to buy another while I have a perfectly good one here.

I thought about using dd to clone the disk so that I could restore it in the future if I need to get my desktop system working again. However it's 240 GB in size but only 5 GB is currently in use, so ideally I would like to be able to clone it without using all this space.

  • Would this be possible if I compressed the output iso image?

  • Can I just "copy" the files I need for later (including hidden ones?) using rsync? This will be only ~ 5 GB in size - but will I be able to restore these files correctly? eg; I might need to preserve permissions while doing this to prevent problems when I restore the files to the SSD?

  • Is there a better method?

The OS is either debian/ubuntu.

user3728501

Posted 2017-12-25T16:45:15.650

Reputation: 1 090

1

Possible duplicate of Clone only space in use from hard disk

– Kamil Maciorowski – 2017-12-25T16:57:05.833

1Clonezilla – anders – 2017-12-25T17:24:04.237

1Just using rsync (with --archive) is probably easiest. @AndersD Clonezilla's still basically a linux distribution with terminal menus? Would be nice to just use it's "workings" directly yourself, seems like it's a big wrapper for dd? – Xen2050 – 2017-12-25T23:05:13.523

And the possible Dup Q is about saving any disk's used space, this Q is basically about backing up a linux system disk, not a real duplicate – Xen2050 – 2017-12-25T23:15:54.870

Ofcourse it's nice to be able to do stuff directly on CLI, but what is right for you in your situation is not always whats right for the next guy. I understand it like this: He wants a full backup with MBR in case he wants to "reinstall" his desktop back to its former glory. Then clonezilla does the work nicely, and the backup will only be 5GB if that is the disk space he has used. And its more like a one click restore process when and if the decides to use the desktop again. – anders – 2017-12-26T11:44:50.267

@Anders Do you have proof that Clonezilla's backup will only be 5GB, and not a full raw copy of the 240GB partition? The help only shows cloning disk & partitions. That sounds like a dd copy, including 245GB of free space (that could have old possibly uncompressible data too, if clonezilla even uses compression)

– Xen2050 – 2017-12-27T01:17:36.280

"Clonezilla saves and restores only used blocks in the hard disk. Sector-to-sector copying (by dd) is only done with unsupported file systems (read Features). And I know, not only by actually reading what the software does, but by using it. You can't look at at a screenshot and assume what the software does behind the scenes. This is how clonezilla operates: MBR (by dd) Partition table (by sfdisk and parted), CHS of disk. Data on every partition or LV (logical volume) (by partimage, ntfsclone, partclone or dd. It depends on the "-q" option you choose). Your way is l33t though! – anders – 2017-12-27T09:41:00.690

@Anders Thanks, that's interesting, definitely more versatile than I'd though. They really need to work on their help pages, you'd think they'd mention the tool names more in their how-to guides & screenshots itself., and the wall of text that is their home page is TL;DR ;-) Partimage & Partclone are in Debian's repos, ntfsclone is in ntfs-3g so probably already installed in most linux's (Clonezilla's page has a dead link to the ntfs tools' page) so they can be used "your favourite" linux. – Xen2050 – 2017-12-27T22:30:44.880

Answers

2

You just need to save the files, the free space doesn't matter (unless you want to recover it's files later) so a dd like tool would be starting with 235GB (240GB minus the 5GB used) too much. Instead of spending time & effort & the limited write lifetime of the SSD trying to fill all the empty space with zeros, just backup the 5GB of files.

rsync and it's --archive flag would be good.

For an even smaller backup, use a squashfs image. It's what most of the live linux ISO's use. Running from another live system/ISO would avoid any temporary / in-use / lock files too. After mounting (say to /mnt) basically it's:

mksquashfs /mnt backup.squashfs 

You could use a probably smaller & slower xz compression with:

mksquashfs /mnt backup.squashfs -comp xz  -b 1048576

Then just mount & copy the files back when you're ready. Makes a good general backup too, since it's random access - you don't have to scan through an entire .tar.xz or


If you'll be overwriting the boot sector (and GRUB) you might want to save a copy of it too. For MBR it's just the first 512 bytes, so dd would grab it to a file called MBR.dd:

dd if=/dev/sda of=MBR.dd bs=512 count=1

GPT could use sgdisk (ref answer on U&L) like:

sgdisk --backup=<file> <device>

To restore the backup use:

sgdisk --load-backup=<file> <device>

But ignoring the partition table is good too, you'd have the chance to create & restore to a much smaller partition (only using 5GB out of 240GB) and just restoring GRUB with a live ISO is easy too.

Xen2050

Posted 2017-12-25T16:45:15.650

Reputation: 12 097