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