Skip to content

Instantly share code, notes, and snippets.

@medigeek
Last active June 20, 2020 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medigeek/57b1b1956c83fdfec56bb9d06254f6ad to your computer and use it in GitHub Desktop.
Save medigeek/57b1b1956c83fdfec56bb9d06254f6ad to your computer and use it in GitHub Desktop.
logrotate script for platform.sh -- can be modified for other purposes
#!/usr/bin/env bash
set -e
SCRIPTDIR=$(dirname "$0")
SCRIPTREALPATH=$(realpath $(dirname "$0"))
#cd "$SCRIPTDIR"
#echo $SCRIPTREALPATH
LOGDIR="/app/storage/logs/"
DATESTRING=$(date "+%Y-%m-%d_%H-%M-%S")
#bash function to use with find
gzipper () {
DATESTRING=$(date "+%Y-%m-%d_%H-%M-%S")
echo "$1"
gzip -9 -c "$1" > "$1.$DATESTRING.gz"
echo -n > "$1"
}
#export it so bash in find can use it
export -f gzipper
echo "Initialized logrotate at $DATESTRING"
cd "$LOGDIR"
#find all *.log files and use bash with function gzipper
find $LOGDIR -type f -name "*.log" -exec bash -c 'gzipper "$0"' "{}" \;
#find all *.gz files and delete if older than 14 days
find . -type f -name "*.gz" -mtime +14 -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment