You are not logged in.

#1 2011-10-31 16:45:37

gspe
Member
Registered: 2011-10-15
Posts: 12

Manage btrfs snapshots

One of the feature that i much like about btrfs is Snapshot.

What i want to do is to create snapshots of /home every 5/15 minutes (i have to test better the impact on performance) and retain:

02 - Yearly snapshots
12 - Monthly snapshots
16 - Weekly snapshots
28 - Daily snapshots
48 - Hourly snapshots
60 - Minutely snapshots

The above scheme is indicative and i'm looking for suggestion in order to improve  it.


After long google searching finally i've found a very simple but powerful bash script to manage btrfs snapshots: https://github.com/mmehnert/btrfs-snapshot-rotation

I've changed a bit the script in order to use my naming scheme

#!/bin/bash


# Parse arguments:
SOURCE=$1
TARGET=$2
SNAP=$3
COUNT=$4
QUIET=$5

# Function to display usage:
usage() {
    scriptname=`/usr/bin/basename $0`
cat <<EOF
$scriptname: Take and rotate snapshots on a btrfs file system

  Usage:
  $scriptname source target snap_name count [-q]
 
  source:    path to make snaphost of
  target:    snapshot directory
  snap_name: Base name for snapshots, to be appended to 
             date "+%F--%H-%M-%S"
  count:     Number of snapshots in the timestamp-@snap_name format to
             keep at one time for a given snap_name. 
  [-q]:      Be quiet.

Example for crontab:
15,30,45  * * * *   root    /usr/local/bin/btrfs-snapshot /home /home/__snapshots quarterly 4 -q
0         * * * *   root    /usr/local/bin/btrfs-snapshot /home /home/__snapshots hourly 8 -q

Example for anacrontab:
1             10      daily_snap      /usr/local/bin/btrfs-snapshot /home /home/__snapshots daily 8
7             30      weekly_snap     /usr/local/bin/btrfs-snapshot /home /home/__snapsnots weekly 5
@monthly      90      monthly_snap    /usr/local/bin/btrfs-snapshot /home /home/__snapshots monthly 3


EOF
    exit
}

# Basic argument checks:
if [ -z $COUNT ] ; then
    echo "COUNT is not provided."
    usage
fi

if [ ! -z $6 ] ; then
    echo "Too many options."
    usage
fi

if [ -n "$QUIET" ] && [ "x$QUIET" != "x-q"  ] ; then
    echo "Option 4 is either -q or empty. Given: \"$QUIET\""
    usage
fi


# $max_snap is the highest number of snapshots that will be kept for $SNAP.
max_snap=$(($COUNT -1))

# $time_stamp is the date of snapshots
time_stamp=`date "+%F_%H-%M"`

# Clean up older snapshots:
for i in `ls $TARGET|sort |grep ${SNAP}|head -n -${max_snap}`; do
    cmd="btrfs subvolume delete   $TARGET/$i"
    if [ -z $QUIET ]; then
        echo $cmd
    fi
    $cmd >/dev/null
done


# Create new snapshot:
cmd="btrfs subvolume snapshot $SOURCE $TARGET/"${SNAP}-$time_stamp""
if [ -z $QUIET ]; then
    echo $cmd
fi
$cmd >/dev/null

I use fcontab

[root@kabuky ~]# fcrontab -l
17:32:58 listing root's fcrontab
@ 5	/usr/local/bin/btrfs-snapshot /home /home/__snapshots minutely	60 -q
@ 1h	/usr/local/bin/btrfs-snapshot /home /home/__snapshots hourly	48 -q
@ 1d	/usr/local/bin/btrfs-snapshot /home /home/__snapshots daily	28 -q
@ 1w	/usr/local/bin/btrfs-snapshot /home /home/__snapshots weekly	16 -q
@ 1m	/usr/local/bin/btrfs-snapshot /home /home/__snapshots monthly	12 -q

And this is what i get

[root@kabuky ~]# ls /home/__snapshots
[root@kabuky ~]# ls -l /home/__snapshots
total 0
drwxr-xr-x 1 root root 30 Jul 15 19:00 hourly-2011-10-31_17-00
drwxr-xr-x 1 root root 30 Jul 15 19:00 hourly-2011-10-31_17-44
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-18
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-20
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-22
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-24
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-26
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-32
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-37
drwxr-xr-x 1 root root 30 Jul 15 19:00 minutely-2011-10-31_17-42

Offline

#2 2011-10-31 16:56:15

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: Manage btrfs snapshots

Is there an issue or is this just an informational post?

Offline

#3 2011-10-31 17:13:55

gspe
Member
Registered: 2011-10-15
Posts: 12

Re: Manage btrfs snapshots

tavianator wrote:

Is there an issue or is this just an informational post?

It's just an informational post.

Offline

#4 2011-10-31 22:20:41

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: Manage btrfs snapshots

Cool, I just wasn't sure if the "And this is what i get" was what you were expecting.

Looks good, I may take a look since I'm using btrfs.  Don't confuse this for a backup though smile.

Offline

#5 2011-11-01 06:57:19

gspe
Member
Registered: 2011-10-15
Posts: 12

Re: Manage btrfs snapshots

tavianator wrote:

Cool, I just wasn't sure if the "And this is what i get" was what you were expecting.

Looks good, I may take a look since I'm using btrfs.  Don't confuse this for a backup though smile.

Sorry for confusion, my english is not very good smile

Yes this is not a backup.
It's a sort of parachute if you do something stupid with your files you can at any time come back

Offline

#6 2011-11-21 20:28:10

richardmurri
Member
Registered: 2008-10-29
Posts: 39

Re: Manage btrfs snapshots

I did this for a year.  Be aware it can kill the performance of your file system to keep snapshot around for a long time.  I'm still not sure why, but I suspect it has to do with fragmented files.

Offline

Board footer

Powered by FluxBB