how to copy entire linux root filesystem to new hard drive on with ssh and tar
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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:
- Some files may be in use/locked.
- Make sure to exclude not just /dev/ but also /proc/.
E.g.tar --exclude='/dev' --exclude='/proc'
Solution 5
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 -
Related videos on Youtube
CHK
Updated on September 18, 2022Comments
-
CHK over 1 year
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
andssh
, 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"
-
Admin almost 11 yearsofftopic. not a programming question. but try
ssh user@failingsys "tar cfz - /" > oldsys.tar.gz
-
ganesh almost 11 yearsYou want to take care that you do not tar /dev/ (e.g. /dev/random, /dev/sdX, ... ). Ditto /proc/
-
-
mpy almost 11 yearsThe 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. Sorsync -aAv -e ssh root@failingharddrivehost:/ /good_computer/newpartition_mountpoint
-
CHK almost 11 yearsYes 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 almost 11 yearsEnded 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.
-
Alexander Remesch almost 8 yearsI'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.
-
Alex over 6 yearsI'd add -x to ignore items not on the root filesystem, then you can skip the --exclude arguments for various paths.
-
TNT almost 3 yearsrsync -avPAHXx --numeric-ids origin destination/
-
Malvineous over 2 yearsYou 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.