logrotate 791 B

123456789101112131415161718192021222324252627282930
  1. #!/bin/bash
  2. ## Simple logrotate script
  3. ## http://superglue.it | Danja Vasiliev, 2014
  4. ##
  5. ## Takes filepath(s) as argument:
  6. ## logrotate /www/access.log [/www/error.log] [/www/admin/admin.log]
  7. ## backlogs to keep
  8. _ROTATE=7
  9. _DATE=$(date +%d.%m.%y-%H:%M:%S)
  10. _LOGS=( $* )
  11. function err {
  12. printf %s": "%s"\n" "$(basename $0)" "$1"; exit 1
  13. }
  14. [[ -z $_LOGS ]] && err 'missing input file'
  15. [[ ! -e $_LOGS ]] && err 'input file not readable'
  16. for _FILE in ${_LOGS[@]}; do
  17. ## get number of backlogs and purge unwanted
  18. _N=( $(ls -1 -r $_FILE*.gz &>/dev/null || echo '') )
  19. [[ ${#_N[@]} -gt $((_ROTATE-1)) ]] && rm -f ${_N[@]:$((_ROTATE-1)) }
  20. ## compress and move
  21. gzip -f $_FILE || err 'failed to gzip file'
  22. mv $_FILE.gz "$_FILE"_"$_DATE".gz || err 'failed to rename archive'
  23. done
  24. exit 0