10

I need to make a copy of a working linux system, which (being contained in /, and the new place is prepared in /mnt/sdb5) seems to contain considerable amount of hard-, soft-links and special files in /dev; would cpio handle this job without applying additional magic?

Currently known safeguards:

  • not going to remove/modify running system until the moment the copy is considered bootable and working with root=/dev/sdb5; before removing, take a full partition backup.
  • will use cpio for archiving each root directory separately, thus will unpack it from LiveCD environment so donor partition will not be harmed

But still, not going to lose time just because cpio missed some flag and crippled the permissions/node type/soft or hardlink.

Which tool to use / which underwater rocks to avoid?

Teddy
  • 5,134
  • 1
  • 22
  • 27
kagali-san
  • 1,921
  • 5
  • 17
  • 20

4 Answers4

12

To answer the actual question regarding cpio: These are the flags I would use for cpio:

find / -xdev -depth \! -path ./lost+found -print0 | cpio --pass-through --null --dot --make-directories --unconditional --preserve-modification-time --sparse /mnt/sdb5

Of course, since you're not copying over the network, I would just use cp:

cp --archive --sparse=always --verbose --one-file-system --target-directory=/mnt/sdb5 /

And if you want to be able to do the copying several times, rsync is a better choice for its resuming capabilities. (It also, like cp, handles ACLs and extended attributes and can optionally work over the network like cpio. So it's the most useful option, except for doing the first copy locally, which I prefer to do using cp.)

rsync --archive --inplace --hard-links --acls --xattrs --devices --specials --one-file-system --8-bit-output --human-readable --progress / /mnt/sdb5

Don't Forget to copy /boot and /dev!

/boot is easy, just copy it. But /dev is much trickier nowadays since it's hidden by udev. I recommend the following procedure:

  1. mkdir /tmp/dev
  2. mount --move /dev /tmp/dev
  3. Copy /dev to /mnt/sdb5 using one of the above commands
  4. mount --move /tmp/dev /dev
  5. rmdir /tmp/dev
Teddy
  • 5,134
  • 1
  • 22
  • 27
6

As @Klox mentioned, when copying partitions of the same size I agree on using dd.

But when you want to copy a disk onto a different partition whith different size, I'd rather go with rsync. Mount the new partition (let's say, /mnt/new) and:

# rsync -a --exclude=/proc --exclude=/dev --exclude=/sys / /mnt/new

No aditional magic for symlinks and no need for live cd (single user / init 1 will do just fine).

Torian
  • 2,314
  • 18
  • 10
  • Ahh, yes. I use `rsync` all the time and missed the obvious. I like `dd` for the reasons I stated, but `rsync` is a better fit for the OP's needs. – Klox Aug 31 '11 at 01:41
3

When moving Linux installations between hard drives, I always boot from a Live CD and use dd to copy the entire partition. I recognize that this doesn't deal with changes in disk size (inevitably the new disk is bigger, which simplifies things), but I like the technique for exactly the reasons you are concerned about using cpio: something may go wrong. Using the dd technique, it's all or nothing: either the new disk boots and everything is the same, or the disk doesn't boot. There's no risk of lurking problems popping up later.

Now, of course, there's the issue of the partition not filling the new disk, but I'd rather just create a new partition to fill the extra space and rely on symlinks to move directories around. (I'm sure there's tools for resizing partitions, too, but I haven't used them.)

Klox
  • 173
  • 1
  • 7
  • The system I talk about has just about 50 gigs free on 500 gb XFS partition (that can't be shrinked, currently only growing is supported via `xfs_grow`), and the root partition is XFS too; even worse, it is a well-established Gentoo which is almost that fat itself. Even worse, the move has to be done without adding more storage and currently can't backup more than 60 gigs - so no partition operations. Need to put Win' to that machine for testing some stuff. – kagali-san Aug 31 '11 at 00:12
  • also, dd is nice, but for drive/partition operations I tend to use more .. proprietary solutions. Acronis TrueImage seems to work better (compression, samba shares - from livecd/liveusb flash). – kagali-san Aug 31 '11 at 00:16
3

The more optimal variant of dd is using partimage, it will copy only the used section of the partition making copying of large unused partitions more expedient.

Note the important caveat:

Partimage does NOT support Ext4 which is the default on new Ubuntu installations.

A convenient copy is included on the System Rescue CD distribution.

Steve-o
  • 829
  • 6
  • 12