20

I need to transfer an entire linux root filesystem off of a failing hard drive onto another computer with an open, available partition. I've pretty sure this involves tar and ssh, but I can't remember exactly how to do this.

I'm imaging probably using a live cd on the new/target host to run something like this:

ssh user@failingharddrivehost "some tar command | piped into something else"

2
  • offtopic. not a programming question. but try ssh user@failingsys "tar cfz - /" > oldsys.tar.gz
    – Marc B
    May 11, 2013 at 4:46
  • You want to take care that you do not tar /dev/ (e.g. /dev/random, /dev/sdX, ... ). Ditto /proc/
    – Hennes
    May 11, 2013 at 10:00

7 Answers 7

30

Use rsync. From the new host, you can use

rsync -avP --numeric-ids --exclude='/dev' --exclude='/proc' --exclude='/sys' root@failedharddrivehost:/ /path/to/destination/

I wouldn't try involving something like tar because it probably won't work when there are broken files.

4
  • 1
    Ended up using rsync, as describe above by etagenklo and @Hennes. Migration went well. Just need to fix grub, but that shouldn't be too bad.
    – CHK
    May 13, 2013 at 14:03
  • 10
    I'd add -AHX as flags to rsync as well in order to preserve acls, xattrs and hardlinks resulting in an even more exact copy of the original fs. Aug 5, 2016 at 8:53
  • 8
    I'd add -x to ignore items not on the root filesystem, then you can skip the --exclude arguments for various paths.
    – Alex
    Jan 15, 2018 at 1:54
  • 2
    rsync -avPAHXx --numeric-ids origin destination/
    – TNT
    Jul 18, 2021 at 18:31
8

Why are you combine with directory excluding? Isn't better idea mount the same device into another directory? modern kernels allows this way. for example, you have mounted

/dev/sda1 as / then do: mkdir /CLEANROOT mount /dev/sda1 /CLEANROOT

after this you have: /dev/sda1 as / /dev/sda1 as /CLEANROOT

This is the same filesystem visible with two places, but /CLEANROOT haven't got additive mounts. Then you can tar or rsync /CLEANROOT without any exclusions instead copying / with exclusions.

Of course you must copy another data partitions when you've got some.

Copying partition is first step for server recovery. another is regenerate boot sectors, otherwise system wont boot from copied disk. Usefull is rescue mode when you boot from install/rescue CD or pendrive.

1
  • 1
    You can also do it with a bind mount so you don't need to know the device: mount / /mnt/oldroot -o bind then confirm you aren't getting the additional mounts: ls /mnt/oldroot/proc should be an empty folder.
    – Malvineous
    Oct 19, 2021 at 5:29
7

If both computers are on the same (safe) LAN, I recommend a different approach using netcat. This is usually much faster as it doesn't encrypt the data.

root@good_host$ cd good_partition; netcat -l -p 1234 | tar xvpmf -
root@bad_host$ tar -cv -f- --exclude=/proc --exclude=/sys / | netcat good_host.ip 1234

which opens a listening port 1234 on the good machine netcat -l -p 1234 and pipes the incoming data to tar to extract (preserving mtime and permissions). The bad host sends the data to this port, also using tar and netcat. I included some --exclude parameters, as /proc and /sys are virtual filesystems and hence useless on the new host. (especially the file representing your RAM in (/proc/kcore) will add an unnecessary amount of data).

However, you should (also) consider to make a dd dump of the failing drive's partitions:

user@good_host$ cd good_partition; netcat -l -p 1234 > dump_of_bad_partition_1.dd
root@bad_host$ dd if=/dev/sda1 | netcat good_host.ip 1234

where you had to adopt /dev/sda1 to the right device. Do that with other partitions on the failing drive, too.

With that dump you are sure, that you did not miss any important metadata (like ACLs) which tar won't capture.

1

Do you have physical access to the failing host?

If you do then boot from a live CD. Then use:

  • dump (dump/restores whole filesystems including its permissions).
  • Tar with /dev excluded. You can combine this with outputting to std_out and piping that though netcat
    The exclude syntax is: tar --exclude='/dev'.
  • or rsync with the same excludes. E.g.
    rsync -zvr --exclude /dev/ / destination_computer_name_or_ip
  • or use dd like this:
    nc -l 4242 | gunzip | cat > my_full_disk_backup_of_PC_named_foo
    dd if=/dev/sda of=- bs=1M | gzip | nc -p 4242 name_of_the_destination

