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"
18 # Make sure we aren't already running
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
27 [[ -e "$PROCESS_LIST" ]] && rm -f $PROCESS_LIST
29 # This function checks to see if our runtime has exceeded our stoptime
31 if [[ $maxruntime == 0 ]]; then
34 currenttime=`date +%s`
35 elapsedtime=$(($currenttime - $starttime))
36 stoptime=$(($maxruntime*60))
37 if [[ $elapsedtime -gt $stoptime ]]; then
38 #echo "$elapsedtime > $stoptime"
41 #echo "$elapsedtime < $stoptime"
45 # This function cleanup and exit trap
47 #echo "cleanup and exit"
51 trap cleanup_and_exit INT
54 # echo message to terminal
56 # send email notification
57 echo -e "$1" | $mailx -s "zfs-replicate-all on $hostname failed" "$mailto"
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 # Import the local backup pool if needed and the option is given to do so, else error out
93 zpool list -H "$backup_pool" >/dev/null 2>&1
95 if [[ $import_export_backup_pool == 1 ]]; then
96 zpool import $backup_pool
98 fatal_and_exit "-E- unable to import the backup pool $backup_pool on $hostname" "$mailto"
101 fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." "$mailto"
105 # Obtain the zpool guid for the local backup pool
106 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
107 zpool get guid $backup_pool > /dev/null 2>&1
109 fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" "$mailto"
112 # Setup our backup marker names
113 current_backup_marker=@current-backup-${backup_pool_guid}
114 previous_backup_marker=@previous-backup-${backup_pool_guid}
116 # Auto snapshot every zfs filesystem on the system specified below
117 echo "$date ZFS replicate started" >> $logfile
118 echo "$date ZFS replicate started" | tee -a $mylogfile
120 # Sort the filesystems to replicate by the oldest backup first
122 for filesystem in $filesystems_to_replicate; do
123 age=`snapshot_age $filesystem`
124 echo $filesystem $age >> $tmpfile
126 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
129 # Replicate the sorted filesystems
130 for filesystem in $sorted_filesystems; do
131 echo "-> Replicating $remote:$filesystem to ${backup_pool}/${filesystem}" | tee -a $mylogfile
132 replicate $remote $filesystem
135 # Export the local pool if told to do so
136 if [[ $import_export_backup_pool == 1 ]]; then
137 # Don't export the pool if there is a currently running zfs-scrub operation
138 ps -ef | grep "zfs-scrub" | grep -q "${backup_pool}" | grep -v grep
140 zpool export $backup_pool
143 fatal("-E- unable to export the local backup pool $backup_pool on $hostname")
149 echo `date` ZFS replicate complete >> $logfile
150 echo `date` ZFS replicate complete | tee -a $mylogfile
152 # Parse the log file and extract our backup stats
153 zfs-log-parser "$logfile" "$date" >> $logfile
154 zfs-log-parser "$logfile" "$date" | tee -a $mylogfile