Select Git revision
uploads.ini
doli 5.88 KiB
#!/bin/bash
get_conf_val () {
grep "^\$dolibarr_$1" dolibarr_src/htdocs/conf/conf.php | cut -d '=' -f 2 | sed -e "s/^[\"']//" -e "s/[\"'];//"
}
APP_SERVICE="dolibarr"
DB_SERVICE="mariadb"
user=$(get_conf_val main_db_user)
db=$(get_conf_val main_db_name)
PASS_VARIABLE="PASS"
DUMP_PATH="dumps/internal.sql.gz"
document_path=$(get_conf_val main_data_root)
is_docker() {
if [ ! -z "$(which docker 2>/dev/null)" ];
then
echo "1"
else
echo "0"
fi
}
usage() {
echo -e "Usage $0 <command> [args]\n"
echo -e "COMMANDS\n"
echo "bash"
echo -e "\t opens a bash terminal in front container or just run bash"
echo "down"
echo -e "\t stops the docker stack"
echo "help"
echo -e "\t displays this messages and exit"
echo "logs"
echo -e "\t Follow all useful logs"
echo "logs <term1> <term2> ..."
echo -e "\t Follow all useful logs and highlight terms"
echo "mysql"
echo -e "\t pen a mysql prompt in LNB database"
echo "mysql_dump"
echo -e "\t creates a database dump"
echo "mysql_init"
echo -e "\t populate LabNbook database (Docker only)"
echo "mysql_restore"
echo -e "\t restores database from a dump"
echo "perms"
echo -e "\t sets default files permissions"
echo "restart"
echo -e "\t restart the docker stack or apache2"
echo "shell"
echo -e "\t run a php shell, same as $0 artisan tinker"
echo "up"
echo -e "\t starts the docker stack"
echo "tags"
echo -e "\t generate ctags"
echo "fail2ban"
echo -e "\t root only - put fail2ban jail and filter in good directory with good log path"
}
if [ "$(is_docker)" -eq 1 ]; then
if [ -z "$(which docker-compose)" ]; then
compose="docker compose"
else
compose="docker-compose"
fi
cmd="$compose exec $APP_SERVICE"
cmdmy="$compose exec $DB_SERVICE"
cmdmyInput="docker exec -i $($compose ps -q $DB_SERVICE)"
cmdrestart="$compose restart"
cmdup="$compose up"
cmddown="$compose down"
else
cmdrestart="apache2ctl restart"
fi
if [ ! -z "$(which ccze)" ]; then
reader="ccze -A"
else
reader="tee"
fi
pass=$(get_conf_val main_db_pass)
mysql="mariadb -u $user $db -p$pass"
DIR="$(dirname $0)"
action=$1
shift
# Keep actions sorted
case $action in
"bash")
$cmd bash
;;
"down")
$cmddown
;;
"help")
usage
;;
"logs")
set -x
# All parameters after $1 are highlight terms
HIGHLIGHT_TERMS=("${@:1}")
# Build sed command: start with highlighting ERR in red
sed_exprs=( -e 's/ERR/\x1b[31m&\x1b[0m/g' )
# Add each additional highlight term in yellow background + black text
for term in "${HIGHLIGHT_TERMS[@]}"; do
# Escape slashes and ampersands
safe_term=$(printf '%s' "$term" | sed 's/[\/&]/\\&/g')
sed_exprs+=( -e "s/$safe_term/\x1b[4;43;97m&\x1b[0m/gI" )
done
# Pipe logs through constructed sed commands and reader
$cmd tail -f \
$document_path/dolibarr.log \
$document_path/dolibarr_payment.log \
$document_path/dolibarr_cron.log \
$document_path/cron_run_jobs.php.log \
$document_path/dolibarr.log \
/var/log/apache2/access.log \
/var/log/apache2/dolibarr_access.log \
/var/log/apache2/dolibarr_error.log \
/var/log/apache2/dolibarr_erro..log\
| LC_ALL=C sed "${sed_exprs[@]}" \
| $reader
set +x
;;
"mysql")
set -x
$cmdmy $mysql
set +x
;;
"mysql_dump")
$cmdmy mariadb-dump --databases $db -u $user -p$pass | gzip > ${db}_$(date +%Y%m%d_%H%M%S).sql.gz
;;
"mysql_restore")
if [ -z "$1" ]; then
echo "Usage $0 mysql_restore <file>"
exit 1
fi
read -p "Do you want to restore your database from file '$1' ? This command will erase your current data. (y/n). " yn
case $yn in
[Yy]* )
zcat $1 | grep -v '/\*M' | $cmdmyInput $mysql
;;
[Nn]* )
exit
;;
* )
echo "Please answer yes or no.";;
esac
;;
"mysql_init")
read -p "Do you want to init your database with default data? This command will erase your current data. (y/n). " yn
case $yn in
[Yy]* )
zcat $DUMP_PATH | $cmdmyInput $mysql
;;
[Nn]* )
exit
;;
* )
echo "Please answer yes or no.";;
esac
;;
"perms")
$cmd chown -R $(id -u):33 dolibarr/
$cmd chown -R $(id -u):33 $document_path
$cmd chmod -R g+rw $document_path
$cmd mkdir -p $document_path/users/temp/odtaspdf/
$cmd chmod g+x $document_path/users/temp/odtaspdf/
$cmd chmod -R g-w dolibarr/htdocs
$cmd chmod -R g+w dolibarr/htdocs/custom
$cmd touch $document_path/install.lock
;;
"restart")
$cmdrestart $@
;;
"shell")
if [ $(is_docker) -eq 1 ]; then
docker cp doli_shell.php $($compose ps -q $APP_SERVICE):/var/www
fi
$cmd php -a -d auto_prepend_file=/var/www/doli_shell.php
;;
"up")
$cmdup $@
;;
"tags")
workdir=$PWD
cd $DIR/dolibarr_src
ctags -R --fields=+aimlS --languages=php
;;
"fail2ban")
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
workdir=$PWD
echo $workdir
read -p "Give name of instance: " name
echo $name
if [ -f "/etc/fail2ban/jail.d/dolibarr_${name}.conf" ]; then
echo "The jail 'etc/fail2ban/jail.d/dolibarr_${name}.conf' alredy exist."
exit
fi
cp $workdir/fail2ban/dolibarr_filter.conf /etc/fail2ban/filter.d/dolibarr.conf
cp $workdir/fail2ban/dolibarr_jail.conf /etc/fail2ban/jail.d/dolibarr_${name}.conf
sed -i "s|LOG_PATH|$workdir/dolibarr_src/documents/dolibarr.log|g" /etc/fail2ban/jail.d/dolibarr_${name}.conf
;;
*)
echo "ERROR: No command given"
usage
exit 1
;;
esac