rsync-backup.in: Factor out log file expiry into a separate function.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 6 Mar 2014 09:58:08 +0000 (09:58 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 6 Mar 2014 09:58:08 +0000 (09:58 +0000)
No real use for this yet, but it's tidier this way.

rsync-backup.in

index d7267ca..a9145d6 100644 (file)
@@ -177,6 +177,30 @@ runhook () {
   done
 }
 
+remove_old_logfiles () {
+  base=$1
+  ## Remove old logfiles with names of the form BASE.DATE#N, so that there
+  ## are at most $MAXLOG of them.
+
+  ## Count up the logfiles.
+  nlog=0
+  for i in "$base".*; do
+    if [ ! -f "$i" ]; then continue; fi
+    nlog=$(( nlog + 1 ))
+  done
+
+  ## If there are too many, go through and delete some early ones.
+  if [ $dryrun = nil ] && [ $nlog -gt $MAXLOG ]; then
+    n=$(( nlog - MAXLOG ))
+    for i in "$base".*; do
+      if [ ! -f "$i" ]; then continue; fi
+      rm -f "$i"
+      n=$(( n - 1 ))
+      if [ $n -eq 0 ]; then break; fi
+    done
+  fi
+}
+
 ###--------------------------------------------------------------------------
 ### Database operations.
 
@@ -744,23 +768,8 @@ run_backup_cmd () {
     bkprc=1
   fi
 
-  ## Count up the logfiles.
-  nlog=0
-  for i in "$logdir/$host/$fs".*; do
-    if [ ! -f "$i" ]; then continue; fi
-    nlog=$(( nlog + 1 ))
-  done
-
-  ## If there are too many, go through and delete some early ones.
-  if [ $dryrun = nil ] && [ $nlog -gt $MAXLOG ]; then
-    n=$(( nlog - MAXLOG ))
-    for i in "$logdir/$host/$fs".*; do
-      if [ ! -f "$i" ]; then continue; fi
-      rm -f "$i"
-      n=$(( n - 1 ))
-      if [ $n -eq 0 ]; then break; fi
-    done
-  fi
+  ## Clear away any old logfiles.
+  remove_old_logfiles "$logdir/$host/$fs"
 }
 
 backup () {