# ZFS snapshot automatically in bash



## bsus (Sep 17, 2011)

Hi,

I want to write a script which manages automatically my zfs snapshots. The script itself will get started from cron.

I thought it would be the best if there would be a structure like this:

Daily snapshot from each day of the actual month

30-09-2011
...
03-09-2011
02-09-2011
01-09-2011

Weekly snapshot of the month before

29-08-2011
22-08-2011
15-08-2011
08-08-2011
01-08-2011

One snapshot of the two months before before

01-07-2011
01-06-2011

That was the idea.

Now I have some problems with writting this to code:


```
#! /usr/local/bin/bash

# Settings
DATASET="storage/home"
DATE="$(date +%d-%m-%Y)"

zfs snapshot "$DATASET"@"$DATE"
```

I would need something which erases every day except monday from the month before and something which erases every day except the first of the months before before.

The second would be easier
=> I need to "minus" the "%m" of the date command 2 (date +%d-(%m-2)-%Y) and with this variable I could set up a for loop which starts with 2 and end at 31 or the days which the month has.

For the first I need to get the dates of the mondays before the actual month.
Than a for loop has to remove all days except these mondays.

My question is now how I will get this to detail. So how I have to form the date command to use the month before and how to filter the mondays

Hope for help 

Regards


----------



## nORKy (Sep 17, 2011)

Try  sysutils/zfsnap


----------



## bsus (Sep 17, 2011)

To zfSnap:

```
zfSnap -s -S -v -a tt30 storage/home
```
Would this mean that zfSnap creates a snapshot which deletes itself after 30 days?

How does the deletion work?
Does the script work in the background or does it check at the next time where I use it which snaps to delete?

Regards


----------



## graudeejs (Sep 18, 2011)

bsus said:
			
		

> To zfSnap:
> 
> ```
> zfSnap -s -S -v -a tt30 storage/home
> ...



Check the wiki:
https://github.com/graudeejs/zfSnap/wiki

btw [red]-a tt30[/red] is invalid, you should use *-a 30d* for 30 days


----------



## bsus (Sep 19, 2011)

Ok thanks 

works good. But you have to delete them "manually" with 
	
	



```
zfSnap -d
```
 but this is no problem with cron

I now have an last issue. I want to have a softlink from /usr/home/.zfs/snapshot to my home-dir. So I can easily acces over nfs/samba older files of me.

Does this work? And if yes what is the recommand way? I already tried it with

```
ln -s /usr/home/.zfs/snapshot /usr/home/<user>/snapshots
ln: ./snapshots: Operation not supported
```

Regards


----------

