linux:purging_script
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | lamp:purging_script [2018/04/17 08:48] – ↷ Page moved from purging_script to lamp:purging_script dodger | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Description ====== | ||
+ | Have you ever fight with logrotate configuration files? Did you survived? Cool, this document is not for you xD | ||
+ | This is script is a very simple method to drop unnecessary files from your filesystems (logs?) and rotate them so they don't growth forever :-P | ||
+ | This script write 2 logs each time : | ||
+ | * '' | ||
+ | * '' | ||
+ | So you will have detailed the script actions+errors there | ||
+ | |||
+ | ====== Instructions ====== | ||
+ | |||
+ | ===== Usage ===== | ||
+ | <code bash>/ | ||
+ | ===== Configuration ===== | ||
+ | It's a file-based configuration, | ||
+ | < | ||
+ | CONFIGFILE=/ | ||
+ | </ | ||
+ | |||
+ | ==== Config file sytax ==== | ||
+ | Each line in the config file is set of file to be analysed and purged/ | ||
+ | < | ||
+ | |||
+ | Where: | ||
+ | * Path is the folder from which the script begin searching (serching with find) | ||
+ | * Mask: file mask for matching files, the script uses '' | ||
+ | * Action: Any of the avalaible actions detailed here down. | ||
+ | |||
+ | |||
+ | Y las opciones disponibles son (explicadas como parámetros de find) | ||
+ | === Actions === | ||
+ | ^ path ^ mask ^ action ^ param1 ^ param2 ^ param3 ^ | ||
+ | | /path | ? | bydate | mtime | maxdepth | N/A | | ||
+ | | /path | ? | bymin | mmin | maxdepth | N/A | | ||
+ | | /path | ? | rotate | size | maxdepth | N/A | | ||
+ | | /path | ? | rotatezip | size | maxdepth | gzip parameters | | ||
+ | | /path | ? | zip | mtime | maxdepth | gzip parameters | | ||
+ | | /path | ? | dirbydate | mtime | maxdepth | N/A | | ||
+ | | /path | ? | dirbymin | mmin | maxdepth | N/A | | ||
+ | |||
+ | Description: | ||
+ | |||
+ | * bydate : Delete files based on modification time ('' | ||
+ | * bymin : Drop files based on modification minute ('' | ||
+ | * dirbydate : Drop folders based on modification time. ('' | ||
+ | * dirbymin : Drop folders based on modification minute ('' | ||
+ | * rotate : Rotate the file ('' | ||
+ | * rotatezip : Rorate and compress the file ('' | ||
+ | * zip : Compress the file ('' | ||
+ | |||
+ | ==== Sample Config ==== | ||
+ | < | ||
+ | # Allways purge the script logs | ||
+ | / | ||
+ | / | ||
+ | |||
+ | # Target: nginx logs | ||
+ | / | ||
+ | |||
+ | # Objetivo : Oracle listerner trace files | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Script Code ===== | ||
+ | <file bash purge_files.sh> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | # (C) dodger@ciberterminal.net | ||
+ | # | ||
+ | |||
+ | |||
+ | # Exit codes: | ||
+ | # 1 : No config file | ||
+ | # 2 : Wrong option in the config file | ||
+ | # 3 : | ||
+ | # 4 : | ||
+ | # 5 : | ||
+ | # 6 : | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # CONSTANTS | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | # colors | ||
+ | LIGHTGREEN=" | ||
+ | LIGHTRED=" | ||
+ | WHITE=" | ||
+ | RESET=" | ||
+ | |||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # / CONSTANTS | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # VARIABLES | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | MYDATE=$(date +%Y%m%d%H%M) | ||
+ | CONFIGFILE=/ | ||
+ | LOGDIR=$(dirname $0)/logs/ | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # / VARIABLES | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # FUNCTIONS | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | usage() | ||
+ | { | ||
+ | printf " | ||
+ | $0 | ||
+ | | ||
+ | PLEASE READ | ||
+ | https:// | ||
+ | # VERY INITIAL CHECKS | ||
+ | } | ||
+ | |||
+ | remove_files() | ||
+ | { | ||
+ | local AUX="" | ||
+ | local OPT=" | ||
+ | local DIR=" | ||
+ | if [[ " | ||
+ | local TYPEOF=" | ||
+ | local FINDCMD=" | ||
+ | local MESSAGE=" | ||
+ | else | ||
+ | local TYPEOF=" | ||
+ | local FINDCMD=" | ||
+ | local MESSAGE=" | ||
+ | fi | ||
+ | | ||
+ | [[ " | ||
+ | printf " | ||
+ | eval find ${FOLDER} ${AUX} -name " | ||
+ | } | ||
+ | |||
+ | rotate_files() | ||
+ | { | ||
+ | local USEZIP=" | ||
+ | local MESSAGE=" | ||
+ | [[ " | ||
+ | printf " | ||
+ | # | ||
+ | # [[ " | ||
+ | | ||
+ | case $USEZIP in | ||
+ | " | ||
+ | [[ ! " | ||
+ | local MOVECMD=" | ||
+ | local DSTFILE=" | ||
+ | printf "%s and gzipping with ${PARAM3}\n" | ||
+ | ;; | ||
+ | " | ||
+ | local MOVECMD=" | ||
+ | local DSTFILE=" | ||
+ | printf " | ||
+ | ;; | ||
+ | * ) | ||
+ | printf " | ||
+ | return 1 | ||
+ | ;; | ||
+ | esac | ||
+ | while read FILE ; do | ||
+ | echo ${FILE} | ||
+ | ${MOVECMD} " | ||
+ | > ${FILE} | ||
+ | done < <(find ${FOLDER} ${AUX} -name " | ||
+ | } | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # / FUNCTIONS | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # MAIN | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | [ ! -d ${LOGDIR} ] && mkdir -p ${LOGDIR} | ||
+ | |||
+ | exec 1>> ${LOGDIR}/ | ||
+ | exec 2>> ${LOGDIR}/ | ||
+ | |||
+ | |||
+ | if [ ! -f ${CONFIGFILE} ]; then | ||
+ | echo "No configuration file found" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | |||
+ | |||
+ | # workaround for eval " | ||
+ | AUXDIR="/ | ||
+ | mkdir ${AUXDIR} | ||
+ | cd ${AUXDIR} | ||
+ | #/ workaround for eval " | ||
+ | |||
+ | while read LINE ; do | ||
+ | |||
+ | |||
+ | if [[ ! " | ||
+ | printf " | ||
+ | continue | ||
+ | fi | ||
+ | | ||
+ | FOLDER=$(echo $LINE | cut -d',' | ||
+ | MASK=$(echo $LINE | cut -d',' | ||
+ | ACTION=$(echo $LINE | cut -d',' | ||
+ | PARAM1=$(echo $LINE | cut -d',' | ||
+ | PARAM2=$(echo $LINE | cut -d',' | ||
+ | PARAM3=$(echo $LINE | cut -d',' | ||
+ | |||
+ | case ${ACTION,,} in | ||
+ | " | ||
+ | remove_files mtime | ||
+ | ;; | ||
+ | " | ||
+ | remove_files mmin | ||
+ | ;; | ||
+ | " | ||
+ | remove_files mtime dir | ||
+ | ;; | ||
+ | " | ||
+ | remove_files mmin dir | ||
+ | ;; | ||
+ | " | ||
+ | rotate_files nozip | ||
+ | ;; | ||
+ | " | ||
+ | rotate_files zip | ||
+ | ;; | ||
+ | " | ||
+ | printf " | ||
+ | [[ " | ||
+ | [[ ! " | ||
+ | find ${FOLDER} ${AUX} -name " | ||
+ | ;; | ||
+ | * ) | ||
+ | usage | ||
+ | exit 2 | ||
+ | ;; | ||
+ | esac | ||
+ | | ||
+ | done < <(cat ${CONFIGFILE} | egrep -v " | ||
+ | |||
+ | |||
+ | |||
+ | # workaround for eval " | ||
+ | cd ${OLDPWD} | ||
+ | rm -fr ${AUXDIR} | ||
+ | #/ workaround for eval " | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # / MAIN | ||
+ | # | ||
+ | ######################################################################## | ||
+ | </ |
linux/purging_script.txt · Last modified: 2022/02/11 11:36 by 127.0.0.1