source: src-sh/lpreserver/backend/runsnap.sh @ 042dc70

enter/10releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2releng/10.2stable/10
Last change on this file since 042dc70 was 042dc70, checked in by Kris Moore <kris@…>, 18 months ago

Improve listing of ZFS snapshots in life-preserver, also fix a bug with replication,
don't auto-prune the last replicated snapshot, so that zfs send/recv are happy

  • Property mode set to 100755
File size: 5.4 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
69  num=0
70  for snap in $rSnaps
71  do
72     # Skip snaps not created by life-preserver
73     cur="`echo $snap | cut -d '-' -f 1`"
74     if [ "$cur" != "auto" ] ; then continue; fi
75
76     # If this snapshot is the last one replicated, lets skip pruning it for now
77     if [ "$cur" = "$lastSEND" ]; then continue; fi
78
79     sec="`echo $snap | cut -d '-' -f 7`"
80     min="`echo $snap | cut -d '-' -f 6`"
81     hour="`echo $snap | cut -d '-' -f 5`"
82     day="`echo $snap | cut -d '-' -f 4`"
83     mon="`echo $snap | cut -d '-' -f 3`"
84     year="`echo $snap | cut -d '-' -f 2`"
85
86     # Convert this snap to epoc time
87     snapEpoc=`date -j -f "%Y %m %d %H %M %S" "$year $mon $day $hour $min $sec" "+%s"`
88
89     # Get the epoch time elapsed
90     check=`expr $curEpoc - $snapEpoc`
91     pruned=0
92
93     # Looking for snaps older than 12 months
94     if [ $check -gt 31536000 ]; then
95        do_auto_prune "$DATASET" "$snap"
96        pruned=1
97     fi
98
99     # Looking for multiple snaps older than 30 days
100     if [ $check -gt 2592000 -a $pruned -eq 0 ]; then
101        # Did we already have a snapshot from this month?
102        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" ] ; then
103          do_auto_prune "$DATASET" "$snap"
104          pruned=1
105        fi
106     fi
107
108     # Looking for multiple snaps older than a day
109     if [ $check -gt 86400 -a $pruned -eq 0 ]; then
110        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" -a "$day" = "$lastDay" ] ; then
111          do_auto_prune "$DATASET" "$snap"
112          pruned=1
113        fi
114     fi
115
116     # Looking for multiple snaps older than an hour
117     if [ $check -gt 3600 -a $pruned -eq 0 ]; then
118        if [ "$year" = "$lastYear" -a "$mon" = "$lastMon" -a "$day" = "$lastDay" -a "$hour" = "$lastHour" ] ; then
119          do_auto_prune "$DATASET" "$snap"
120          pruned=1
121        fi
122     fi
123
124     # Save values of this snapshot for next pass
125     lastYear="$year" ; lastMon="$mon" ; lastDay="$day" ; lastHour="$hour"
126     lastMin="$min" ; lastSec="$sec"
127  done
128
129}
130
131# Now run the main script
132DATASET="${1}"
133KEEP="${2}"
134snapStat=0
135
136if [ -z "${DATASET}" ]; then
137  exit_err "No dataset specified!"
138fi
139
140# Make sure this is a valid DATASET
141zfs list ${DATASET} >/dev/null 2>/dev/null
142if [ $? -ne 0 ] ; then
143   exit_err "Invalid dataset specified ${DATASET}"
144fi
145
146# Create the snapshot now with the "auto-" tag
147echo_log "Creating snapshot on ${DATASET}"
148mkZFSSnap "${DATASET}" "auto-"
149if [ $? -ne 0 ] ; then
150  echo_log "ERROR: Failed creating snapshot on ${DATASET}"
151  queue_msg "ERROR: Failed creating snapshot on ${DATASET} @ `date`\n\r`cat $CMDLOG`"
152  snapStat=1
153else
154  queue_msg "Success creating snapshot on ${DATASET} @ `date`\n\r`cat $CMDLOG`"
155fi
156
157# Before we start pruning, check if any replication is running
158skipPrune=0
159export pidFile="${DBDIR}/.reptask-`echo ${DATASET} | sed 's|/|-|g'`"
160if [ -e "${pidFile}" ] ; then
161   pgrep -F ${pidFile} >/dev/null 2>/dev/null
162   if [ $? -eq 0 ] ; then skipPrune=1; fi
163fi
164
165if [ $skipPrune -eq 1 ] ; then
166  # No pruning since replication is currently running
167  echo_log "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running."
168  queue_msg "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running."
169else
170  # Safe to do the pruning, no replication is in progress
171  if [ "$KEEP" = "auto" ] ; then
172     do_automatic_prune
173  else
174     do_numeric_prune
175  fi
176fi
177
178# If we failed at any point, sent out a notice
179if [ $snapStat -ne 0 ] ; then
180   email_msg "FAILED - Automated Snapshot" "`echo_queue_msg`"
181fi
182
183# If we are successful and user wants all notifications, send out a message
184if [ $snapStat -eq 0 -a "$EMAILMODE" = "ALL" ] ; then
185   email_msg "Success - Automated Snapshot" "`echo_queue_msg`"
186else
187   rm ${MSGQUEUE} 2>/dev/null
188fi
189
190# Check if we need to run a replication task for this dataset
191${PROGDIR}/backend/runrep.sh ${DATASET} sync
Note: See TracBrowser for help on using the repository browser.