├── .gitignore ├── CHANGELOG.md ├── README.md └── btrfs-snap /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # btrfs-snap Changelog 2 | ## v1.7.4 (Apr 02 2023) 3 | * Minor bugfix to work with btrfs-progs 5.10 4 | 5 | # btrfs-snap Changelog 6 | ## v1.7.3 (Jul 25 2017) 7 | * Removed perl dependency (replaced perl regex by bash regex) 8 | 9 | ## v1.7.2 (Feb 11 2017) 10 | * Added new switch -q which makes output cron-compatible: silent unless an error occurs. 11 | * Regex fix: correctly remove trailing slashes on mount-points 12 | 13 | ## v1.7.1 (Mar 16 2016) 14 | * Fixed timestamp-bug (introduced in v1.6.1) 15 | 16 | ## v1.7.0 (Mar 14 2016) 17 | * Added new switch -E which treats omitting a snapshot as an error. 18 | 19 | ## v1.6.1 (Mar 14 2016) 20 | * Bugfix: touch to update the timestamp is only needed for option '-t'. 21 | For '-T' it is counter-productive and therefore disabled. 22 | 23 | ## v1.6.0 (Mar 09 2016) 24 | * Added new flag -T which uses the transaction ids of btrfs to decide if new data is present. 25 | 26 | ## v1.5.0 (Mar 09 2016) 27 | * Added new flag -p to use the as a postfix. 28 | 29 | ## v1.4.2 (Feb 19 2016) 30 | * Bugfix for prefix VFS: Removed an unneccessary $-sign which broke the code. (Thanks to 'clawes' for reporting.) 31 | 32 | ## v1.4.1 (Aug 31 2015) 33 | * Bugfix for switch -t: Force update of source timestamp to prevent outdated timestamps on the folders 34 | 35 | ## v1.4.0 (Aug 28 2015) 36 | * Added checks for existing snapshots and use pattern to search for existing snapshots 37 | * Added a switch -B to specify the full path snapshot path 38 | * Added a switch -t time to create only a snapshot if the newest already existing snapshot is older than 'time' seconds. 39 | * Added PATH since script is most likely called by crontab with reduced PATH-settings 40 | 41 | ## v1.3.2 (Jan 10 2015) 42 | * added switch to generate more compatible snapshot names 43 | (no colons in file names: this confuses SAMBA/Windows clients) 44 | 45 | ## v1.3.1 (Jan 05 2015) 46 | 47 | * added switch to override default snapshot directory name ".snapshot" 48 | * cleanup 49 | 50 | ## v1.2.2 (Sept 24 2013) 51 | 52 | * trailing slash removal failed on multipart paths due to missing line 53 | terminator. Time for a bats test suite. 54 | 55 | ## v1.2.1 (Sept 24 2013) 56 | 57 | * fix trailing slash removal so that it works when the mountpoint is '/'. 58 | This required a change to perl from sed (standard sed doesn't have negated 59 | character classes) 60 | 61 | ## v1.2.0 (Sept 21 2013) 62 | 63 | * added VFS snapshot naming support courtesy of gitmopp 64 | 65 | ## v1.1.0 (July 30 2013) 66 | 67 | * add README 68 | * add readonly snapshot and base directory features 69 | 70 | ## v1.0.0 (July 20 2013) 71 | 72 | * Initial version written by Birger Monsen 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | btrfs-snap 2 | ========== 3 | 4 | btrfs-snap creates and maintains the history of snapshots of btrfs filesystems. 5 | 6 | Features: 7 | 8 | * can snapshot inside of the filesystem or rooted in a base directory 9 | * can purge old snapshots when too many exist 10 | * can keep multiple snapshot schedules for the same filesystem 11 | 12 | btrfs-snap is designed to be called from cron on a regular schedule. 13 | 14 | Usage 15 | ----- 16 | 17 | btrfs-snap [-r] [-b basedir] mountpoint prefix count 18 | 19 | * mountpoint is the filesystem to snapshot 20 | * prefix is the prefix, which usually corresponds to a schedule (e.g. 1m, 5m, 3h, 1d, 1w, 3mo) 21 | * count is the number of snapshots with the same prefix to keep 22 | * -r makes the snapshot readonly (requires btrfs-tools v0.20) 23 | * -c generates more compatible snapshot names 24 | (ie. no colons that confuse SAMBA/Window$ clients) 25 | * -E treats omitting of snapshots as error 26 | * -p redefines the prefix to be used as postfix 27 | * -d dir places the snapshot in dir, relative to the mountpoint 28 | * -b basedir places the snapshot in basedir with a directory structure that mimics the mountpoint 29 | * -B basedir places the snapshots in basedir with NO additional subdirectory structure 30 | * -t/-T time creates only a snapshot if the newest already existing snapshot is older than 'time' seconds. 31 | * -q output becomes cron-compatible: silent unless an error occurs. 32 | 33 | Without -b, -B, or -d, snapshots are placed in a directory called .snapshot at the top of the mountpoint. 34 | 35 | mountpoint must be a mounted btrfs filesystem (i.e. appears in the output of 36 | "mount -t btrfs") or an unmounted btrfs subvolume (which must exit 0 when 37 | "btrfs su sh" is called on it). Unmounted subvolumes require a recent 38 | version of btrfs-progs; see "OS Support" below for specifics. 39 | 40 | Examples 41 | -------- 42 | 43 | ```cron 44 | */5 * * * * btrfs-snap -r / 5m 12 45 | 0 0 * * * btrfs-snap -r -b /snapshots /home 1d 7 46 | ``` 47 | 48 | The above cronjob would 49 | 50 | 1. create a snapshot of / every 5 minutes, keeping 12 generations in /.snapshot 51 | 1. create a snapshot of /home every day, keeping 7 generations in /snapshots/home 52 | 53 | OS Support 54 | ---------- 55 | 56 | Tested on 57 | 58 | * Ubuntu 12.04 with the raring kernel 59 | * Ubuntu 13.04 60 | * Debian 8 61 | * Debian 9 62 | * Debian 11 (bullseye) 63 | 64 | Anything else, YMMV. Reports of successful use on other operating systems 65 | is welcome 66 | 67 | To snaphshot an unmounted btrfs subvolume, you need a version of btrfs-progs 68 | that supports the "btrfs subvolume show" command. 69 | 70 | The version that comes with Ubuntu 13.04 is too old; the version that comes 71 | with Ubuntu 13.10 works. You should be able to compile a working version 72 | from the sources (refer to 73 | https://btrfs.wiki.kernel.org/index.php/Btrfs_source_repositories for 74 | details). 75 | 76 | License 77 | ------- 78 | 79 | This program is distributed under the GNU General Public License 80 | 81 | http://www.gnu.org/licenses/gpl.txt 82 | 83 | Authors 84 | ------- 85 | 86 | Originally by Birger Monsen 87 | 88 | Readonly and basedir additions by James FitzGibbon 89 | 90 | VFS snapshot naming support by gitmopp (https://github.com/gitmopp) 91 | 92 | Support for snapshotting unmounted btrfs subvolumes by Brian Kloppenborg (https://github.com/bkloppenborg) 93 | 94 | Switches -c (for windows-combitible timestamps) and -d (for specifying the snapshot directory) by Lukas Pirl (btrfs-snap@lukas-pirl.de) 95 | 96 | Switches -B (absolute path of snapshot directory) and -t/-T (time-dependent snapshot) by Michael Walz (btrfs-snap@serpedon.de) 97 | 98 | 99 | -------------------------------------------------------------------------------- /btrfs-snap: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # btrfs-snap - make periodic snapshots of btrfs filesystem 4 | # 5 | # Copyright (C) 2010 Birger Monsen birger@birger.sh 6 | # Copyright (C) 2013 James FitzGibbon james@nadt.net 7 | # Copyright (C) 2014 Brian Kloppenberg 8 | # Copyright (C) 2015 Lukas Pirl btrfs-snap@lukas-pirl.de 9 | # Copyright (C) 2015-2017 Michael Walz btrfs-snap@serpedon.de 10 | # Copyright (C) 2017 Raimund Jacob-Bloedorn jacob@pinuts.de 11 | 12 | # This program is distributed under the GNU General Public License 13 | # http://www.gnu.org/licenses/gpl.txt 14 | # 15 | 16 | set -u 17 | 18 | LOG_FACILITY=local0 19 | VERSION="1.7.4" 20 | prog=${0##*/} 21 | PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin" 22 | 23 | USAGE="Usage: ${prog} -h for usage help 24 | ${prog} -V for version 25 | ${prog} [options] " 26 | SYNOPSIS="${prog} [options] 27 | is the mountpoint of the btrfs file system to make a 28 | snapshot of 29 | is the prefix to be used in the name of the snapshot. 30 | E.g. hourly, daily, weekly... 31 | If prefix=VFS a Samba vfs_shadow_copy snapshot naming convention 32 | will be used (@GMT-%Y.%m.%d-%H.%M.%S). 33 | The number of snapshots with the given prefix to keep. 34 | 35 | btrfs-snap / hourly 24 36 | would make a snapshot in /.snapshot called hourly__