source: src-sh/lpreserver/backend/runsnap.sh @ 9cd0fa0

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 9cd0fa0 was 9cd0fa0, checked in by Kris Moore <kris@…>, 9 months ago
  • Major update to life-preserver backend

Add new "automatic" mode for creating snapshots

When enabled, this will create snapshots in the following manner:

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