I decided to change the name of the example script again.
[zfs-nexenta/.git] / zfs-replicate-wrapper
1 #!/bin/bash
2
3 # Author: Alan J. Pippin
4 # Description: This script calls zfs-replicate for each filesystem needing
5 #              to be backed up, or replicated, to another ZFS pool.
6
7 # Setup some default values
8 replicate="/usr/local/etc/bin/zfs-replicate"
9 logfile_parser="/usr/local/etc/bin/zfs-log-parser"
10 logfile="/var/log/zfs-replicate.log"
11 lockdir="/tmp/zfs-admin-lock"
12 destpool="backups"
13 maxsleeptime=60
14 released_lock_date=0
15
16 # Setup our cleanup and exit trap
17 cleanup() { 
18   rm -rf "$lockdir"
19   if [ $released_lock_date == 0 ]; then 
20     echo `date` ZFS admin lock released >> $logfile
21   fi
22 }
23 trap cleanup EXIT
24
25 # Auto snapshot every zfs filesystem on the system specified below
26 date=`date`;
27 echo "$date Polling for ZFS admin lock" >> $logfile
28
29 # Poll for a lock on the zfs subsystem, and make the lock once we can do so
30 while true; do
31   if ! mkdir "$lockdir" >/dev/null 2>&1; then
32     # Another zfs admin tool is running.
33     # Wait a random amount of time and try again
34     ransleep=$(($RANDOM % $maxsleeptime))
35     sleep $ransleep
36   else 
37     # No other zfs admin tool is running, we can now.
38     break
39   fi
40 done
41 date=`date`;
42 echo "$date ZFS admin lock obtained" >> $logfile
43
44 # List the filesystems to replicate
45 # The parent filesystems MUST be listed ahead
46 # of the children filesystems.
47 # Pool root filesystems must end with a slash.
48 $replicate tank/ $destpool
49 $replicate tank/usr $destpool
50 $replicate tank/usr/home $destpool
51 $replicate tank/usr/videos $destpool
52 $replicate tank/usr/local $destpool
53 $replicate tank/usr/local/etc $destpool
54 $replicate tank/backup $destpool
55
56 # Release our lock
57 released_lock_date=1
58 echo `date` ZFS admin lock released >> $logfile
59
60 # Parse the log file and extract our backup stats
61 $logfile_parser "$logfile" "$date" >> $logfile