Moved pool names to variable
[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/ext-drive-power off
25     echo `date` ZFS admin lock released >> $logfile
26   fi
27   exit
28 }
29 trap cleanup INT
30
31 # Auto snapshot every zfs filesystem on the system specified below
32 echo "$date Polling for ZFS admin lock" >> $logfile
33
34 # Poll for a lock on the zfs subsystem, and make the lock once we can do so
35 attempts=0
36 while true; do
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))
41     sleep $ransleep
42     ((attempts=attempts+1))
43   else 
44     # No other zfs admin tool is running, we can now.
45     break
46   fi
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
50     exit 1
51   fi
52 done
53 date=`date`;
54 echo "$date ZFS admin lock obtained" >> $logfile
55
56 # Poweron the destpool and import it
57 /usr/local/etc/bin/ext-drive-power on >> $logfile
58 zpool import $destpool
59
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
73
74 # Export the destpool and power it down
75 zpool export $destpool
76 /usr/local/etc/bin/ext-drive-power off >> $logfile
77
78 # Release our lock
79 released_lock_date=1
80 echo `date` ZFS admin lock released >> $logfile
81
82 # Parse the log file and extract our backup stats
83 $logfile_parser "$logfile" "$date" >> $logfile
84
85 # clean things up and exit
86 cleanup
87