If you can not boot from a live CD then some most of the above solutions will stay the same, but:

  1. Some files may be in use/locked.
  2. Make sure to exclude not just /dev/ but also /proc/.
    E.g. tar --exclude='/dev' --exclude='/proc'
1
  • Yes I have physical (and root) access to both hosts. Going to try with rsync. the netcat crashed for the failing box for unknown reasons. the target filesystem doesn't want to be mounted. it fails with: #mount /dev/sda1 /mnt/fedora mount: unknown filesystem type 'LVM2_member'
    – CHK
    May 11, 2013 at 22:59
1

Here is a description of how to copy files using tar and ssh. Basically, you would run one of the following, depending on whether you want to copy local -> remote or remote -> local:

tar cf - files... | ssh remotehost -c 'cd /destination && tar xvf -'

ssh remotehost -c 'cd /destination && tar cf - files' | tar xvf -
0

you should consider using rsync

the follwoing command assumes 2 things:

  1. you are on the system with failing hard drive
  2. new partition has a minimal linux installation with ssh enabled.

rsync / new_partition:/wherever/you/want/

Note: the trailing / is important, otherwise your files will end up in one directory level above

1
  • The OP probably want to use the -a parameter (and perhaps the -A), as it preserves times, ownership, symlinks (and perhaps ACLs) etc. and the -e ssh parameter, as the data should be transferred to another computer. So rsync -aAv -e ssh root@failingharddrivehost:/ /good_computer/newpartition_mountpoint
    – mpy
    May 11, 2013 at 7:42
0

Clone a live root file system using tar

Let's say you want to clone a live Ubuntu root file system to an unused partition /dev/sdc5 on the same disk. /dev/sdc5 size must be no less than the data occupied by the root partioned being cloned. Let's assume that an EFI partition is /dev/sdc1.

  • Format the destination partition and use a label to identify it
mkfs.ext4 -L Ubuntu-sdc5 -J size=128 /dev/sdc5

mkdir /tmp/{dstRoot,srcRoot}
mount /dev/sdc5 /tmp/dstRoot # destination root
mount --bind / /tmp/srcRoot # source root
  • Clone the current root to /dev/sdc5 mounted partition
tar -C /tmp/srcRoot -cf - . | tar -C /tmp/dstRoot -xf -
  • Edit /tmp/dstRoot/etc/fstab to reflect the new label assigned to /dev/sdc5:
LABEL=Ubuntu-sdc5 / ext4 errors=remount-ro 0 1
  • Mount EFI VFAT parition
mount /dev/sdc1 /tmp/dstRoot/boot/efi
  • Clone the current Ubuntu EFI folder
mkdir /tmp/dstRoot/boot/efi/EFI/ubuntu2
rsync -av /boot/efi/EFI/ubuntu/ /tmp/dstRoot/boot/efi/EFI/ubuntu2/
  • Edit /tmp/dstRoot/boot/efi/EFI/ubuntu2/grub.cfg to reflect the new label Ubuntu-sdc5 and partition gpt5
search.fs_label Ubuntu-sdc5 root hd2,gpt5
  • Mount the necessary folders for grub-mkconfig
for i in dev dev/pts proc sys run; do mount -B /$i /tmp/dstRoot/$i; done
  • Reconfigure grub on the cloned root file system
chroot /tmp/dstRoot
grub-mkconfig -o /boot/grub/grub.cfg
exit # exit chroot

for i in run sys proc dev/pts dev; do umount /tmp/dstRoot/$i; done
umount /tmp/dstRoot/boot/efi
umount /tmp/dstRoot
umount /tmp/srcRoot
rmdir /tmp/{dstRoot,srcRoot}
  • Reboot into the EFI setup menu and add a new boot entry to boot options
systemctl reboot --firmware-setup

NOTE: The EFI boot software should automatically detect a new boot entry, but if it doesn't, boot to the original root and try to add it manually:

efibootmgr -c -d /dev/sdc -p 1 -L "Ubuntu2" -l "\EFI\ubuntu2\shimx64.efi"
  • Reboot again into the EFI setup menu and add a new boot entry to boot options

You must log in to answer this question.

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