Changed from looking directly in .zfs for snaps.
[zfs-ubuntu/.git] / zfs-replicate-all
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 tmpfile=`mktemp`
18
19 # Make sure we aren't already running
20 SCRIPT_NAME=${0##*/}
21 PROCESS_LIST=`mktemp`
22 ps -ef | grep -e "$SCRIPT_NAME" | grep -v grep | grep -v tail | grep -v less | grep -v $$ | grep -v $PPID >> $PROCESS_LIST
23 if [[ $? == 0 ]]; then
24    echo "$date Another $SCRIPT_NAME process is already running" >> $mylogfile
25    cat $PROCESS_LIST >> $mylogfile
26    exit 1
27 fi
28 [[ -e "$PROCESS_LIST" ]] && rm -f $PROCESS_LIST
29
30 # This function checks to see if our runtime has exceeded our stoptime
31 timeexceeded() { 
32   if [[ $maxruntime == 0 ]]; then
33     return 0
34   fi
35   currenttime=`date +%s`  
36   elapsedtime=$(($currenttime - $starttime))
37   stoptime=$(($maxruntime*60))
38   if [[ $elapsedtime -gt $stoptime ]]; then
39     #echo "$elapsedtime > $stoptime"
40     return 1
41   fi
42   #echo "$elapsedtime < $stoptime"
43   return 0
44 }
45
46 # This function cleanup and exit trap
47 cleanup_and_exit() { 
48   #echo "cleanup and exit"
49   rm -rf "$lockdir"
50   exit 0
51 }
52 trap cleanup_and_exit INT
53
54 fatal_and_exit() {
55   # echo message to terminal
56   echo -e 2>&1 "$1"
57   # send email notification
58   echo -e "$1" | $mailx -s "zfs-replicate-all on $hostname failed" "$2"
59   # exit with bad return code unless 3rd argument was defined that says not to
60   if [[ -z "$3" ]]; then
61     exit 1
62   fi
63 }
64
65 # This function executes the replicate command and checks the stoptime
66 replicate() { 
67   zfs-replicate $*  >> $logfile 2>&1
68   timeexceeded
69   if [ $? == 1 ]; then
70     cleanup_and_exit
71   fi
72 }
73
74 # This function obtains the date a given snapshot was created in epoch seconds
75 snapshot_age() {
76   snapshot=${backup_pool}/${1}${previous_backup_marker}
77   $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
78   if [ $? == 0 ]; then
79     $zfs get creation ${snapshot} > /dev/null 2>&1
80     if [ $? == 0 ]; then
81       snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
82       snap_age=`date -d "$snap_creation" +%s` 
83       echo "$snap_age"
84     else
85       echo "0"
86     fi
87   else
88     echo "0"
89   fi
90 }
91
92 # Replicate every zfs filesystem specified in the config file
93 echo "$date ZFS replicate started" >> $logfile
94 echo "$date ZFS replicate started" | tee -a $mylogfile
95
96 # Loop over each backup pool
97 backup_pools=$backup_pool
98 for backup_pool in $backup_pools; do
99
100 # Import the local backup pool if needed and the option is given to do so, else error out
101 zpool list -H $backup_pool > $tmpfile 2>&1
102 if [ $? != 0 ]; then
103   if [[ $import_export_backup_pool == 1 ]]; then
104     zpool import -f $backup_pool >> $tmpfile 2>&1
105     if [ $? != 0 ]; then
106       msgs=`cat $tmpfile`
107       fatal_and_exit "-E- unable to import the backup pool '$backup_pool' on $hostname: $msgs" "$mailto"
108     fi
109   else 
110     msgs=`cat $tmpfile`
111     fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist: $msgs" "$mailto"
112   fi
113 fi
114 rm -f $tmpfile
115
116 # Obtain the zpool guid for the local backup pool
117 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
118 zpool get guid $backup_pool > /dev/null 2>&1
119 if [ $? != 0 ]; then
120   fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" "$mailto"
121 fi
122
123 # Setup our backup marker names
124 current_backup_marker=@current-backup-${backup_pool_guid}
125 previous_backup_marker=@previous-backup-${backup_pool_guid}
126
127 # Sort the filesystems to replicate by the oldest backup first
128 for filesystem in $filesystems_to_replicate; do
129   if [[ $filesystem =~ ':' ]]; then
130     dst_pool=${filesystem%%:*}
131     filesystem=${filesystem#*:} # remove src_pool from string
132   else
133     dst_pool=$backup_pool
134   fi
135   # Only backup filesystems that are specified to go this backup_pool
136   if [[ $backup_pool == $dst_pool ]]; then
137     age=`snapshot_age $filesystem`
138     echo $filesystem $age >> $tmpfile
139   fi
140 done
141 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
142 rm -f $tmpfile
143
144 # Replicate the sorted filesystems
145 for filesystem in $sorted_filesystems; do
146   echo "-> Replicating $remote:$filesystem to ${backup_pool}/${filesystem}" | tee -a $mylogfile
147   replicate $remote $filesystem $backup_pool
148 done
149
150 # Export the local pool if told to do so
151 if [[ $import_export_backup_pool == 1 ]]; then
152   # Don't export the pool if there is a currently running zfs-scrub operation
153   ps -ef | grep "zfs-scrub" | grep -q "${backup_pool}" | grep -v grep
154   if [ $? != 0 ]; then
155     zpool export $backup_pool >> $tmpfile 2>&1
156     if [ $? != 0 ]; then
157       sleep 300
158       zpool export $backup_pool >> $tmpfile 2>&1
159       if [ $? != 0 ]; then
160         lsof /$backup_pool/* >> $tmpfile 2>&1
161         msgs=`cat $tmpfile`
162         fatal_and_exit "-E- unable to export the local backup pool $backup_pool on $hostname: $msgs" "$mailto" 0
163       fi
164     fi
165   fi
166 fi
167 rm -f $tmpfile
168
169 done
170
171 # All done
172 echo `date` ZFS replicate complete >> $logfile
173 echo `date` ZFS replicate complete | tee -a $mylogfile
174
175 # Parse the log file and extract our backup stats
176 zfs-log-parser "$logfile" "$date" >> $logfile
177 zfs-log-parser "$logfile" "$date" | tee -a $mylogfile
178