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 # Setup some default values
8 zfsreplicate="/etc/bin/zfs-replicate"
9 logfile_parser="/etc/bin/zfs-log-parser"
10 logfile="/var/log/zfs/zfs-replicate.log"
11 remote="tank.pippins.net"
13 mailto=root@pippins.net
18 # Specify the list of filesystems to replicate
19 filesystems_to_replicate='
24 # Specify the maximum run time in minutes that this script can run
27 # This function checks to see if our runtime has exceeded our stoptime
29 currenttime=`date +%s`
30 elapsedtime=$(($currenttime - $starttime))
31 stoptime=$(($maxruntime*60))
32 if [[ $elapsedtime -gt $stoptime ]]; then
33 #echo "$elapsedtime > $stoptime"
36 #echo "$elapsedtime < $stoptime"
40 # This function cleanup and exit trap
42 #echo "cleanup and exit"
46 trap cleanup_and_exit INT
48 # This function executes the replicate command and checks the stoptime
50 $zfsreplicate $* >> $logfile 2>&1
57 # This function obtains the date a given snapshot was created in epoch seconds
59 snapshot=${local_pool}/${1}${previous_backup_marker}
60 $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
62 $zfs get creation ${snapshot} > /dev/null 2>&1
64 snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
65 snap_age=`date -d "$snap_creation" +%s`
75 # Obtain the zpool guid for the local pool
76 local_pool_guid=`zpool get guid $local_pool 2>&1 | grep $local_pool | awk '{ print $3 }'`
77 zpool get guid $local_pool > /dev/null 2>&1
79 echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
83 # Setup our backup marker names
84 current_backup_marker=@current-backup-${local_pool_guid}
85 previous_backup_marker=@previous-backup-${local_pool_guid}
87 # Auto snapshot every zfs filesystem on the system specified below
88 echo "$date ZFS replicate started" >> $logfile
90 # Sort the filesystems to replicate by the oldest backup first
92 for filesystem in $filesystems_to_replicate; do
93 age=`snapshot_age $filesystem`
94 echo $filesystem $age >> $tmpfile
96 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
99 # Replicate the sorted filesystems
100 for filesystem in $sorted_filesystems; do
101 echo "-> Replicating $remote:$filesystem to ${local_pool}/${filesystem}"
102 replicate $remote $filesystem
106 echo `date` ZFS replicate complete >> $logfile
108 # Parse the log file and extract our backup stats
109 $logfile_parser "$logfile" "$date" >> $logfile