b52e64e05e3ffbb7c8fb77a88af8756013ed61e3
[zfs-ubuntu/.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/zfs-replicate.log"
11 lockdir="/tmp/zfs-admin-lock"
12 mailto=root
13 destpool="backups"
14 maxsleeptime=60
15 maxattempts=600
16 released_lock_date=0
17 date=`date`
18
19 # Setup our cleanup and exit trap
20 cleanup() { 
21   rm -rf "$lockdir"
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
26   fi
27 }
28 trap cleanup EXIT
29
30 # Auto snapshot every zfs filesystem on the system specified below
31 echo "$date Polling for ZFS admin lock" >> $logfile
32
33 # Poll for a lock on the zfs subsystem, and make the lock once we can do so
34 attempts=0
35 while true; do
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))
40     sleep $ransleep
41     ((attempts=attempts+1))
42   else 
43     # No other zfs admin tool is running, we can now.
44     break
45   fi
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
49     exit 1
50   fi
51 done
52 date=`date`;
53 echo "$date ZFS admin lock obtained" >> $logfile
54
55 # Poweron the destpool and import it
56 /usr/local/etc/bin/usb-drive-power on >> $logfile
57 zpool import $destpool
58
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
72
73 # Export the destpool and power it down
74 zpool export $destpool
75 /usr/local/etc/bin/usb-drive-power off >> $logfile
76
77 # Release our lock
78 released_lock_date=1
79 echo `date` ZFS admin lock released >> $logfile
80
81 # Parse the log file and extract our backup stats
82 $logfile_parser "$logfile" "$date" >> $logfile
83