Facebook
From Sloppy Eider, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 69
  1. #!/bin/bash
  2. #
  3. #  prune_dir - prune directory by deleting files if we are low on space
  4. #
  5. DIR_FROM=$1
  6. DIR_TO=$2
  7. CAPACITY_LIMIT=$3
  8.  
  9. if [ "$DIR_FROM" == "" ]
  10. then
  11.     echo "ERROR: directory not specified"
  12.     exit 1
  13. fi
  14.  
  15. if ! cd $DIR_FROM
  16. then
  17.     echo "ERROR: unable to chdir to directory '$DIR_FROM'"
  18.     exit 2
  19. fi
  20.  
  21. if [ "$DIR_TO" == "" ]
  22. then
  23.     echo "ERROR: directory not specified"
  24.     exit 1
  25. fi
  26.  
  27. if ! cd $DIR_TO
  28. then
  29.     echo "$DIR_TO not found. Mkdir for new directory!'"
  30.     mkdir $DIR_TO
  31.     if [ ! cd $DIR_TO ]
  32.         then
  33.         echo "ERROR: Didn't create specific directory! Something went wrong! Exit from script!"
  34.         exit 1
  35.     fi
  36. fi
  37.  
  38. if ! [[ "$CAPACITY_LIMIT" =~ ^[0-9]+$ ]]
  39.     then
  40.         echo "Sorry integers only"
  41.         exit 1
  42. fi
  43.  
  44. if [ "$CAPACITY_LIMIT" == "" ] || [ "$CAPACITY_LIMIT" -lt 60 ]
  45. then
  46.     CAPACITY_LIMIT=90   # default limit
  47. fi
  48.  
  49. CAPACITY=$(df -k $DIR_FROM | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  50.  
  51. while [ $CAPACITY -gt $CAPACITY_LIMIT ]
  52. do
  53.     oldest_file=$(find $DIR_FROM -type f -printf '%T+ %p\n' | sort | head -n 1 | tr -s ' ' | cut -d ' ' -f2)
  54.  
  55.     file_size=$(du -k $oldest_file | cut -f1)
  56.     free_space=$(df -k $DIR_TO | awk '{gsub("%",""); capacity=$4}; END {print capacity}')
  57.  
  58.     zero_size=0
  59.     if [ $file_size -gt $free_space]
  60.     then
  61.         SECOND_CAPACITY=$(df -k $DIR_TO | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  62.         test_limit=6
  63.         while [ $SECOND_CAPACITY -gt $test_limit ]
  64.         do
  65.             second_disk_old_file=$(find $DIR_TO -type f -printf '%T+ %p\n' | sort | head -n 1 | tr -s ' ' | cut -d ' ' -f2)
  66.             parent_second_directory_path=$(dirname $second_disk_old_file)
  67.             files_number_second=$(find $parent_second_directory_path -type f | wc -l)
  68.             rm -rf $second_disk_old_file
  69.             if [ $files_number_second -eq 1 ]
  70.                 then
  71.                 rm -rf $parent_second_directory_path
  72.             fi
  73.  
  74.             SECOND_CAPACITY=$(df -k $DIR_TO | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  75.         done
  76.     fi
  77.  
  78.     parent_directory_name=$(dirname $oldest_file | sed 's,^\(.*/\)\?\([^/]*\),\2,')
  79.     parent_directory_path=$(dirname $oldest_file)
  80.     grand_parent_directory_path=$(dirname $parent_directory_path)
  81.     grand_parent_directory_name=$(dirname $parent_directory_path | sed 's,^\(.*/\)\?\([^/]*\),\2,')
  82.  
  83.    
  84.     dest_grand_parent_directory_path="${DIR_TO}/$grand_parent_directory_name"          
  85.     dest_directory_path="${dest_grand_parent_directory_path}/$parent_directory_name"
  86.    
  87.     if [ ! -d $dest_grand_parent_directory_path ]
  88.     then
  89.         mkdir $dest_grand_parent_directory_path
  90.         if [ ! -d $dest_grand_parent_directory_path ]
  91.         then
  92.             echo "ERROR"
  93.             exit 1
  94.         else
  95.             mkdir $dest_directory_path
  96.             if [ ! -d $dest_directory_path ]
  97.             then
  98.                 echo "ERROR"
  99.                 exit 1
  100.             fi
  101.         fi
  102.     else
  103.         mkdir $dest_directory_path
  104.         if [ ! -d $dest_directory_path ]
  105.         then
  106.             echo "ERROR"
  107.             exit 1
  108.         fi
  109.  
  110.     fi
  111.  
  112.     parent_directory_path=$(dirname $oldest_file)
  113.     echo "---------------"
  114.     echo "PRZENOSZE: $oldest_file"
  115.     echo "DO: $dest_directory_path"
  116.     echo $'---------------\n'
  117.     mv $oldest_file $dest_directory_path
  118.  
  119.     files_number=$(find $parent_directory_path -type f | wc -l)
  120.     a=0
  121.     if [ $files_number -eq $a ]
  122.     then
  123.         rm -rf $parent_directory_path
  124.     fi
  125.  
  126.     CAPACITY=$(df -k $DIR_FROM | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  127. done
  128.  
  129.    
  130.