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.
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/zfs-replicate.log"
11 lockdir="/tmp/zfs-admin-lock"
19 # Setup our cleanup and exit trap
22 if [ $released_lock_date == 0 ]; then
23 zpool export $destpool
24 /usr/local/etc/bin/usb-drive-power off
25 echo `date` ZFS admin lock released >> $logfile
31 # Auto snapshot every zfs filesystem on the system specified below
32 echo "$date Polling for ZFS admin lock" >> $logfile
34 # Poll for a lock on the zfs subsystem, and make the lock once we can do so
37 if ! mkdir "$lockdir" >/dev/null 2>&1; then
38 # Another zfs admin tool is running.
39 # Wait a random amount of time and try again
40 ransleep=$(($RANDOM % $maxsleeptime))
42 ((attempts=attempts+1))
44 # No other zfs admin tool is running, we can now.
47 if [[ $attempts -gt $maxattempts ]]; then
48 # We've exceeded our maximum while loop count
49 ls -ld $lockdir | /usr/bin/mailx -s "zfs-replicate-all unable to obtain zfs admin lock" $mailto
54 echo "$date ZFS admin lock obtained" >> $logfile
56 # Poweron the destpool and import it
57 /usr/local/etc/bin/usb-drive-power on >> $logfile
58 zpool import $destpool
60 # List the filesystems to replicate
61 # The parent filesystems MUST be listed ahead
62 # of the children filesystems.
63 # Pool root filesystems must end with a slash.
64 $replicate tank/ $destpool
65 $replicate tank/var $destpool
66 $replicate tank/usr $destpool
67 $replicate tank/usr/home $destpool
68 $replicate tank/usr/videos $destpool
69 $replicate tank/usr/local $destpool
70 $replicate tank/usr/local/etc $destpool
71 $replicate tank/usr/local/var $destpool
72 $replicate tank/backup $destpool
74 # Export the destpool and power it down
75 zpool export $destpool
76 /usr/local/etc/bin/usb-drive-power off >> $logfile
80 echo `date` ZFS admin lock released >> $logfile
82 # Parse the log file and extract our backup stats
83 $logfile_parser "$logfile" "$date" >> $logfile
85 # clean things up and exit