Last active
June 20, 2020 21:55
-
-
Save medigeek/57b1b1956c83fdfec56bb9d06254f6ad to your computer and use it in GitHub Desktop.
logrotate script for platform.sh -- can be modified for other purposes
This file contains 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
#!/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