how to copy entire linux root filesystem to new hard drive on with ssh and tar
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.
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.
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.