piwik/OLD/trigger_piwik_archives.sh.01

201 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
PATH=/usr/local/apache2/bin:/usr/local/php/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
stats_base_dir="/var/www/stats.warenform.de"
php_bin=/usr/local/php/bin/php
http_user=www-data
# - MySQL / MariaDB credentials
# -
# - Giving password on command line is insecure an sind mysql 5.5
# - you will get a warning doing so.
# -
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
# - commandline parameter '--defaults-file'.
# -
# - Since Mysql Version 5.6, you can read username/password from
# - encrypted file.
# -
# - Create (encrypted) option file:
# - $ mysql_config_editor set --login-path=piwik_admin --socket=/tmp/mysql.sock --user=piwik_admin --password
# - $ Password:
# -
# - Use of option file:
# - $ mysql --login-path=local ...
# -
# - Example
# - mysql_credential_args="--login-pat=piwik_admin"
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
mysql_credential_args="--login-path=piwik_admin"
error_log="/tmp/$$.err"
# --------------------------------------------------- #
# ------------------- Funktionen -------------------- #
#
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: ` basename $0` [Options ]
Script archives piwik reports.
Options:
-h Prints this help.
-v Verbose Mode
Applies only, if script is running in a console, or in otherwords:
parameter has no effect, if script is running as a cronjob.
EOF
exit
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
error (){
echo ""
echo -e "\t[ \033[31m\033[1mError\033[m ]: $*"
echo ""
}
error_cron(){
echo
echo "----"
echo "$1"
echo
echo "$2"
echo
}
echo_ok() {
echo -e "\033[85G[ \033[32mok\033[m ]"
## echo -e " [ ok ]"
}
echo_failed(){
echo -e "\033[85G[ \033[1;31mfailed\033[m ]"
## echo -e " [ failed ]"
}
#
# ------------------ Ende Funktionen ---------------- #
# --------------------------------------------------- #
VERBOSE=false
# ---
# - Read in Commandline arguments
# ---
while getopts hv opt ; do
case $opt in
v) VERBOSE=true
;;
h) usage
;;
esac
done
cd /tmp
databases=`mysql $mysql_credential_args -N -s -e "show databases" | grep piwik_`
if $LOGGING ; then
echo -e "\n\t\033[1;32mStarting Script for Archiving Reports of Piwik Databases\033[1;37m\033[m\n\n"
fi
declare -i sum_errors=0
for db_name in $databases ; do
if [ -f "$stats_base_dir/$db_name/console" ];then
if $LOGGING ; then
echononl "\tArchiving Reports of Piwik Database \033[1m$db_name\033[m"
# - Archive Reports
# -
su $http_user -s /bin/bash \
-c "$php_bin $stats_base_dir/$db_name/console core:archive --url=https://stats.warenform.de/$db_name/" \
> $error_log 2>&1
if [[ $? -ne 0 ]]; then
let "sum_errors += 1"
echo_failed
error "$(cat $error_log)"
else
echo_ok
if $VERBOSE ; then
echo
echo "$(cat $error_log)"
echo
echo
fi
fi
else
# - Archive Reports
# -
su $http_user -s /bin/bash \
-c "$php_bin $stats_base_dir/$db_name/console core:archive --url=https://stats.warenform.de/$db_name/" \
> $error_log 2>&1
if [[ $? -ne 0 ]]; then
let "sum_errors += 1"
error_cron "Error while executing archive script for database \"$db_name\"" "$(cat $error_log)"
fi
fi
else
if $LOGGING ; then
error "Archive script \"$stats_base_dir/$db_name/console\" for database \"$db_name\" not found!"
else
error_cron "Error: archive script for database \"$db_name\" not found" "$stats_base_dir/$db_name/console"
fi
continue
fi
done
if $LOGGING ; then
if [[ $sum_errors -eq 0 ]]; then
info "Script ended successfully."
else
error "Script ended with \033[1m${sum_errors}\033[m error(s)!"
fi
fi
rm -f $error_log
exit 0