# Scheduled Snapshots Not Working



## notfed (Apr 14, 2010)

Hi everyone,

I'm trying to set up cron to store daily snapshots of my entire filesystem, but it's not working out how I'd expect.

I have the partition /usr, and I'd like to store a snapshot of it at /usr/snapshot/snap.  I can do this manually like so:

```
mount -u -o snapshot /usr/snapshot/snap /usr
```

So, I made a script to do this for me for each partition.  It looks like this (repeated for each partition, but let's assume I'm just worried about snapshotting /usr):

```
#!/bin/sh
if [ -f /usr/snapshot/snap ]; then
  rm -f /usr/snapshot/snap_old
  mv /usr/snapshot/snap /usr/snapshot/snap_old
fi
mount -u -o snapshot /usr/snapshot/snap /usr
```

When I run this script, it moves the snapshot /usr/snapshot/snap to /usr/snapshot/snap_old, and then creates a new /usr/snapshot/snap.  It works fine when I run it manually, and usually takes about 1~2 minutes (it's a 25Gb filesystem).

Now, for whatever reason, when I put this into a cron script, it doesn't work how I'd expect it to.  It moves the current snapshot to snap_old, but the mount command doesn't seem to do its job.  Here's the /etc/crontab entry (where /root/backup.sh is the above script) :

```
# Perform daily snapshot at 3:00 AM
0       3       *       *       *       root    /root/backup.sh
```

----OS Information:----

```
$ uname -a
FreeBSD dell.petio.org 7.0-RELEASE FreeBSD 7.0-RELEASE #2: Tue Dec 23 01:38:59 EST 2008     [email]root@dell.petio.org[/email]:/usr/obj/usr/src
/sys/DELL  i386
```


I'd be grateful if anyone could help me out.  I'd be glad to provide more information, but I'm not too sure which log files (if any) would lead to any clues here.


----------



## DutchDaemon (Apr 14, 2010)

Try full path names to the commands in the script, or expand the PATH variable for cron. It's probably missing /sbin.


----------



## jalla (Apr 14, 2010)

There won't be anything useful in logfiles, but any output from the cronjob will be mailed to root so you could check that.

Also be aware that you are reinventing the wheel here. There are some good tools in ports for doing scheduled, rotating snapshots.
sysutils/freebsd-snapshot is the one I use myself, but if you search you'll find others as well.


----------



## notfed (Apr 14, 2010)

DutchDaemon - Thanks, that makes perfect sense, I changed 'mount' to '/sbin/mount', and I think this fixed it.

jalla - Thanks for the tip.  At first glance, I feel skeptical, since I enjoy the simplicity of using the mount command, and anything more complicated seems like it might just slow down my system.  After reading on freebsd-snapshot, though, it looks like you're correct and I should probably start looking into it.


----------



## knarf (Apr 14, 2010)

sysutils/freebsd-snapshot and sysutils/zfs-snapshot-mgmt are nice.

But I'm still looking for a good implementation of doing recursive snapshots with exceptions.


----------



## notfed (Apr 15, 2010)

Follow up: Yes!! After taking DutchDaemon's tip, today I looked in my snapshots folder and the problem has been fixed.  Such a simple, stupid little mistake, but it's been bugging me for a long time!


----------



## DutchDaemon (Apr 15, 2010)

Putting this as the first line in your crontab should generally fix this:


```
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
```

What the default (short) path for cron is can be seen in /etc/crontab (which you shouldn't edit or use for your own jobs).


----------

