Skip to content

Instantly share code, notes, and snippets.

@drewreece
Last active March 8, 2020 20:25
Show Gist options
  • Save drewreece/7609424 to your computer and use it in GitHub Desktop.
Save drewreece/7609424 to your computer and use it in GitHub Desktop.
Gather all the logs on a Mac.Gather copies in a folder next to this script…Logs/DateStamp/Hostname/
#!/bin/sh
#
# get-logs.sh (rename to get-logs.command to make clickable version)
# Get all the logs on a Mac
#
# Requires Admin account & password.
# Results end up in directory next to the script
# Logs/Date/hostname/folders
#
echo `basename "$0"` "\nRequires Admin account & password.\t [ctrl+c to abort]"
# setup logs destination with date & hostname
HOST=`/bin/hostname`
DATE=`/bin/date "+%v_%H%M%S"`
SCRIPTLOC=`dirname "$0"`
DESTINATION="$SCRIPTLOC/Logs/$DATE/$HOST"
WHO=`/usr/bin/whoami`
# create destination
/bin/mkdir -p "$DESTINATION"
# Get System logs ...
sudo cp -r /var/log/ "$DESTINATION/var-log/"
sudo cp -r /Library/Logs/ "$DESTINATION/Library-Logs/"
## uncomment next 2 lines to ONLY get the current User logs
#echo "Getting current user's logs - ($WHO) ..."
#sudo cp -r /Users/$WHO/Library/Logs/ "$DESTINATION/$WHO-Library-Logs/"
## uncomment entire 'for' statement for ALL USERS logs
for DIR in $(sudo find /Users/*/Library/ -maxdepth 0)
do
# Split out username
USER=`echo $DIR | /usr/bin/awk -F"/" '''{print $3}'`
echo "Getting logs for $USER"
# copy out to $DESTINATION/User-$1-Library-Logs/
sudo cp -r $DIR/Logs/ "$DESTINATION/User-$USER-Library-Logs/"
#sudo chmod -R 755 "$DESTINATION"
# Make current user own the logs
sudo chown -R "$WHO" "$DESTINATION"
sudo find "$DESTINATION" -type d -exec sudo chmod -R 755 {} \;
sudo find "$DESTINATION" -type f -exec sudo chmod -R 644 {} \;
done
# End of ALL USERS
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment