Ignore:
Timestamp:
02/25/14 10:55:49 (6 months ago)
Author:
Kris Moore <kris@…>
Branches:
master, releng/10.0.1, releng/10.0.2
Children:
4f9718b
Parents:
c24d6287
Message:
  • 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src-sh/lpreserver/backend/runsnap.sh

    rc0e6494 r9cd0fa0  
    1010. ${PROGDIR}/backend/functions.sh 
    1111 
     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 
    12126DATASET="${1}" 
    13127KEEP="${2}" 
     
    35149fi 
    36150 
    37 # Get our list of snaps 
    38 snaps=$(snaplist "${DATASET}") 
    39  
    40 # Reverse the list 
    41 for tmp in $snaps 
    42 do 
    43    rSnaps="$tmp $rSnaps" 
    44 done 
    45  
    46151# Before we start pruning, check if any replication is running 
    47152skipPrune=0 
     
    56161  echo_log "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running." 
    57162  queue_msg "WARNING: Skipped pruning snapshots on ${DATASET} while replication is running." 
    58  
    59163else 
    60   # Do any pruning 
    61   num=0 
    62   for snap in $rSnaps 
    63   do 
    64      # Only remove snapshots which are auto-created, so we don't delete one the user 
    65      # made specifically 
    66      cur="`echo $snap | cut -d '-' -f 1`"  
    67      if [ "$cur" != "auto" ] ; then 
    68        continue; 
    69      fi 
    70  
    71      num=`expr $num + 1` 
    72      if [ $num -gt $KEEP ] ; then 
    73         echo_log "Pruning old snapshot: $snap" 
    74         rmZFSSnap "${DATASET}" "$snap" 
    75         if [ $? -ne 0 ] ; then 
    76           echo_log "ERROR: Failed pruning snapshot $snap on ${DATASET}" 
    77           queue_msg "ERROR: Failed pruning snapshot $snap on ${DATASET} @ `date`\n\r`cat $CMDLOG`" 
    78           snapStat=1 
    79         else 
    80           queue_msg "Success pruning snapshot $snap on ${DATASET} @ `date`\n\r`cat $CMDLOG`" 
    81         fi 
    82       fi 
    83   done 
     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 
    84170fi 
    85171 
Note: See TracChangeset for help on using the changeset viewer.