< Btrfs
Btrfs/snapshots
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:
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<syntaxhighlight lang="bash">#!/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)"</syntaxhighlight>
It is recommended you schedule to run the /mnt/pool/snapshots/snapshot_home.sh
with cron.
FILE
/etc/cron.hourly/autosnap.sh
hourly cron script<syntaxhighlight lang="bash">#!/bin/bash /mnt/pool/snapshots/snapshot_home.sh >> /mnt/pool/snapshots/snapshot_home.log 2>&1</syntaxhighlight>
Important
Btrfs
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.
- Btrbk - A backup tool for btrfs subvolumes, taking advantage of btrfs specific capabilities to create atomic snapshots and transfer them incrementally to specified backup locations.
- 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 — 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.
This article is issued from Gentoo. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.