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)

Thomson TG787v and PXE boot

If you have a Thomson TG787v modem, like we have from KPN Business in the Netherlands, you might want to add an PXE server to your network to quickly install computers without the hassle of running around with CDs or thumb drives.

I will not explain how to set up the PXE environment itself (which is in fact a TFTP service) but I will stick to the modification of the modem/router. This modification can NOT be done using the webbased configuration panel.

We will log in using telnet, create option templates for the DHCP server, install those option templates and finally add them to the active pool so they will actually be in use.

If you think that’s rather a lot of work to install option 66 (TFTP server address) and option 67 (Bootfile name) into your modem, please complain to Thomson and insist they make it a) more intuitive; b) improve their documentation; c) implement these things in their web-based configuration; d) all of the above.

  • Log in on the CLI using telnet at your router, port 23 and enter your credentials
    Note: with the unlocked versions, your login will be the username from the web-based configuration and no password. Try “Administrator”.
  • Install the new template for the PXE server address:
    dhcp server option tmpladd name=tftp_server_name optionid=66
  • Install the new template for the PXE bootfile name:
    dhcp server option tmpladd name=bootfile optionid=67
  • Instantiate the templates:
    dhcp server option instadd name=tftp_server_name tmplname=tftp_server_name value=(addr)
    dhcp server option instadd name=bootfile tmplname=bootfile value=(ascii)pxelinux.0

    Note: The file name can be configured to whatever you need.

  • List all DHCP server pools: dhcp server pool list
  • Optionally, add the option to the desired pool:

    dhcp server pool optadd name=LAN_private instname=tftp_server_name
    dhcp server pool optadd name=LAN_private instname=bootfile

It seems that the template instantiation is added by default to the “LAN_private” pool.