Created
August 8, 2017 04:05
-
-
Save erincerys/572aaf2a52a86c1990f73c7f34f03271 to your computer and use it in GitHub Desktop.
Merge irssi log directories, deleting small files
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
#!/bin/bash | |
# migrate.sh | |
# DESCRIPTION: Parse two directories of logfiles. | |
# FUNCTION: Append new files with old ones, delete files that are small, and move new files that aren't present to the old directory. | |
# USAGE: bash migrate.sh /path/to/old_logdir /path/to/new_logdir | |
# ELABORATION: | |
# Settings | |
minlines=15 # Threshhold number of lines to delete a logfile | |
# Configure directories to compare against and process to. | |
if [ $# -le 1 ] ; then | |
echo "Usage: $0 /path/to/old_logdir /path/to/new_logdir" | |
exit 1 | |
else | |
ologdir=$1 | |
nlogdir=$2 | |
fi | |
# Iterate logfiles | |
p=0 | |
m=0 | |
a=0 | |
d=0 | |
for file in ${nlogdir}/*/*.log ; do | |
p=(( $c + 1 )) | |
wc=($(wc -l $file)) | |
# If file is less than MINLINES, delete it. | |
if [ ${wc[0]} -lt $minlines ] ; then | |
d=(( $d + 1 )) | |
echo "Deleting $file as it is less than $minlines lines..." | |
rm $file | |
else | |
# If file exists in OLD_LOG_DIR, concatenate new logfile to old. | |
if [ -s ${ologdir}/${file} ] ; then | |
a=(( $a + 1 )) | |
echo "Appending $file to present copy in irclogs..." | |
cat "${nlogdir}/${file}" >> "${ologdir}/${file}" | |
# Else, move new logfile to old logdir, preserving subdirectory. | |
else | |
m=(( $m + 1) | |
echo "Moving $file to '${ologdir}, as it does not exist..." | |
mv "${nlogdir}/${file}" "${ologdir}/${file}" | |
fi | |
fi | |
done | |
echo "[!] Done! Performed $a merges, $m moves, and $d deletions." | |
# Delete new logdir | |
while true ; do | |
read "Delete '${nlogdir}' (y/n)? " deleteq | |
case deleteq in | |
[Yy]*) rm -rf "$nlogdir" ; break ;; | |
[Nn]*) break ;; | |
esac | |
done | |
exit 0 | |
#EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment