Linux / Gentoo Linux

Backing up/cloning a linux system over a network

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[3621]: 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

  1. 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/proc will be empty and dev will be empty or very sparse.

  2. 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
  3. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *