Copying LV contents over the LAN
Posted on Thu 09 April 2020 in Software
I've been upgrading my storage server's main disks from hard drives to SSDs. The disks are a software (MDADM) raid 1 with LVM on top to partition out storage to each VM.
I wanted to ensure I had a good copy at all times, so I set up the new SSDs on a spare box using a Debian live USB. I manually created the PV, VG and LVs on the new disks but I didn't want to try to figure out DD over SSH with sudo to copy the disk contents over the network.
Instead I found a post that showed how to use netcat to do the work. The transfer is unencrypted but given it's my home network on a trusted VLAN, I'm okay with this.
The commands I used are:
On the target host:
sudo sh -c "nc -l -p 6789 | pv | dd of=/dev/$vg_name/$lv_name"
On the sending host:
sudo sh -c "dd if=/dev/$vg_name/$lv_name bs=4M | pv | nc $target_host_ip 6789 -q 10"
This is assuming that netcat (nc) and pv are installed. If not, they need to be installed with:
sudo apt-get install netcat pv
I manually created the LVs because I only had a few to copy over and I did not copy over the swap disk contents. Instead I recreated the LVs and ran a mkswap on them.