This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

Btrfs/snapshots

From Gentoo Wiki (test)
< Btrfs
Jump to:navigation Jump to:search
Resources

Btrfs

Btrfs is a copy-on-write (CoW) filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration. Jointly developed at Oracle, Red Hat, Fujitsu, Intel, SUSE, STRATO and many others, Btrfs is licensed under the GPL and open for contribution from anyone.

Automatic snapshots

This scripts uses Btrfs subvolume list-new function to create snapshots only when files have changed, which is used to create fewer snapshots.

Note
The script assumes the following layout:
  • /mnt/pool - mountpoint for btrfs root volume
  • /mnt/pool/volumes/home - user homes volume
  • /mnt/pool/snapshots/home - symlink to latest snapshot
  • /mnt/pool/snapshots/home_@GMT_2017.02.11-22.00.01 - latest snapshot of home
FILE /mnt/pool/snapshots/snapshot_home.shAutomatic incremental snapshots
#!/bin/bash
d="$(date +%Y.%m.%d-%H.%M.%S)"
echo "Backup started at ${d}"
time="@GMT_${d}"

# Mountpoint
mount="/mnt/pool/volumes/home"

## symlink to most recent snapshop
symlink="/mnt/pool/snapshots/home"
old_snap="$(readlink "/mnt/pool/snapshots/home")"

## New snapshot name
new_snap=$symlink"_$time"

## Check for most recent generation ID for most recent snapshot.
## This is used when looking for changed files.
if [ -d "$old_snap" ]; then
        # find-new outputs last generation ID when using a too high value is used for comparing.
        gen_id=$(/sbin/btrfs sub find-new "$old_snap" 9999999|cut -d " " -f 4)

        # Count changed files.
        count="$(/sbin/btrfs subvolume find-new "$mount" $gen_id | cut -d " " -f 17-1000 | sed '/^$/d'| wc -l)"
        /sbin/btrfs subvolume find-new "$mount" $gen_id | cut -d " " -f 17-1000
        # Create a new snapshot if we have changed files.
        if [[ $count > '0' ]]; then
                /sbin/btrfs subvolume snapshot -r "$mount" "$new_snap"

                ## Recreate a symnlink to the new snapshot
                rm "$symlink" && ln -s "$new_snap" "$symlink"
        else
                echo "No files changed, skipping creating of a new snapshot"
        fi
else
        echo Something went wrong. Check that symlink $symlink points to a real snapshot
fi
echo "Backup finished at $(date +%Y.%m.%d-%H.%M.%S)"


It is recommended you schedule to run the /mnt/pool/snapshots/snapshot_home.sh with cron.

FILE /etc/cron.hourly/autosnap.shhourly cron script
#!/bin/bash
/mnt/pool/snapshots/snapshot_home.sh >> /mnt/pool/snapshots/snapshot_home.log 2>&1
Important
Btrfs subvolume find-new does not detect all types of changes, for example deleted files. If you need to ensure maximum time difference since deletion of files, then you need to schedule normal time-based snapshots.

For more detailed information on btrfs subvolumes and snapshots see the btrfs wiki.

See also

  • Btrfs/System Root Guide - Use the Btrfs filesystem as a collection of subvolumes including one as a system root.
  • Btrfs native system root guide - An alternative guide on using a subvolume in a Btrfs filesystem as the system's root.
  • Snapper - A command-line program capable of managing Btrfs filesystem snapshots.
  • Samba shadow copies - Using Samba to expose Shadow Copies as 'Previous Versions' to Windows clients.
  • ext4 - The default filesystem for most Linux distributions.
  • ZFS - A filesystem that shares much in common with Btrfs, but has licensing issues.