Convert files to webp

Do you have a folder with many images that you want to convert to webp? Don’t want to do it all by hand? Use this script then. You will need to set the WORKDIR variable, and perhaps change the path to the bash shell (like when you’re on FreeBSD, the path is /usr/local/bin/bash).

Requirements: webp (obviously, see https://developers.google.com/speed/webp/download), bash (duh), basename (should be installed already).

#!/bin/bash

WORKDIR=/path/to/images

# Search for files per extension in subdirectories and convert them to webp.
for EXT in jpg jpeg JPG JPEG ; do

        for DIR in `find $WORKDIR -type d` ; do

                for FILE in `ls -1 $DIR/*.$EXT` ; do
                        FILENAME=`basename --suffix=.$EXT $FILE`
                        /usr/bin/cwebp $FILE -o $DIR/$FILENAME.webp
                done

        done

done
repair and optimize all tables

Sometimes the database daemon crashes (because the server ran out of swap space for example). This can cause the databases to be damaged. Finding and repairing the database table in question can be an arduous task, especially if you have a lot of databases. This script runs both repair and optimize on all tables.

You’ll first need a .my.cnf file in your own directory (/root/.my.cnf if you’re root for example), with the following contents:

[client]
user=YOURUSERNAMEHERE
password=YOURPASSWORDHERE

For instance, if you’re running directadmin, they username is da_admin. The password can be found in /usr/local/directadmin/conf/mysql.conf

This script does work for all situations of MySQL and MariaDB though, so do not get hung up on the username. Just use the username you need to access ALL the databases. Make sure the permissions on the file are 600.

Here’s the (simple) script:

# Finding and repairing the database table in question can be an
# arduous task, especially if you have a lot of databases. This
# script runs both repair and optimize on all tables.
###
# (c)opyleft Take13
#!/bin/bash

MYSQLBIN=/usr/bin/mysql

for DATABASE in `echo "show databases" | $MYSQLBIN | grep -v Database | grep -v information_schema | grep -v performance_schema` ; do
        for TABLE in `echo "show tables" | $MYSQLBIN $DATABASE | grep -v Tables_in_` ; do
                echo $DATABASE"."$TABLE
                echo "repair table "$DATABASE"."$TABLE | $MYSQLBIN
                echo "optimize table "$DATABASE"."$TABLE | $MYSQLBIN
        done
done
Automatically block asshole bots

There’s a great many bots out there that set off so many requests webservers can get swamped. This script searches for particular user agents, finds the originating ip address (ipv4 or ipv6) and then blocks the whole subnet. This is specifically done for yandex and MJ12bot who use a large amount of subnets.

This script uses directadmin’s ConfigServer Security & Firewall (csf) which needs to be installed seperately.

# This script searches for particular user agents, finds the 
# originating ip address (ipv4 or ipv6) and then blocks the whole 
# subnet. This is specifically done for yandex and MJ12bot who use a
# large amount of subnets.
###
# (c)opyleft Take13
#!/bin/bash

# Variables
BADBOTS=adscanner\|AhrefsBot\|Baiduspider\|BLEXBot\|DotBot\|Exabot\|OpenindexSpider\|Pinterestbot\|Seekport\|SeznamBot\|YandexBot\|YandexImages\|MJ12Bot\|ltx71\|Baiduspider\|Qwantify\|OpenindexSpider
LOGPATH=/var/log/httpd/domains/
LOGS=`ls -1 $LOGPATH/*log | grep -v error | grep -v bytes`

# Comment to keep the logfile of blocked ip addresses/net blocks
rm /root/blocked.txt

# Find badbots in logfile and block with iptables
echo "Searching for bad bots in $LOG."

for LOG in `echo $LOGS` ; do
        # This line searches for ip addresses, remove comment to use, make sure the next for statement is commented out
        #for IP in `egrep $BADBOTS $LOG | awk '{print $1}' | awk '{print $1}' | sort | uniq` ; do

        # This line searches for net blocks, remove comment to use, make sure the previous for statement is commented out
        for IP in `egrep -i $BADBOTS $LOG | awk '{print $1}' | sort | uniq | awk '{print "whois -l "$1" | grep route | grep -v mnt"}' | sh | awk '{print $2}' | sort | uniq` ; do

                echo $IP
                echo $IP >> /root/blocked.txt
                csf -d $IP

        done

done
Restart all daemons

Sometimes a (directadmin) server runs out of swap space. It’s nice to know which service exactly is causing it but sometimes you do not have the time. A fast option to restart all services without having to figure out exactly which services you’ve got running (especially now that there’s the possibility of using 4 different versions of PHP).

#!/bin/bash

# Specifically for directadmin/custombuild and CentOS
# Detects (some) options and restarts daemons, then
# shows free swap space
###
# (c)opyleft Take13

OPTIONS=/usr/local/directadmin/custombuild/options.conf 

# Show free swap space
function swapfree {
	echo Swap free: `free -h | grep Swap | awk '{print $4}'`
	}

# Detect running php-fpm instances, restart
for PHP in `systemctl | grep php-fpm | awk -F . '{print $1}'`; do
	echo Restarting $PHP
        systemctl restart $PHP
	swapfree
done

# Detect webserver config, restart
WEBSERVER=`grep webserver $OPTIONS | awk -F = '{print $2}'`

case $WEBSERVER in
apache)		echo Restart apache
		systemctl restart httpd
		swapfree
;;
nginx)		echo Restart nginx
		systemctl restart nginx
		swapfree
;;
nginx_apache)	echo Restart nginx
		systemctl restart nginx
		swapfree
		echo Restart apache
		systemctl restart httpd
		swapfree
;;
litespeed)	echo Restart litespeed
		systemctl restart lsws
		swapfree
;;
esac

# Restart exim
echo "Restart exim"
systemctl restart exim
swapfree

# Restart dovecot
echo "Restart dovecot"
systemctl restart dovecot
swapfree

# Detect database, restart
DB=`grep inst $OPTIONS | awk -F = '{print $2}'`
case $DB in
mysql)		echo Restart MySQL
		systemctl restart mysqld
		swapfree
;;
mariadb)	echo Restart MariaDB
		systemctl restart mariadb
		swapfree
;;
esac

echo "*kusje*"