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 scrub on $hostname failed" "$mailto"
61 # This function executes the replicate command and checks the stoptime
63 zfs-replicate $* >> $logfile 2>&1
70 # This function obtains the date a given snapshot was created in epoch seconds
72 snapshot=${backup_pool}/${1}${previous_backup_marker}
73 $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
75 $zfs get creation ${snapshot} > /dev/null 2>&1
77 snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
78 snap_age=`date -d "$snap_creation" +%s`
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
91 if [[ $import_export_backup_pool == 1 ]]; then
92 zpool import $backup_pool
94 fatal_and_exit "-E- unable to import the backup pool $backup_pool" "$mailto"
97 fatal_and_exit "-E- The local backup pool, '$backup_pool' doesn't seem to exist." "$mailto"
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
105 fatal_and_exit "-E- Unable to extract the guid for the local backup pool: $backup_pool" "$mailto"
108 # Setup our backup marker names
109 current_backup_marker=@current-backup-${backup_pool_guid}
110 previous_backup_marker=@previous-backup-${backup_pool_guid}
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
116 # Sort the filesystems to replicate by the oldest backup first
118 for filesystem in $filesystems_to_replicate; do
119 age=`snapshot_age $filesystem`
120 echo $filesystem $age >> $tmpfile
122 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
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
131 # Export the local pool if told to do so
132 if [[ $import_export_backup_pool == 1 ]]; then
133 zpool export $backup_pool
135 fatal_and_exit "-E- unable to export the local backup pool $backup_pool" "$mailto"
140 echo `date` ZFS replicate complete >> $logfile
141 echo `date` ZFS replicate complete | tee -a $mylogfile
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