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