public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Marc MERLIN <marc@merlins.org>
To: Duncan <1i5t5.duncan@cox.net>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: Understanding btrfs and backups => automatic snapshot script
Date: Sun, 16 Mar 2014 22:42:24 -0700	[thread overview]
Message-ID: <20140317054224.GD6143@merlins.org> (raw)
In-Reply-To: <pan$ee422$4703cdfd$58fec22b$1a891275@cox.net>

On Thu, Mar 06, 2014 at 09:33:24PM +0000, Duncan wrote:
> However, best snapshot management practice does progressive snapshot 
> thinning, so you never have more than a few hundred snapshots to manage 
> at once.  Think of it this way.  If you realize you deleted something you 
> needed yesterday, you might well remember about when you deleted it and 
> can thus pick the correct snapshot to mount and copy it back from.  But 
> if you don't realize you need it until a year later, say when you're 
> doing your taxes, how likely are you to remember the specific hour, or 
> even the specific day, you deleted it?  A year later, getting a copy from 
> the correct week, or perhaps the correct month, will probably suffice, 
> and even if you DID still have every single hour's snapshots a year 
> later, how would you ever know which one to pick?  So while a day out, 
> hourly snapshots are nice, a year out, they're just noise.

I'm happy to share my script with others if that helps:
http://marc.merlins.org/linux/scripts/btrfs-snaps

Or for the list archives/google:
----------------------------------------------------------------------------
#!/bin/bash

# By Marc MERLIN <marc_soft@merlins.org>
# License GPL-2 or BSD at your option.

# This lets you create sets of snapshots at any interval (I use hourly,
# daily, and weekly) and delete the older ones automatically.

# Usage:
# This is called from /etc/cron.d like so:
# 0 * * * * root btrfs-snaps hourly 3 | egrep -v '(Create a snapshot of|Will delete the oldest|Delete subvolume|Making snapshot of )'
# 1 0 * * * root btrfs-snaps daily  4 | egrep -v '(Create a snapshot of|Will delete the oldest|Delete subvolume|Making snapshot of )'
# 2 0 * * 0 root btrfs-snaps weekly 4 | egrep -v '(Create a snapshot of|Will delete the oldest|Delete subvolume|Making snapshot of )'

: ${BTRFSROOT:=/mnt/btrfs_pool1}
DATE="$(date '+%Y%m%d_%H:%M:%S')"

type=${1:-hourly}
keep=${2:-3}

cd "$BTRFSROOT"

for i in $(btrfs subvolume list -q . | grep "parent_uuid -" | awk '{print $11}')
do
    # Skip duplicate dirs once a year on DST 1h rewind.
    test -d "$BTRFSROOT/${i}_${type}_$DATE" && continue
    echo "Making snapshot of $type"
    /sbin/btrfs subvolume snapshot "$BTRFSROOT"/$i "$BTRFSROOT/${i}_${type}_$DATE"
    count="$(ls -d ${i}_${type}_* | wc -l)"
    clip=$(( $count - $keep ))
    if [ $clip -gt 0 ]; then
	echo "Will delete the oldest $clip snapshots for $type"
	for sub in $(ls -d ${i}_${type}_* | head -n $clip)
	do
	    #echo "Will delete $sub"
	    /sbin/btrfs subvolume delete "$sub"
	done
    fi
done
----------------------------------------------------------------------------
-- 
"A mouse is a device used to point at the xterm you want to type in" - A.S.R.
Microsoft is to operating systems ....
                                      .... what McDonalds is to gourmet cooking
Home page: http://marc.merlins.org/  

  parent reply	other threads:[~2014-03-17  5:42 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-06 18:18 Understanding btrfs and backups Eric Mesa
2014-03-06 21:33 ` Duncan
2014-03-07 10:13   ` Wolfgang Mader
2014-03-09 15:46     ` Duncan
2014-03-07 14:03   ` Eric Mesa
2014-03-07 15:14     ` Sander
2014-03-09  4:13       ` Chris Samuel
2014-03-09 15:30         ` Duncan
2014-03-13  8:18           ` Chris Samuel
2014-03-09 16:40     ` Duncan
2014-03-11  0:39       ` Testing BTRFS Lists
2014-03-11  1:02         ` Avi Miller
2014-03-11 19:08           ` Eric Sandeen
2014-03-11 20:30             ` Avi Miller
2014-03-12 11:15             ` xfstests btrfs/035 (was Re: Testing BTRFS) David Disseldorp
2014-03-13 18:10           ` Testing BTRFS Lists
2014-03-13 20:20             ` Avi Miller
2014-03-11 13:33         ` Josef Bacik
2014-03-13 17:12     ` Understanding btrfs and backups Chris Murphy
2014-03-17  5:42   ` Marc MERLIN [this message]
2014-03-21  5:57     ` Understanding btrfs and backups => automatic snapshot script Marc MERLIN
2014-03-21  7:41       ` Duncan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140317054224.GD6143@merlins.org \
    --to=marc@merlins.org \
    --cc=1i5t5.duncan@cox.net \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox