Эх сурвалжийг харах

add belowspace deleter script

ChesTeRcs 5 жил өмнө
commit
72990063bd
1 өөрчлөгдсөн 56 нэмэгдсэн , 0 устгасан
  1. 56 0
      autoDeleterBelowSpace.sh

+ 56 - 0
autoDeleterBelowSpace.sh

@@ -0,0 +1,56 @@
+#!/bin/bash
+#
+#  prune_dir - prune directory by deleting files if we are low on space
+#
+DIR=/media/usb/ftp/data/domsod/FI9900P_00626E69B672/record
+CAPACITY_LIMIT=55
+
+if [ "$DIR" == "" ]
+then
+    echo "ERROR: directory not specified"
+    exit 1
+fi
+
+if ! cd $DIR
+then
+    echo "ERROR: unable to chdir to directory '$DIR'"
+    exit 2
+fi
+
+if [ "$CAPACITY_LIMIT" == "" ]
+then
+    CAPACITY_LIMIT=95   # default limit
+fi
+
+CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
+
+if [ $CAPACITY -gt $CAPACITY_LIMIT ]
+then
+    #
+    # Get list of files, oldest first.
+    # Delete the oldest files until
+    # we are below the limit. Just
+    # delete regular files, ignore directories.
+    #
+    ls -rt | while read FILE
+    do
+        if [ -f $FILE ]
+        then
+            if rm -f $FILE
+            then
+                echo "Deleted $FILE"
+
+                CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
+
+                if [ $CAPACITY -le $CAPACITY_LIMIT ]
+                then
+                    # we're below the limit, so stop deleting
+                    echo "Below limit, stopped deleting."
+                    exit
+                fi
+            fi
+        fi
+    done
+else
+    echo "Don't need to delete anything, capacity: ${CAPACITY}"
+fi