Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Will remove oldest unneeded backups one by one unitl there are KEEP_FREE_GIGS gigabytes of space free on device containing STORAGE_DIR.
  4. # Unneeded backups are picked regarding daily/weekly/monthly structure.
  5.  
  6. echo_with_time()
  7.         {
  8.         cur_time=$( date --rfc-3339=ns )
  9.         echo "$cur_time $1"
  10.         }
  11.  
  12. alldone()
  13.         {
  14.         echo_with_time "$THIS_SCRIPT_NAME completed"
  15.         echo
  16.         exit 0
  17.         }
  18.  
  19. THIS_SCRIPT_NAME=$( basename $0 )
  20.  
  21. LOG_DIR='/opt/gpms/logs'
  22. #DATE_POSTFIX=$( date '+_%Y_%m_%d_%H_%M_%S' )
  23. LOG_FILE="${LOG_DIR}/${THIS_SCRIPT_NAME}${DATE_POSTFIX}.log"
  24. if [ ! -d "$LOG_DIR" ]; then
  25.         mkdir -p "$LOG_DIR"
  26. fi
  27. exec 1>>"$LOG_FILE" 2>&1        # Redirect all output to log
  28.  
  29. echo_with_time "$THIS_SCRIPT_NAME started ----------------"
  30.  
  31. KEEP_FREE_GIGS='1200'   # Amount of freee space we need to keep in gigabytes
  32. KEEP_FREE_BYTES=$(( KEEP_FREE_GIGS * 1024 * 1024 * 1024 ))
  33.  
  34. STORAGE_DIR='/mnt/misc2/storage/backup'
  35.  
  36. DAILY_APP_STORAGE_DIR="${STORAGE_DIR}/app/daily"
  37. WEEKLY_APP_STORAGE_DIR="${STORAGE_DIR}/app/weekly"
  38. MONTHLY_APP_STORAGE_DIR="${STORAGE_DIR}/app/monthly"
  39.  
  40. DAILY_DB_STORAGE_DIR="${STORAGE_DIR}/db/daily"
  41. WEEKLY_DB_STORAGE_DIR="${STORAGE_DIR}/db/weekly"
  42. MONTHLY_DB_STORAGE_DIR="${STORAGE_DIR}/db/monthly"
  43.  
  44. echo "Storage dir is $STORAGE_DIR"
  45.  
  46. files_for_removal+=$( find "${DAILY_APP_STORAGE_DIR}" -mtime +2 -type f)
  47. files_for_removal+=$'\n'
  48. files_for_removal+=$( find "${WEEKLY_APP_STORAGE_DIR}" -mtime +8 -type f )
  49. files_for_removal+=$'\n'
  50. files_for_removal+=$( find "${MONTHLY_APP_STORAGE_DIR}" -mtime +366 -type f )
  51. files_for_removal+=$'\n'
  52.  
  53. files_for_removal+=$( find "${DAILY_DB_STORAGE_DIR}" -mtime +2 -type f )
  54. files_for_removal+=$'\n'
  55. files_for_removal+=$( find "${WEEKLY_DB_STORAGE_DIR}" -mtime +8 -type f )
  56. files_for_removal+=$'\n'
  57. files_for_removal+=$( find "${MONTHLY_DB_STORAGE_DIR}" -mtime +366 -type f )
  58. files_for_removal+=$'\n'
  59.  
  60. files_for_removal=$( echo "$files_for_removal" | xargs stat --format='%Y %s %n' | sort -n )
  61.  
  62. #free_space_gigs=$( df -P --block-size=1G "$STORAGE_DIR" | tail -n +2 | tr -s ' ' | cut -d ' ' -f 4 )
  63. free_space_gigs=$( df -P --block-size=1G "$STORAGE_DIR" | sed '1d;s/^\(\([^ ]*\)\s\+\)\{4\}.*/\2/' )
  64. free_space_bytes=$( df -P --block-size=1 "$STORAGE_DIR" | sed '1d;s/^\(\([^ ]*\)\s\+\)\{4\}.*/\2/' )
  65.  
  66. echo "There are $free_space_bytes bytes (${free_space_gigs}G) free on the device"
  67. echo "Should keep $KEEP_FREE_BYTES bytes (${KEEP_FREE_GIGS}G) free"
  68.  
  69. if [ "$free_space_bytes" -ge "$KEEP_FREE_BYTES" ]; then
  70.         echo "No actions needed"
  71.         alldone
  72. fi
  73.  
  74. should_free_bytes=$(( KEEP_FREE_BYTES - free_space_bytes ))
  75. echo "Should free $should_free_bytes bytes"
  76.  
  77. numfiles=0
  78. numfiles=$( echo "$files_for_removal" | wc -l )
  79. numallfiles=$( find "$STORAGE_DIR" -type f | wc -l )
  80. echo "Have $numfiles of $numallfiles files available for deletion"
  81.  
  82. freed=0
  83. enough=''
  84. numfiles=0
  85. while read timestamp size file; do
  86.         echo "Removing \"$file\" size $size bytes"
  87.         rm "$file"
  88. #       mv "$file" /mnt/misc1/for_removal
  89.         freed=$(( freed + size))
  90.         numfiles=$(( numfiles + 1 ))
  91.         if [ "$freed" -ge "$should_free_bytes" ]; then
  92.                 enough='yes'
  93.                 break
  94.         fi
  95. done <<< "$files_for_removal"
  96.  
  97. echo "$numfiles files deleted"
  98. echo "Freed $freed bytes"
  99. if [ -z "$enough" ]; then
  100.         echo 'That is not enough'
  101. else
  102.         echo 'That is enough'
  103. fi
  104.  
  105. alldone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement