0ed35f56a149af0d61f170ea0b0323692b6df8c9
[zfs-ubuntu/.git] / zfs-replicate-all
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 # source our configuration 
8 config="${0%/*}/zfs-scripts.conf"
9 [ -e "${config}.dist" ] && . ${config}.dist
10 [ -e "${config}" ] && . ${config}
11
12 # Setup some default values
13 logfile="$logdir/zfs-replicate.log"
14 mylogfile="$logdir/zfs-replicate-all.log"
15 date=`date`
16 starttime=`date +%s`
17
18 # Make sure we aren't already running
19 SCRIPT_NAME=${0##*/}
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
25    exit 1
26 fi
27 [[ -e "$PROCESS_LIST" ]] && rm -f $PROCESS_LIST
28
29 # This function checks to see if our runtime has exceeded our stoptime
30 timeexceeded() { 
31   if [[ $maxruntime == 0 ]]; then
32     return 0
33   fi
34   currenttime=`date +%s`  
35   elapsedtime=$(($currenttime - $starttime))
36   stoptime=$(($maxruntime*60))
37   if [[ $elapsedtime -gt $stoptime ]]; then
38     #echo "$elapsedtime > $stoptime"
39     return 1
40   fi
41   #echo "$elapsedtime < $stoptime"
42   return 0
43 }
44
45 # This function cleanup and exit trap
46 cleanup_and_exit() { 
47   #echo "cleanup and exit"
48   rm -rf "$lockdir"
49   exit 0
50 }
51 trap cleanup_and_exit INT
52
53 # This function executes the replicate command and checks the stoptime
54 replicate() { 
55   zfs-replicate $*  >> $logfile 2>&1
56   timeexceeded
57   if [ $? == 1 ]; then
58     cleanup_and_exit
59   fi
60 }
61
62 # This function obtains the date a given snapshot was created in epoch seconds
63 snapshot_age() {
64   snapshot=${backup_pool}/${1}${previous_backup_marker}
65   $zfs list -t snapshot ${snapshot} > /dev/null 2>&1
66   if [ $? == 0 ]; then
67     $zfs get creation ${snapshot} > /dev/null 2>&1
68     if [ $? == 0 ]; then
69       snap_creation=`$zfs get creation ${snapshot} | grep $1 | awk '{ print $3" "$4" "$5" "$6" "$7 }'`
70       snap_age=`date -d "$snap_creation" +%s` 
71       echo "$snap_age"
72     else
73       echo "0"
74     fi
75   else
76     echo "0"
77   fi
78 }
79
80 # Import the local backup pool if needed and the option is given to do so, else error out
81 zpool list -H "$backup_pool" >/dev/null 2>&1
82 if [ $? != 0 ]; then
83   if [[ $import_export_backup_pool == 1 ]]; then
84     zpool import $backup_pool
85     if [ $? != 0 ]; then
86       echo "-E- unable to import the backup pool $backup_pool"
87       exit 1
88     fi
89   else 
90     echo "-E- The local backup pool, '$backup_pool' doesn't seem to exist." $mailto
91     exit 1
92   fi
93 fi
94
95 # Obtain the zpool guid for the local backup pool
96 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
97 zpool get guid $backup_pool > /dev/null 2>&1
98 if [ $? != 0 ]; then
99   echo >&2 "-E- Unable to extract the guid for the local backup pool: $backup_pool"
100   exit 1
101 fi
102
103 # Setup our backup marker names
104 current_backup_marker=@current-backup-${backup_pool_guid}
105 previous_backup_marker=@previous-backup-${backup_pool_guid}
106
107 # Auto snapshot every zfs filesystem on the system specified below
108 echo "$date ZFS replicate started" >> $logfile
109 echo "$date ZFS replicate started" | tee -a $mylogfile
110
111 # Sort the filesystems to replicate by the oldest backup first
112 tmpfile=`tempfile`
113 for filesystem in $filesystems_to_replicate; do
114   age=`snapshot_age $filesystem`
115   echo $filesystem $age >> $tmpfile
116 done
117 sorted_filesystems=`cat $tmpfile | sort -n -k 2 | awk '{ print $1 }'`
118 rm -f $tmpfile
119
120 # Replicate the sorted filesystems
121 for filesystem in $sorted_filesystems; do
122   echo "-> Replicating $remote:$filesystem to ${backup_pool}/${filesystem}" | tee -a $mylogfile
123   replicate $remote $filesystem
124 done
125
126 # Export the local pool if told to do so
127 if [[ $import_export_backup_pool == 1 ]]; then
128   zpool export $backup_pool
129   if [ $? != 0 ]; then
130     fatal_and_exit "-E- unable to export the local backup pool $backup_pool"
131   fi
132 fi
133
134 # All done
135 echo `date` ZFS replicate complete >> $logfile
136 echo `date` ZFS replicate complete | tee -a $mylogfile
137
138 # Parse the log file and extract our backup stats
139 zfs-log-parser "$logfile" "$date" >> $logfile
140 zfs-log-parser "$logfile" "$date" | tee -a $mylogfile
141