Skip to content

Instantly share code, notes, and snippets.

@cinhtau
Created February 26, 2018 10:52
Show Gist options
  • Save cinhtau/fb668dde4c12a05b1f6d8b180e9b4802 to your computer and use it in GitHub Desktop.
Save cinhtau/fb668dde4c12a05b1f6d8b180e9b4802 to your computer and use it in GitHub Desktop.
Copies all log files for a specific date range to another directory
#!/usr/bin/env bash
#
# Usage: logrange-copy.sh myApp 2017-10-17 /srv/nas/logs/myApp/
# param 1: myApp is a prefix and has multiple folders
# param 2: 2017-10-17 the upper range for daily iteration
# param 3: target directory to copy the files, could also be AWS S3
#
# program is used to copy jboss logs for reindexing with elasticsearch
# used for logstash
#
logdir=$1
enddate=$2
target=$3
# array, fetch all log directories
logdirs=( $(ls | grep "$logdir") )
for d in "${logdirs[@]}"
do
# extract date information
year=$(date --date="$enddate" "+%Y")
month=$(date --date="$enddate" "+%m")
day=$(date --date="$enddate" "+%d")
# increment from 01 to end, for 17 -> 01 02 .. 17
for v in $(seq -w 01 $day);
do
logdate="$year-$month-$v"
# usually gzipped
logfile="server.log.$logdate.0.gz"
# check for existence
if [ -e "$d/$logfile" ]
then
echo "copy $d/$logfile to $target"
cp "$d/$logfile" "$target/$d-$logdate.log.gz"
else
# echo "$d/$logfile not found"
:
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment