All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Skwar <alexanders.mailinglists+nospam@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: Script for creating/managing snapshots of all subvolumes of all filesystems
Date: Fri, 3 May 2013 11:43:18 +0000 (UTC)	[thread overview]
Message-ID: <loom.20130503T133254-992@post.gmane.org> (raw)

Hi

FWIW, I've also written a script which creates and "manages"
(ie. deletes old) snapshots.

It figures out all the available filesystems and creates snaps
for all the available (sub)volumes.

It's also on https://copy.com/WI9AXqTH2nD4 and http://pastebin.com/YX8WKcsR 
to avoid line break issues and also with comments.

Regards,
Alexander

----- cut here

#!/bin/sh
    echo "Usage: $0 SNAPSHOT_TAG NUM_SNAPSHOTS
Create hourly, daily, weekly, and monthly snapshots of btrfs filesystems.

Based somewhat on http://article.gmane.org/gmane.comp.file-
systems.btrfs/12609

Here's my crontab:
00,15,30,45  * * * *  $0 frequently  4
38           * * * *  $0 hourly     24
08          00 * * *  $0 daily       7
08          12 * * 0  $0 weekly      4"
    exit 1
fi

SNAPSHOT_TAG="$1"
NUM_SNAPSHOTS="$2"

snap_prefix="snapshot:$SNAPSHOT_TAG:"
snap_date="`date +%Y-%m-%d--%H.%M.%S.%N`"
script_name=`basename "$0"`

log_fac="local5"
log_tag="$script_name"

btrfs_progs_dev_path="/home/a/Copy/Computerkram/Programme/btrfs-
progs.dev/bin"
PATH="$btrfs_progs_dev_path:$PATH"

btrfs fi show 2>/dev/null | awk '/ path / {print $NF}' | while read dev; do
    set -- `btrfs fi show 2>/dev/null | grep -B2 " path $dev" | \
     grep "Label:" | sed  's,.*: \(.*\)  uuid: \(.*\),\1 \2,'`
    label="$1"
    uuid="$2"
    logger -t "$log_tag" -p "$log_fac.info" -- \
     "Processing filesystem with label $label and uuid $uuid on $dev"
    safe_dev=`echo "$dev" | tr / .`
    tmp_mount_dir=`mktemp -d "/tmp/.btrfs.mount.$uuid.$safe_dev.XXXXXX"`
    if ! mount -t btrfs "$dev" "$tmp_mount_dir"; then
        logger -t "$log_tag" -p "$log_fac.err" -- \
         "Error! Could not do: mount -t btrfs $dev $tmp_mount_dir"
        exit 1
    fi
    
    _snap_name="$tmp_mount_dir/,$snap_prefix$snap_date"
    if ! btrfs subv snaps -r "$tmp_mount_dir" "$_snap_name" > /dev/null; 
then
        logger -t "$log_tag" -p "$log_fac.err" -- \
         "Error! Could not do: btrfs subv snaps -r $tmp_mount_dir 
$_snap_name"
        exit 1
    else
        logger -t "$log_tag" -p "$log_fac.info" -- \
        "Created snapshot $Path,$snap_prefix$snap_date for root volume of fs 
with uuid $uuid"
    fi
    (btrfs subv list -r "$tmp_mount_dir" | grep " path ,$snap_prefix" \
     | tail -"$NUM_SNAPSHOTS"
     btrfs subv list -r "$tmp_mount_dir" | grep " path ,$snap_prefix") \
     | sort | uniq -u \
     | while read __id IdDel __gen GenDel __top __level ToplevelDel __path 
PathDel; do
        if ! btrfs subv del "$tmp_mount_dir/$PathDel" > /dev/null; then
            logger -t "$log_tag" -p "$log_fac.err" -- \
             "Error! Could not do: btrfs subv del $tmp_mount_dir/$PathDel"
            exit 1
        else
            logger -t "$log_tag" -p "$log_fac.info" -- \
             "Removed snapshot $PathDel"
        fi
    done
    
    (btrfs subv list -ar $tmp_mount_dir; btrfs subv list -a $tmp_mount_dir) 
\
     | sort | uniq -u \
     | while read _id Id _gen Gen _top _level Toplevel _path Path; do
        _snap_name="$tmp_mount_dir/$Path,$snap_prefix$snap_date"
        if ! btrfs subv snaps -r "$tmp_mount_dir/$Path" "$_snap_name" > 
/dev/null; then
            logger -t "$log_tag" -p "$log_fac.err" -- \
             "Error! Could not do: btrfs subv snaps -r $tmp_mount_dir/$Path 
$_snap_name"
            exit 1
        else
            logger -t "$log_tag" -p "$log_fac.info" -- \
             "Created snapshot $Path,$snap_prefix$snap_date for subvolume 
$Path"
        fi
        (btrfs subv list -r "$tmp_mount_dir" \
         | grep " path $Path,$snap_prefix" | tail -"$NUM_SNAPSHOTS"
         btrfs subv list -r "$tmp_mount_dir"|grep " path 
$Path,$snap_prefix") \
         | sort | uniq -u \
         | while read __id IdDel __gen GenDel __top __level ToplevelDel 
__path PathDel; do
            if ! btrfs subv del "$tmp_mount_dir/$PathDel" > /dev/null; then
                logger -t "$log_tag" -p "$log_fac.err" -- \
                 "Error! Could not do: btrfs subv del 
$tmp_mount_dir/$PathDel"
                exit 1
            else
                logger -t "$log_tag" -p "$log_fac.info" -- \
                 "Removed snapshot $PathDel"
            fi
        done
    done
    if ! umount "$tmp_mount_dir"; then
        logger -t "$log_tag" -p "$log_fac.err" -- \
         "Error! Could not do: umount$tmp_mount_dir"
    fi
    if ! rmdir "$tmp_mount_dir"; then
        logger -t "$log_tag" -p "$log_fac.err" -- \
         "Error! Could not do: rmdir $tmp_mount_dir"
    fi
done

exit 0



             reply	other threads:[~2013-05-03 11:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-03 11:43 Alexander Skwar [this message]
2013-05-05 17:36 ` Script for creating/managing snapshots of all subvolumes of all filesystems Kai Krakow
2013-05-05 19:26   ` Alexander Skwar

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=loom.20130503T133254-992@post.gmane.org \
    --to=alexanders.mailinglists+nospam@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.