source: src-sh/lpreserver/backend/runsnap.sh @ e9724ed

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since e9724ed was e9724ed, checked in by Kris Moore <kris@…>, 8 months ago

If the user enabled replication, and then disabled it at a later time, resume
usual snapshot pruning.

  • Property mode set to 100755
File size: 6.2 KB
Line 
1#!/bin/sh
2# Do the cron snapshot
3######################################################################
4
5# Set our vars
6PROGDIR="/usr/local/share/lpreserver"
7
8# Source our functions
9. /usr/local/share/pcbsd/scripts/functions.sh
10. ${PROGDIR}/backend/functions.sh
11
12do_auto_prune() {
13  echo_log "Pruning old snapshot: $2"
14  rmZFSSnap "$1" "$2"
15  if [ $? -ne 0 ] ; then
16     echo_log "ERROR: Failed pruning snapshot $2 on ${1}"
17     queue_msg "ERROR: Failed pruning snapshot $2 on ${1} @ `date`\n\r`cat $CMDLOG`"
18     snapStat=1
19  else
20     queue_msg "Success pruning snapshot $2 on ${1} @ `date`\n\r`cat $CMDLOG`"
21  fi
22}
23
24do_numeric_prune()
25{
26
27  # Get our list of snaps
28  snaps=$(snaplist "${DATASET}")
29
30  # Reverse the list
31  for tmp in $snaps
32  do
33     rSnaps="$tmp $rSnaps"
34  done
35
36  # Do any pruning
37  num=0
38  for snap in $rSnaps
39  do
40     # Only remove snapshots which are auto-created, so we don't delete one the user
41     # made specifically
42     cur="`echo $snap | cut -d '-' -f 1`"
43     if [ "$cur" != "auto" ] ; then continue; fi
44
45     num=`expr $num + 1`
46     if [ $num -gt $KEEP ] ; then
47        do_auto_prune "$DATASET" "$snap"
48     fi
49  done
50}
51
52# Function to do automatic rotation / pruning
53do_automatic_prune()
54{
55  curEpoc=`date +%s`
56  lastYear=""; lastMon=""; lastDay=""; lastHour="" lastMin="" ; lastSec=""
57  # Get our list of snaps
58  snaps=$(snaplist "${DATASET}")
59
60  # Reverse the list, sort from newest to oldest
61  for tmp in $snaps
62  do
63     rSnaps="$tmp $rSnaps"
64  done
65
66  # Get the last replicated snapshot
67  lastSEND=`zfs get -r backup:lpreserver ${LDATA} | grep LATEST | awk '{$1=$1}1' OFS=" " | tail -1 | cut -d '@' -f 2 | cut -d ' ' -f 1`
68  if [ -n "$lastSend" ] ; then
69     sec="`echo $lastSend | cut -d '-' -f 7`"
70     min="`echo $lastSend | cut -d '-' -f 6`"
71     hour="`echo $lastSend | cut -d '-' -f 5`"
72     day="`echo $lastSend | cut -d '-' -f 4`"
73     mon="`echo $lastSend | cut -d '-' -f 3`"
74     year="`echo $lastSend | cut -d '-' -f 2`"
75     sendEpoc=`date -j -f "%Y %m %d %H %M %S" "$year $mon $day $hour $min $sec" "+%s"`
76     # Check that this replication target is still active
77     if [ -e "$REPCONF" ] ; then
78       cat ${REPCONF} | grep -q "^${LDATA}:"
79       if [ $? -ne 0 ] ; then
80          unset lastSEND
81          unset sendEpoc
82       fi
83     fi
84  fi
85
86  num=0
87  for snap in $rSnaps
88  do
89     # Skip snaps not created by life-preserver
90     cur="`echo $snap | cut -d '-' -f 1`"
91     if [ "$cur" != "auto" ] ; then continue; fi
92
93     # If this snapshot is the last one replicated, lets skip pruning it for now
94     if [ "$cur" = "$lastSEND" ]; then continue; fi
95
96     sec="`echo $snap | cut -d '-' -f 7`"
97     min="`echo $snap | cut -d '-' -f 6`"
98     hour="`echo $snap | cut -d '-' -f 5`"
99     day="`echo $snap | cut -d '-' -f 4`"
100     mon="`echo $snap | cut -d '-' -f 3`"
101     year="`echo $snap | cut -d '-' -f 2`"
102
103     # Convert this snap to epoc time
104     snapEpoc=`date -j -f "%Y %m %d %H %M %S" "$year $mon $day $hour $min $sec" "+%s"`
105
106     # If we are replicating, don't prune anything which hasn't gone out yet
107     if [ -n "$sendEpoc" ] ; then
108        if [ $sendEpoc -gt $snapEpoc ] ; then continue; fi
109     fi
110
111     # Get the epoch time elapsed
112     check=`expr $curEpoc - $snapEpoc`
113     pruned=0
114
115     # Looking for snaps older than 12 months
116     if [ $check -gt 31536000 ]; then
117        do_auto_prune "$DATASET" "$snap"
118        pruned=1
119     fi
120
121     # Looking for multiple snaps older than 30 days
122     if [ $check -gt 2592000 -a $pruned -eq 0 ]; then
123        # Did we already have a snapshot from this month?
124        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" ] ; then
125          do_auto_prune "$DATASET" "$snap"
126          pruned=1
127        fi
128     fi
129
130     # Looking for multiple snaps older than a day
131     if [ $check -gt 86400 -a $pruned -eq 0 ]; then
132        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" -a "$day" = "$lastDay" ] ; then
133          do_auto_prune "$DATASET" "$snap"
134          pruned=1
135        fi
136     fi
137
138     # Looking for multiple snaps older than an hour
139     if [ $check -gt 3600 -a $pruned -eq 0 ]; then
140        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" -a "$day" = "$lastDay" -a "$hour" = "$lastHour" ] ; then
141          do_auto_prune "$DATASET" "$snap"
142          pruned=1
143        fi
144     fi
145
146     # Save values of this snapshot for next pass
147     lastYear="$year" ; lastMon="$mon" ; lastDay="$day" ; lastHour="$hour"
148     lastMin="$min" ; lastSec="$sec"
149  done
150
151}
152
153# Now run the main script
154DATASET="${1}"
155KEEP="${2}"
156snapStat=0
157
158if [ -z "${DATASET}" ]; then
159  exit_err "No dataset specified!"
160fi
161
162# Make sure this is a valid DATASET
163zfs list ${DATASET} >/dev/null 2>/dev/null
164if [ $? -ne 0 ] ; then
165   exit_err "Invalid dataset specified ${DATASET}"
166fi
167
168# Create the snapshot now with the "auto-" tag
169echo_log "Creating snapshot on ${DATASET}"
170mkZFSSnap "${DATASET}" "auto-"
171if [ $? -ne 0 ] ; then
172  echo_log "ERROR: Failed creating snapshot on ${DATASET}"
173  queue_msg "ERROR: Failed creating snapshot on ${DATASET} @ `date`\n\r`cat $CMDLOG`"
174  snapStat=1
175else
176  queue_msg "Success creating snapshot on ${DATASET} @ `date`\n\r`cat $CMDLOG`"
177fi
178
179# Before we start pruning, check if any replication is running
180skipPrune=0
181export pidFile="${DBDIR}/.reptask-`echo ${DATASET} | sed 's|/|-|g'`"
182if [ -e "${pidFile}" ] ; then
183   pgrep -F ${pidFile} >/dev/null 2>/dev/null
184   if [ $? -eq 0 ] ; then skipPrune=1; fi
185fi
186
187if [ $skipPrune -eq 1 ] ; then
188  # No pruning since replication is currently running
189  echo_log "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running."
190  queue_msg "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running."
191else
192  # Safe to do the pruning, no replication is in progress
193  if [ "$KEEP" = "auto" ] ; then
194     do_automatic_prune
195  else
196     do_numeric_prune
197  fi
198fi
199
200# If we failed at any point, sent out a notice
201if [ $snapStat -ne 0 ] ; then
202   email_msg "FAILED - Automated Snapshot" "`echo_queue_msg`"
203fi
204
205# If we are successful and user wants all notifications, send out a message
206if [ $snapStat -eq 0 -a "$EMAILMODE" = "ALL" ] ; then
207   email_msg "Success - Automated Snapshot" "`echo_queue_msg`"
208else
209   rm ${MSGQUEUE} 2>/dev/null
210fi
211
212# Check if we need to run a replication task for this dataset
213${PROGDIR}/backend/runrep.sh ${DATASET} sync
Note: See TracBrowser for help on using the repository browser.