DEV Community

klo2k
klo2k

Posted on

Convert directory into BTRFS subvolume

I've been using BTRFS for development and am loving the simplicity and features it provides.

One thing I do a lot is database snapshot on docker volume (to test db migration) - which I can easily do with BTRFS.

Here's how:

  1. Convert docker volume directory into BTRFS subvolume
  2. Snapshot the docker volume (now a BTRFS subvolume)

Convert docker volume directory into BTRFS subvolume

# Directory to convert into BTRFS subvolume
dirPath='/var/lib/docker/test_docker_volume'

# Rename original dierctory
mv "${dirPath}" "${dirPath}_original"

# Create btrfs subvolume
btrfs subvolume create "${dirPath}"

# Copy as "reflink" for speed and save space
cp --archive --one-file-system --reflink=always \
  "${dirPath}_original/." "${dirPath}"

# Remove old directory
rm -rf --one-file-system "${dirPath}_original"
Enter fullscreen mode Exit fullscreen mode

Snapshot the docker volume

Create a read-only snapshot of test_docker_volume docker volume:

btrfs subvolume snapshot -r \
  /var/lib/docker/test_docker_volume \
  /var/lib/docker/test_docker_volume_bak
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
lulonaut profile image
Lulonaut

Thank you!