d699c792e424197ecfb61ab9ac1952282b5915b0
[zfs-ubuntu/.git] / zfs-replicate-wrapper
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 # 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"
12 local_pool=backups
13 mailto=root@pippins.net
14 date=`date`
15 starttime=`date +%s`
16 zfs=/usr/sbin/zfs
17
18 # Specify the list of filesystems to replicate
19 filesystems_to_replicate='
20 naspool/www
21 naspool/git
22 '
23
24 # Specify the maximum run time in minutes that this script can run
25 maxruntime=240
26
27 # This function checks to see if our runtime has exceeded our stoptime
28 timeexceeded() { 
29   currenttime=`date +%s`  
30   elapsedtime=$(($currenttime - $starttime))
31   stoptime=$(($maxruntime*60))
32   if [[ $elapsedtime -gt $stoptime ]]; then
33     #echo "$elapsedtime > $stoptime"
34     return 1
35   fi
36   #echo "$elapsedtime < $stoptime"
37   return 0
38 }
39
40 # This function cleanup and exit trap
41 cleanup_and_exit() { 
42   #echo "cleanup and exit"
43   rm -rf "$lockdir"
44   exit 0
45 }
46 trap cleanup_and_exit INT
47
48 # This function executes the replicate command and checks the stoptime
49 replicate() { 
50   $zfsreplicate $*  >> $logfile 2>&1
51   timeexceeded
52   if [ $? == 1 ]; then
53     cleanup_and_exit
54   fi
55 }
56
57 # This function obtains the date a given snapshot was created in epoch seconds
58 snapshot_age() {
59   snapshot=${local_pool}/${1}${previous_backup_marker}
60   $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
61   if [ $? == 0 ]; then
62     $zfs get creation ${snapshot} > /dev/null 2>&1
63     if [ $? == 0 ]; then
64       snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
65       snap_age=`date -d "$snap_creation" +%s` 
66       echo "$snap_age"
67     else
68       echo "0"
69     fi
70   else
71     echo "0"
72   fi
73 }
74
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
78 if [ $? != 0 ]; then
79   echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
80   exit 1
81 fi
82
83 # Setup our backup marker names
84 current_backup_marker=@current-backup-${local_pool_guid}
85 previous_backup_marker=@previous-backup-${local_pool_guid}
86
87 # Auto snapshot every zfs filesystem on the system specified below
88 echo "$date ZFS replicate started" >> $logfile
89
90 # Sort the filesystems to replicate by the oldest backup first
91 tmpfile=`tempfile`
92 for filesystem in $filesystems_to_replicate; do
93   age=`snapshot_age $filesystem`
94   echo $filesystem $age >> $tmpfile
95 done
96 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
97 rm -f $tmpfile
98
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
103 done
104
105 # All done
106 echo `date` ZFS replicate complete >> $logfile
107
108 # Parse the log file and extract our backup stats
109 $logfile_parser "$logfile" "$date" >> $logfile
110