Added hostname to error messages that are emailed
[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 scrub on $hostname failed" "$mailto"
58   exit 1
59 }
60
61 # This function executes the replicate command and checks the stoptime
62 replicate() { 
63   zfs-replicate $*  >> $logfile 2>&1
64   timeexceeded
65   if [ $? == 1 ]; then
66     cleanup_and_exit
67   fi
68 }
69
70 # This function obtains the date a given snapshot was created in epoch seconds
71 snapshot_age() {
72   snapshot=${backup_pool}/${1}${previous_backup_marker}
73   $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
74   if [ $? == 0 ]; then
75     $zfs get creation ${snapshot} > /dev/null 2>&1
76     if [ $? == 0 ]; then
77       snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
78       snap_age=`date -d "$snap_creation" +%s` 
79       echo "$snap_age"
80     else
81       echo "0"
82     fi
83   else
84     echo "0"
85   fi
86 }
87
88 # Import the local backup pool if needed and the option is given to do so, else error out
89 zpool list -H "$backup_pool" >/dev/null 2>&1
90 if [ $? != 0 ]; then
91   if [[ $import_export_backup_pool == 1 ]]; then
92     zpool import $backup_pool
93     if [ $? != 0 ]; then
94       fatal_and_exit "-E- unable to import the backup pool $backup_pool on $hostname" "$mailto"
95     fi
96   else 
97     fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." "$mailto"
98   fi
99 fi
100
101 # Obtain the zpool guid for the local backup pool
102 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
103 zpool get guid $backup_pool > /dev/null 2>&1
104 if [ $? != 0 ]; then
105   fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" "$mailto"
106 fi
107
108 # Setup our backup marker names
109 current_backup_marker=@current-backup-${backup_pool_guid}
110 previous_backup_marker=@previous-backup-${backup_pool_guid}
111
112 # Auto snapshot every zfs filesystem on the system specified below
113 echo "$date ZFS replicate started" >> $logfile
114 echo "$date ZFS replicate started" | tee -a $mylogfile
115
116 # Sort the filesystems to replicate by the oldest backup first
117 tmpfile=`tempfile`
118 for filesystem in $filesystems_to_replicate; do
119   age=`snapshot_age $filesystem`
120   echo $filesystem $age >> $tmpfile
121 done
122 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
123 rm -f $tmpfile
124
125 # Replicate the sorted filesystems
126 for filesystem in $sorted_filesystems; do
127   echo "-> Replicating $remote:$filesystem to ${backup_pool}/${filesystem}" | tee -a $mylogfile
128   replicate $remote $filesystem
129 done
130
131 # Export the local pool if told to do so
132 if [[ $import_export_backup_pool == 1 ]]; then
133   zpool export $backup_pool
134   if [ $? != 0 ]; then
135     fatal_and_exit "-E- unable to export the local backup pool $backup_pool on $hostname" "$mailto"
136   fi
137 fi
138
139 # All done
140 echo `date` ZFS replicate complete >> $logfile
141 echo `date` ZFS replicate complete | tee -a $mylogfile
142
143 # Parse the log file and extract our backup stats
144 zfs-log-parser "$logfile" "$date" >> $logfile
145 zfs-log-parser "$logfile" "$date" | tee -a $mylogfile
146