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