Added code to poweron the destpool and import it.
[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 # Poweron the destpool and import it
45 /usr/local/bin/br -x /dev/ttyd0 A3 ON
46 sleep 10
47 zpool import $destpool
48
49 # List the filesystems to replicate
50 # The parent filesystems MUST be listed ahead
51 # of the children filesystems.
52 # Pool root filesystems must end with a slash.
53 $replicate tank/ $destpool
54 $replicate tank/var $destpool
55 $replicate tank/usr $destpool
56 $replicate tank/usr/home $destpool
57 $replicate tank/usr/videos $destpool
58 $replicate tank/usr/local $destpool
59 $replicate tank/usr/local/etc $destpool
60 $replicate tank/usr/local/var $destpool
61 $replicate tank/backup $destpool
62
63 # Export the destpool and power it down
64 zpool export $destpool
65 /usr/local/bin/br -x /dev/ttyd0 A3 OFF
66
67 # Release our lock
68 released_lock_date=1
69 echo `date` ZFS admin lock released >> $logfile
70
71 # Parse the log file and extract our backup stats
72 $logfile_parser "$logfile" "$date" >> $logfile