Created
June 1, 2016 07:18
-
-
Save rickowski/6d43cae68c9a0ae834afab927f1e7645 to your computer and use it in GitHub Desktop.
Read all activity from owncloud mysql database
This file contains hidden or 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 | |
# Copyright © 2016 Ole Rickowski | |
# This work is free. You can redistribute it and/or modify it under the | |
# terms of the Do What The Fuck You Want To Public License, Version 2, | |
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
# Path to the owncloud installation (no ending slash) | |
OC_PATH="/var/www/owncloud" | |
function main() { | |
check_oc_path | |
get_mysql_creds | |
get_activities | |
} | |
function usage() { | |
#Print usage information | |
echo "Usage: $0 [OPTIONS]" | |
echo -e "\nOptions:" | |
echo -e "\t-n <NUMBER>\tShow only last <NUMBER> of lines" | |
echo -e "\t-r\tPrint in reverse order" | |
echo -e "\t-h\tPrint this message" | |
} | |
function check_oc_path() { | |
#Check if the set path to owncloud is correct | |
if [ ! -f "$OC_PATH/config/config.php" ]; then | |
echo "Error: The given path to the owncloud installation is not correct! Please change it the scriptfile." | |
exit 1 | |
fi | |
} | |
# Get mysql vars from owncloud config | |
function get_mysql_creds() { | |
while IFS='' read -r line || [[ -n "$line" ]]; do | |
case $line in | |
*"dbname"*) | |
MYSQL_DB=$(echo $line | cut -d"'" -f 4) | |
;; | |
*"dbhost"*) | |
MYSQL_SRV=$(echo $line | cut -d"'" -f 4) | |
;; | |
*"dbtype"*) | |
MYSQL_TYPE=$(echo $line | cut -d"'" -f 4) | |
;; | |
*"dbuser"*) | |
MYSQL_USER=$(echo $line | cut -d"'" -f 4) | |
;; | |
*"dbpassword"*) | |
MYSQL_PASS=$(echo $line | cut -d"'" -f 4) | |
;; | |
*"dbtableprefix"*) | |
MYSQL_PREFIX=$(echo $line | cut -d"'" -f 4) | |
;; | |
esac | |
done < "$OC_PATH/config/config.php" | |
check_db_type | |
} | |
function check_db_type() { | |
#Check if the db type is mysql | |
if [ ! $MYQL_TYPE == "mysql" ]; then | |
echo "Error: The database type is not mysql." | |
exit 1 | |
fi | |
} | |
function get_activities() { | |
DB_NAME=$MYSQL_PREFIX | |
DB_NAME+="activity" | |
#Create temp file and write activity list to it | |
TMPQUERY=$(mktemp) | |
if [ $LIMITER ]; then | |
mysql -h $MYSQL_SRV -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "select * from $DB_NAME order by activity_id desc limit $LIMITER;" \ | |
| awk -F$'\t' '{print $1";"strftime("%Y-%m-%d %H-%M-%S", $2)";"$4";"$5";"$9";"$12";"$13}' > "$TMPQUERY" | |
else | |
mysql -h $MYSQL_SRV -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "select * from $DB_NAME;" \ | |
| awk -F$'\t' '{print $1";"strftime("%Y-%m-%d %H-%M-%S", $2)";"$4";"$5";"$9";"$12";"$13}' > "$TMPQUERY" | |
fi | |
printList | |
} | |
function printList() { | |
#Print the activity list | |
if [ $LIMITER ] && [ "$REVERSE" == "true" ]; then | |
cat "$TMPQUERY" | |
elif [ $LIMITER ] || [ "$REVERSE" == "true" ]; then | |
#Reverse output from tempfile | |
head "$TMPQUERY" -n1 | |
tac "$TMPQUERY" | head -n-1 | |
else | |
cat "$TMPQUERY" | |
fi | |
removeTemp | |
} | |
function removeTemp() { | |
rm "$TMPQUERY" | |
} | |
# Check arguments | |
while getopts ":n:rh" opt; do | |
case $opt in | |
r) | |
REVERSE=true | |
;; | |
n) | |
if [[ $OPTARG =~ ^[0-9]+$ ]]; then | |
LIMITER=$OPTARG | |
else | |
usage | |
exit 1 | |
fi | |
;; | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
#Triggered when found invalid argument | |
echo -e "Invalid option: -$OPTARG\n" | |
usage | |
exit 1 | |
;; | |
:) | |
#Triggered when required parameter is missing | |
echo -e "Option -$OPTARG requires an argument.\n" | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
trap removeTemp SIGINT SIGTERM | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment