Skip to content

Instantly share code, notes, and snippets.

@vegaasen
Last active December 7, 2017 17:40
Show Gist options
  • Save vegaasen/0b291e3ed2cf06962a06dc1c997010d0 to your computer and use it in GitHub Desktop.
Save vegaasen/0b291e3ed2cf06962a06dc1c997010d0 to your computer and use it in GitHub Desktop.
LogArchiver2000: Simple archiver file that grabs files based on filenames and compresses them.. or something like that :-P
#!/bin/bash
#
# @description This script will simply just archive files which is of age month-1->n.
# @author vegaasen
# @since 16.12.2014
#
# Configuration
ARCHIVE_FOLDER_ID='logs-archive';
ARCHIVE_FOLDER="/home/<you>/iam1/$ARCHIVE_FOLDER_ID";
LOG_FOLDER="/home/<you>/iam1/logs";
DAYS_BACK=30;
## Logging of the output from the script itself
OUTPUT_FILE="logArchiver2000.out"
OUTPUT_PATH="/tmp/$OUTPUT_FILE";
function printUsage() {
echo "Usage: ./log_archive.sh {candidates,archive,count} '<file-pattern>'";
echo "ARCHIVE-Example: ./log_archive.sh archive \"cool-[0-9+]*\"";
echo "CANDIDATES-Example: ./log_archive.sh candidates \"cool-[0-9+]*\"";
echo "COUNT-Example: ./log_archive.sh count \"cool-[0-9+]*\"";
echo "This will remove the files of the <file-pattern> for the previous month(s). E.g, if we're in January now, then all other months then January of the current year will be handled.";
exit -1;
}
function verifyParameters() {
if [ $# -lt 2 ]; then
printUsage;
fi
}
function conditionallyCreateArchiveFolder() {
if [ ! -d $ARCHIVE_FOLDER ]; then
echo "Folder $ARCHIVE_FOLDER does not exists. Creating it.";
mkdir $ARCHIVE_FOLDER;
fi
}
function archiveFiles() {
FILE_PATTERN=$1;
echo "Will try to archive file based on filePattern $FILE_PATTERN";
if [ -z $FILE_PATTERN ]; then
echo "Unable to operate on empty string.";
printUsage;
fi
printNumbersOfCandidates $FILE_PATTERN;
find $LOG_FOLDER -name "$FILE_PATTERN" -type f -mtime +$DAYS_BACK -exec gzip -1 {} \;
find $LOG_FOLDER -name "$FILE_PATTERN" -type f -mtime +$DAYS_BACK -exec mv {} $ARCHIVE_FOLDER/. \;
}
function doIt() {
conditionallyCreateArchiveFolder;
archiveFiles $1;
}
function printNumbersOfCandidates() {
FILE_PATTERN=$1;
NUM_FILES=`find $LOG_FOLDER -name "$FILE_PATTERN" -type f -mtime +$DAYS_BACK -exec ls -l {} \; | wc -l`;
echo "There were $NUM_FILES which is flagged and ready for archiving into folder $ARCHIVE_FOLDE";
}
function detectArchiveCandidates() {
FILE_PATTERN=$1;
find $LOG_FOLDER -name "$FILE_PATTERN" -type f -mtime +$DAYS_BACK -exec ls -l {} \;
printNumbersOfCandidates $2;
}
echo "************************************************************";
echo "************************************************************";
echo "************************************************************";
echo "";
echo 'Weclome to the LogArchiver2000! This script will archive logs and stuff..';
echo "";
echo "************************************************************";
echo "Defaults: LogFolder: $LOG_FOLDER -- DAYS_BACK: $DAYS_BACK -- Archive Folder: $ARCHIVE_FOLDER_ID -- Archive Path: $ARCHIVE_FOLDER";
echo "************************************************************";
echo "************************************************************";
verifyParameters $1 $2;
case "$1" in
'archive')
echo "Initiate the archiving. Point of no return :-)!";
doIt $2;
exit 1;
;;
'candidates')
echo "Printing all files which is flagged for archiving";
detectArchiveCandidates $2;
exit 1;
;;
'count')
echo "Detecting number of candidates";
printNumbersOfCandidates $2;
exit 1;
;;
*)
printUsage;
exit -1;
;;
esac;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment