Added replicate wrapper logfile.
[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 zfsreplicate="/etc/bin/zfs-replicate"
9 logdir="/var/log/zfs"
10 logfile_parser="/etc/bin/zfs-log-parser"
11 logfile="$logdir/zfs-replicate.log"
12 mylogfile="$logdir/zfs-replicate-all.log"
13 remote="tank.pippins.net"
14 local_pool=backups
15 mailto=root@pippins.net
16 date=`date`
17 starttime=`date +%s`
18 zfs=/usr/sbin/zfs
19
20 # Specify the list of filesystems to replicate
21 filesystems_to_replicate='
22 naspool/www
23 naspool/git
24 '
25
26 # Specify the maximum run time in minutes that this script can run (0=no limit)
27 maxruntime=240
28
29 # This function checks to see if our runtime has exceeded our stoptime
30 timeexceeded() { 
31   if [[ $maxruntime == 0 ]]; then
32     return 0
33   fi
34   currenttime=`date +%s`  
35   elapsedtime=$(($currenttime - $starttime))
36   stoptime=$(($maxruntime*60))
37   if [[ $elapsedtime -gt $stoptime ]]; then
38     #echo "$elapsedtime > $stoptime"
39     return 1
40   fi
41   #echo "$elapsedtime < $stoptime"
42   return 0
43 }
44
45 # This function cleanup and exit trap
46 cleanup_and_exit() { 
47   #echo "cleanup and exit"
48   rm -rf "$lockdir"
49   exit 0
50 }
51 trap cleanup_and_exit INT
52
53 # This function executes the replicate command and checks the stoptime
54 replicate() { 
55   $zfsreplicate $*  >> $logfile 2>&1
56   timeexceeded
57   if [ $? == 1 ]; then
58     cleanup_and_exit
59   fi
60 }
61
62 # This function obtains the date a given snapshot was created in epoch seconds
63 snapshot_age() {
64   snapshot=${local_pool}/${1}${previous_backup_marker}
65   $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
66   if [ $? == 0 ]; then
67     $zfs get creation ${snapshot} > /dev/null 2>&1
68     if [ $? == 0 ]; then
69       snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
70       snap_age=`date -d "$snap_creation" +%s` 
71       echo "$snap_age"
72     else
73       echo "0"
74     fi
75   else
76     echo "0"
77   fi
78 }
79
80 # Obtain the zpool guid for the local pool
81 local_pool_guid=`zpool get guid $local_pool 2>&1 | grep $local_pool | awk '{ print $3 }'`
82 zpool get guid $local_pool > /dev/null 2>&1
83 if [ $? != 0 ]; then
84   echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
85   exit 1
86 fi
87
88 # Setup our backup marker names
89 current_backup_marker=@current-backup-${local_pool_guid}
90 previous_backup_marker=@previous-backup-${local_pool_guid}
91
92 # Auto snapshot every zfs filesystem on the system specified below
93 echo "$date ZFS replicate started" >> $logfile
94 echo "$date ZFS replicate started" | tee -a $mylogfile
95
96 # Sort the filesystems to replicate by the oldest backup first
97 tmpfile=`tempfile`
98 for filesystem in $filesystems_to_replicate; do
99   age=`snapshot_age $filesystem`
100   echo $filesystem $age >> $tmpfile
101 done
102 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
103 rm -f $tmpfile
104
105 # Replicate the sorted filesystems
106 for filesystem in $sorted_filesystems; do
107   echo "-> Replicating $remote:$filesystem to ${local_pool}/${filesystem}" | tee -a $mylogfile
108   replicate $remote $filesystem
109 done
110
111 # All done
112 echo `date` ZFS replicate complete >> $logfile
113 echo `data` ZFS replicate complete | tee -a $mylogfile
114
115 # Parse the log file and extract our backup stats
116 $logfile_parser "$logfile" "$date" >> $logfile
117 $logfile_parser "$logfile" "$date" | tee -a $mylogfile
118