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.
7 # source our configuration
8 config="${0%/*}/zfs-scripts.conf"
9 [ -e "${config}.dist" ] && . ${config}.dist
10 [ -e "${config}" ] && . ${config}
12 # Setup some default values
13 logfile="$logdir/zfs-replicate.log"
14 mylogfile="$logdir/zfs-replicate-all.log"
19 # Make sure we aren't already running
21 PROCESS_LIST=`tempfile`
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
28 [[ -e "$PROCESS_LIST" ]] && rm -f $PROCESS_LIST
30 # This function checks to see if our runtime has exceeded our stoptime
32 if [[ $maxruntime == 0 ]]; then
35 currenttime=`date +%s`
36 elapsedtime=$(($currenttime - $starttime))
37 stoptime=$(($maxruntime*60))
38 if [[ $elapsedtime -gt $stoptime ]]; then
39 #echo "$elapsedtime > $stoptime"
42 #echo "$elapsedtime < $stoptime"
46 # This function cleanup and exit trap
48 #echo "cleanup and exit"
52 trap cleanup_and_exit INT
55 # echo message to terminal
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
65 # This function executes the replicate command and checks the stoptime
67 zfs-replicate $* >> $logfile 2>&1
74 # This function obtains the date a given snapshot was created in epoch seconds
76 snapshot=${backup_pool}/${1}${previous_backup_marker}
77 $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
79 $zfs get creation ${snapshot} > /dev/null 2>&1
81 snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
82 snap_age=`date -d "$snap_creation" +%s`
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
96 # Loop over each backup pool
97 backup_pools=$backup_pool
98 for backup_pool in $backup_pools; do
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
103 if [[ $import_export_backup_pool == 1 ]]; then
104 zpool import $backup_pool >> $tmpfile 2>&1
107 fatal_and_exit "-E- unable to import the backup pool '$backup_pool' on $hostname: $msgs" "$mailto"
111 fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist: $msgs" "$mailto"
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
120 fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" "$mailto"
123 # Setup our backup marker names
124 current_backup_marker=@current-backup-${backup_pool_guid}
125 previous_backup_marker=@previous-backup-${backup_pool_guid}
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
133 dst_pool=$backup_pool
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
141 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
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
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
155 zpool export $backup_pool >> $tmpfile 2>&1
158 zpool export $backup_pool >> $tmpfile 2>&1
160 lsof /$backup_pool/* >> $tmpfile 2>&1
162 fatal_and_exit "-E- unable to export the local backup pool $backup_pool on $hostname: $msgs" "$mailto" 0
172 echo `date` ZFS replicate complete >> $logfile
173 echo `date` ZFS replicate complete | tee -a $mylogfile
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