Created
June 6, 2014 02:29
-
-
Save gotnix/ef85fdaa686130e5a87c to your computer and use it in GitHub Desktop.
清理过期日志
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # SCRIPT: tidy_dirs.sh | |
| # AUTHOR: Terry.Zheng | |
| # E-MAIL: jfhgmv@gmail.com | |
| # DATE: Thu Jan 23 10:08:29 2014 | |
| # REV: 1.0.A (Valid are A, B, D, T and P) | |
| # (for Alpha, Beta, Dev, Test and Production) | |
| # | |
| # PLATFORM: GNU/Linux (SPECIFY: AIX, FreeBSD, GNU/Linux, HP-UX, Solaris | |
| # or Not platform dependent) | |
| # | |
| # PURPOSE: 清理ERP、SCM等应用的用户操作日志 | |
| # 根据要日志的过期天数把日志目录分类,先删除过期日志,再删除空目录。 | |
| # | |
| # TODO: Add lock file. | |
| # | |
| # REV LIST: | |
| # DATE: DATE_of_REVSION | |
| # BY: AUTHOR_of_MODIFICATION | |
| # MODIFICATION: Describe what was modified, new features, etc-- | |
| # | |
| # | |
| # set -n # Uncomment to check script syntax, without execution. | |
| # # NOTE: Do not forget to put the comment back in or | |
| # # the shell script will not execute! | |
| # | |
| # set -x # Uncommet to debug this shell script. | |
| # | |
| ################################################################ | |
| # DEFINE FILES AND VARIABLES HERE | |
| ################################################################ | |
| LOGGER='/usr/bin/logger -s -t tidy_dirs.sh -p cron.error' | |
| LOG_ERROR='/var/log/tidy_dirs-error.log' | |
| two_days=('/var/www/remote_data/statics/result/' | |
| '/var/www/remote_data/statics/var/' | |
| '/var/www/remote_data/statics/console/' | |
| '/var/log/java/erp/' | |
| '/var/log/java/scm/' | |
| '/var/log/java/img/' | |
| '/opt/tomcat_scm/logs/' | |
| '/opt/apache-tomcat-7.0.42/logs/') | |
| ################################################################ | |
| # DEFINE FUNCTIONS HERE | |
| ################################################################ | |
| ####################################### | |
| # Tidy the backup dir, remove expired files and empty dir. | |
| # Globals: | |
| # LOGGER | |
| # LOG_ERROR | |
| # Arguments: | |
| # dir : tidy file in this directory, and exclude itself. | |
| # days : a number(greater than 1) of expire days. | |
| # Returns: | |
| # None | |
| ####################################### | |
| tidy_dir () | |
| { | |
| local dir=${1} | |
| local days=${2} | |
| if [[ ${days} -gt 1 ]]; then | |
| if ! find "${dir}" -type d -empty 2>&1 > /dev/null; then | |
| cd ${dir} > /dev/null 2>&1 && { | |
| # 1st remove the expire backup files. | |
| /sbin/logsave -as ${LOG_ERROR} \ | |
| find -maxdepth 2 -depth -type f -mtime +${days} \ | |
| | /usr/bin/xargs -t -I {f} rm -f {f} | |
| # 2nd remove the directory if it empty. | |
| /sbin/logsave -as ${LOG_ERROR} \ | |
| find -maxdepth 2 -depth -type d -empty -mtime +${days} \ | |
| | /usr/bin/xargs -t -I {d} rm -rf {d} | |
| } || { | |
| ${LOGGER} -- "Cannot change directory to ${dir}." | |
| exit 4 | |
| } | |
| else | |
| ${LOGGER} -- "${dir} is EMPTY, go to next." | |
| fi | |
| else | |
| ${LOGGER} -- "Confirm the arguments for function tidy_dir ()." | |
| exit 4 | |
| fi | |
| } | |
| ################################################################ | |
| # BEGINNING OF MAIN | |
| ################################################################ | |
| for (( d = 0 ; d < ${#two_days[@]} ; d++ )) ; do | |
| tidy_dir ${two_days[d]} 2 | |
| done | |
| exit $? | |
| ##---------------------- End of script -----------------------## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment