Btrfs/snapshots
From Gentoo Wiki
< Btrfs(Redirected from Btrfs snapshots)
Jump to:navigation
Jump to:search
This page proposes a script to make automatic snapshots with Btrfs filesystem, using btrfs subvolume list-new function to create snapshots only when files have changed, so as to create fewer snapshots.
Note
The script assumes the following layout:
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.sh
Automatic 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 files have changed. 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 to schedule the /mnt/pool/snapshots/snapshot_home.sh with cron.
FILE
/etc/cron.hourly/autosnap.sh
hourly 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. To ensure maximum time difference since deletion of files, then schedule normal time-based snapshots.
btrfs subvolume find-new does not detect all types of changes, for example deleted files. To ensure maximum time difference since deletion of files, then 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.
- Btrbk — a tool for creating incremental snapshots and remote backups of Btrfs subvolumes.
- Snapper — a command-line program to create and manage filesystem snapshots, allowing viewing or reversion of changes.
- Samba shadow copies - Using Samba to expose Shadow Copies as 'Previous Versions' to Windows clients.
- Ext4 — an open source disk filesystem and most recent version of the extended series of filesystems.
- ZFS — a next generation filesystem created by Matthew Ahrens and Jeff Bonwick.