All of lore.kernel.org
 help / color / mirror / Atom feed
* Script for creating/managing snapshots of all subvolumes of all filesystems
@ 2013-05-03 11:43 Alexander Skwar
  2013-05-05 17:36 ` Kai Krakow
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Skwar @ 2013-05-03 11:43 UTC (permalink / raw)
  To: linux-btrfs

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Script for creating/managing snapshots of all subvolumes of all filesystems
  2013-05-03 11:43 Script for creating/managing snapshots of all subvolumes of all filesystems Alexander Skwar
@ 2013-05-05 17:36 ` Kai Krakow
  2013-05-05 19:26   ` Alexander Skwar
  0 siblings, 1 reply; 3+ messages in thread
From: Kai Krakow @ 2013-05-05 17:36 UTC (permalink / raw)
  To: linux-btrfs

Alexander Skwar <alexanders.mailinglists+nospam@gmail.com> schrieb:

> 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.

You should change the calls of "btrfs subv del" etc to use unabbreviated 
versions of the commands. Otherwise you expose your script to erratic 
behavior as soon as new commands are added to btrfs that make your usage of 
the commands ambigious.

Regards,
Kai


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Script for creating/managing snapshots of all subvolumes of all filesystems
  2013-05-05 17:36 ` Kai Krakow
@ 2013-05-05 19:26   ` Alexander Skwar
  0 siblings, 0 replies; 3+ messages in thread
From: Alexander Skwar @ 2013-05-05 19:26 UTC (permalink / raw)
  To: Kai Krakow; +Cc: linux-btrfs@vger.kernel.org

Hi

On Sun, May 5, 2013 at 7:36 PM, Kai Krakow <hurikhan77+btrfs@gmail.com> wrote:
> Alexander Skwar <alexanders.mailinglists+nospam@gmail.com> schrieb:
>
>> 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.
>
> You should change the calls of "btrfs subv del" etc to use unabbreviated
> versions of the commands. Otherwise you expose your script to erratic

Oh, yes. Very good point. And there really isn't any benefit
in using abbreviated versions of the command names in a
script anyway.

On the command line: Yes, less to type = better.
But in a script?

Thanks. Very good point indeed!

Alexander
--
=>        Google+ => http://plus.skwar.me         <==
=> Chat (Jabber/Google Talk) => a.skwar@gmail.com <==

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-05 19:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-03 11:43 Script for creating/managing snapshots of all subvolumes of all filesystems Alexander Skwar
2013-05-05 17:36 ` Kai Krakow
2013-05-05 19:26   ` Alexander Skwar

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.