Added support to replicate and restore from multiple backup pools
[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() {
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" "$mailto"
58 }
59
60 fatal_and_exit() {
61   fatal $*
62   exit 1
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" >/dev/null 2>&1
102 if [ $? != 0 ]; then
103   if [[ $import_export_backup_pool == 1 ]]; then
104     zpool import $backup_pool
105     if [ $? != 0 ]; then
106       fatal_and_exit "-E- unable to import the backup pool $backup_pool on $hostname" "$mailto"
107     fi
108   else 
109     fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." "$mailto"
110   fi
111 fi
112
113 # Obtain the zpool guid for the local backup pool
114 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
115 zpool get guid $backup_pool > /dev/null 2>&1
116 if [ $? != 0 ]; then
117   fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" "$mailto"
118 fi
119
120 # Setup our backup marker names
121 current_backup_marker=@current-backup-${backup_pool_guid}
122 previous_backup_marker=@previous-backup-${backup_pool_guid}
123
124 # Sort the filesystems to replicate by the oldest backup first
125 tmpfile=`tempfile`
126 for filesystem in $filesystems_to_replicate; do
127   if [[ $filesystem =~ ':' ]]; then
128     dst_pool=${filesystem%%:*}
129     filesystem=${filesystem#*:} # remove src_pool from string
130   else
131     dst_pool=$backup_pool
132   fi
133   # Only backup filesystems that are specified to go this backup_pool
134   if [[ $backup_pool == $dst_pool ]]; then
135     age=`snapshot_age $filesystem`
136     echo $filesystem $age >> $tmpfile
137   fi
138 done
139 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
140 rm -f $tmpfile
141
142 # Replicate the sorted filesystems
143 for filesystem in $sorted_filesystems; do
144   echo "-> Replicating $remote:$filesystem to ${backup_pool}/${filesystem}" | tee -a $mylogfile
145   replicate $remote $filesystem $backup_pool
146 done
147
148 # Export the local pool if told to do so
149 if [[ $import_export_backup_pool == 1 ]]; then
150   # Don't export the pool if there is a currently running zfs-scrub operation
151   ps -ef | grep "zfs-scrub" | grep -q "${backup_pool}" | grep -v grep
152   if [ $? != 0 ]; then
153     zpool export $backup_pool
154     if [ $? != 0 ]; then
155       lsof /$backup_pool/*
156       fatal "-E- unable to export the local backup pool $backup_pool on $hostname" "$mailto"
157     fi
158   fi
159 fi
160
161 done
162
163 # All done
164 echo `date` ZFS replicate complete >> $logfile
165 echo `date` ZFS replicate complete | tee -a $mylogfile
166
167 # Parse the log file and extract our backup stats
168 zfs-log-parser "$logfile" "$date" >> $logfile
169 zfs-log-parser "$logfile" "$date" | tee -a $mylogfile
170