Guest User

Untitled

a guest
Nov 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Delete files older than some time
  4. #
  5.  
  6. usage()
  7. {
  8. cat << EOF
  9.  
  10. usage $0 options
  11.  
  12. OPTIONS
  13. -p path
  14. -n name regex
  15. -u unit of time
  16.  
  17. Example: ./clean-backups.sh -p /Users/foo/websites/backup/files -n '*.sql.gz' -u 30d
  18.  
  19. -n : True if the last component of the pathname being examined matches pattern
  20.  
  21. -u : Possible units of time:
  22. s second
  23. m minute (60 seconds)
  24. h hour (60 minutes)
  25. d day (24 hours)
  26. w week (7 days)
  27. Any number of units may be combined in one argument, for example, "1h30m".
  28.  
  29. EOF
  30. }
  31.  
  32. path=''
  33. unitoftime=''
  34. regex='*.sql.gz'
  35.  
  36. if [ $# -eq 0 ];
  37. then
  38. usage
  39. exit 1
  40. fi
  41.  
  42. while getopts "hp:n:u:" opt
  43. do
  44. case $opt in
  45. h)
  46. usage
  47. exit 1
  48. ;;
  49. p)
  50. echo "Path : $OPTARG"
  51. path=$OPTARG
  52. ;;
  53. n)
  54. echo "Name Regex : $OPTARG"
  55. regex=$OPTARG
  56. ;;
  57. u)
  58. echo "Unit of Time : $OPTARG"
  59. unitoftime=$OPTARG
  60. ;;
  61. \?)
  62. echo "Invalid option: $OPTARG" >&2
  63. usage
  64. exit 1
  65. ;;
  66. :)
  67. echo "Option -$OPTARG requires an argument." >&2
  68. usage
  69. exit 1
  70. ;;
  71. esac
  72. done
  73.  
  74. if [ ! -z $path ] && [ ! -z $regex ] && [ ! -z $unitoftime ];
  75. then
  76. if [ ! -d $path ]
  77. then
  78. echo "Directory Not Found!!!"
  79. exit 1
  80. fi
  81. log=$(date +"%d-%m-%Y-%H-%M-%S")_backup_files_deleted.log;
  82. echo "find ${path} -type f -name ${regex} -Btime +${unitoftime}"
  83. find ${path} -type f -name ${regex} -Btime +${unitoftime} > $path/$log
  84. find ${path} -type f -name ${regex} -Btime +${unitoftime} -exec rm {} \;
  85. echo "Cleaned the backups..!"
  86. exit 0
  87. fi
Add Comment
Please, Sign In to add comment