Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Btrfs automatic snapshots and Samba Shadow Copies
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Gatak
Apprentice
Apprentice


Joined: 04 Jan 2004
Posts: 174

PostPosted: Sun Feb 12, 2017 9:05 am    Post subject: Btrfs automatic snapshots and Samba Shadow Copies Reply with quote

Samba has a VFS module to expose filesystem snapshots as Previous Versions to Windows clients. The official Samba documentation uses LVM volumes, but I think it is more useful if combined with Btrfs snapshots.

I made a script that, when run hourly (or otherwise) detects changes in your subvolume and creates a snapshot. If there are no changes, no snapshots are made.

In /etc/samba/smb.conf
Code:
[share]
path = /mnt/pool/share
comment = Shadow Copy Enabled Share

vfs objects = shadow_copy2
shadow:snapdir = /mnt/pool/snapshots/
shadow:basedir = /mnt/pool/share
shadow:sort = desc
shadow:format = share_@GMT_%Y.%m.%d-%H.%M.%S
shadow:localtime = yes


Then create the snapshot script that you run regularly
Code:
#!/bin/bash
echo Backup started at `date +%Y.%m.%d-%H.%M.%S`
time=@GMT_`date +%Y.%m.%d-%H.%M.%S`

# Mountpoint
mount="/mnt/pool/volumes/home"

## symlink to most recent snapshop
symlink="/mnt/pool/snapshots/home"
old_snap=`readlink "/mnt/pool/snapshots/home"`

## New snapshot name
new_snap=$symlink"_$time"

## Check for most recent generation ID for most recent snapshot.
## This is used when looking for changed files.
if [ -d "$old_snap" ]; then
        # find-new outputs last generation ID when using a too high value is used for comparing.
        gen_id=$(/sbin/btrfs sub find-new "$old_snap" 9999999|cut -d " " -f 4)

        # Count changed files.
        count=`/sbin/btrfs subvolume find-new "$mount" $gen_id | cut -d " " -f 17-1000 | sed '/^$/d'| wc -l`
        /sbin/btrfs subvolume find-new "$mount" $gen_id | cut -d " " -f 17-1000
        # Create a new snapshot if we have changed files.
        if [[ $count > '0' ]]; then
                /sbin/btrfs subvolume snapshot -r "$mount" "$new_snap"

                ## Recreate a symnlink to the new snapshot
                rm "$symlink" && ln -s "$new_snap" "$symlink"
        else
                echo "No files changed, skipping creating of a new snapshot"
        fi
else
        echo Something went wrong. Check that symlink $symlink points to a real snapshot
fi
echo Backup finished at `date +%Y.%m.%d-%H.%M.%S`


This guide is available at
* https://wiki.gentoo.org/wiki/Btrfs_snapshots
* https://wiki.gentoo.org/wiki/Samba_shadow_copies (including screenshots)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum