[HowTo] Copy/Move a subvolume from one partition to another

Difficulty: ★★★☆☆

While there are a some applications, which provide a tooling for doing incremental backups, yet there is no simple tutorial which just explain how to move a subvolume. That is the reason why I try to explain the basic steps here.

Preparation

  1. Install pv:
pamac install pv

:notebook: pv is needed to monitor the progress, when streaming or piping on the terminal, but is rather optional.

  1. Switch to root:
sudo --shell --user root
  1. Set the source and target path:
export SOURCE="/storage"
export TARGET="/backup"
export SUBVOL="@subvolume"

Copy the subvolume

  1. Set the subvolume read-only:
btrfs property set -t subvol ${SOURCE}/${SUBVOL} ro true
  1. Stream the read-only subvolume to another partition:
btrfs send ${SOURCE}/${SUBVOL} | pv | btrfs receive ${TARGET}
  1. Set the streamed read-only subvolume writable:
btrfs property set -t subvol ${TARGET}/${SUBVOL} ro false
  1. Set the source subvolume writable again:
btrfs property set -t subvol ${SOURCE}/${SUBVOL} ro false

:notebook: Now the subvolume is completely copied 1:1.
:spiral_notepad: At this stage, it is also possible to use rsync to update the subvolume. Streaming of the subvolume speeds up the initial process heavily.

Move the subvolume

  1. Set the subvolume read-only:
btrfs property set -t subvol ${SOURCE}/${SUBVOL} ro true
  1. Stream the read-only subvolume to another partition:
btrfs send ${SOURCE}/${SUBVOL} | pv | btrfs receive ${TARGET}
  1. Set the streamed read-only subvolume writable:
btrfs property set -t subvol ${TARGET}/${SUBVOL} ro false
  1. Set the source subvolume writable again:
btrfs property set -t subvol ${SOURCE}/${SUBVOL} ro false
  1. Delete the source subvolume and therefore completely move it:
btrfs subvolume delete -c ${SOURCE}/${SUBVOL}

:notebook: Now the subvolume is completely moved 1:1.


:warning: This is a wiki article. You are free to copy, share, or edit the content without restrictions as long as it doesn’t miss the main topic.

2 Likes