I had to perform this emergency procedure every now and then and I seem to reinvent the wheel every time so I reckoned it was time to document it.
This time, its a failing master drive in a simple home server – without a RAID. The SMART monitoring has not yet caught on but the self-tests fail and some files are read-only as writes to the drive fail. Also the logs are full with SATA errors like:
Apr 29 10:23:57 rivenscryr smartd: Device: /dev/sda, 18 Currently unreadable (pending) sectors
To save the installation and all the files, I want to clone or backup the entire root filesystem (which is ext3, but that is not relevant) to a new drive (formatted ext4) in another machine. The reason to do this between machines can be numerous: perhaps you are migrating a live machine or (in this case) perhaps you worry that the current drive may fail when you power-cycle the system when the replacement drive is installed.
Backup/Clone Over Network Recipe ∞
- Mount the root filesystem of the original machine on another location in order to have a clean copy:
mkdir /mnt/rootfs && mount / /mnt/rootfs -o bind
Check if it worked by listing the
/mnt/rootfs/folder. Note that
/mnt/rootfs/procwill be empty and
devwill be empty or very sparse.
- Use netcat and tar on the receiving machine (my case: random system with Ubuntu on USB stick and new HDD plugged in) to receive and write the data.
nc -l 7777 | tar -xz -C /mnt/targetdir
Use tar and netcat on the original machine to stream-copy the root filesystem over the network.
Big fat warning: My cloning is done on an internal, trusted network. You will be cloning using an unsecured stream over this network.
tar -czp --atime-preserve --numeric-owner \ --xattrs --recursion /mnt/rootfs | nc remotehost 7777
Here is what all these options do:
- -c: Create a new archive
- -z: Use GZip compression to speed up the transfer
- -p: Preserve file permissions
- –atime-preserve: Do not update the accessed timestamp
- –numeric-owner: Copy using numeric ids only – useful when cloning into a foreign system
- –xattrs: Include ACL attributes and SElinux attributes
- –recursion: Recurse down into sub-folders
- –ignore-failed-read: Useful when a drive is heavily damaged and you only care to extract as much data as possible. Note: do not enable this by default as you might end up with an unusable clone if some files are missing. (Aka, this is a last resort option for non-functional clones)