Copy Linux sparse files over network
Sparse files are nice to use to store virtual machine’s virtual disks but can be a real pain in the ass to backup efficiently, especially over the network.
Luckily rsync
provides a way to intelligently copy sparse files both locally and over the network.
The trick is use --sparse
and --inplace
options.
Let’s say we have a sparse 60 GB qemu virtual disk with only around 7 GB used:
$ ls -lh fedora24.qcow2
-rw------- 1 root root 61G Nov 8 19:11 fedora24.qcow2
$ du -h fedora24.qcow2
7.2G fedora24.qcow2
The first thing to note is that ls does not recognize sparse files while du does.
The first time a file is copied use the --sparse
option:
$ rsync -avP --sparse fedora24.qcow2 root@remotemachine:/tmp
Every other time use --inplace
to tell rsync
to copy only the blocks that actually changed since the last copy:
$ rsync -avP --inplace fedora24.qcow2 root@remotemachine:/tmp