I have created a small bash-script which check and deletes obsolete php-fpm configs (from phpswitcher bug) via cron job.
Functions:
- backup of the config files to be deleted
- delete obsolete php-fpm config files
- restart of phpswitcher PHP services
- E-Mail notification
Use at your own risk - please test in a V-Box before use it!
Suggestions for improvement are welcome.
usage:
apt install mailutils
nano /root/check-php-conf.sh
copy & paste the script below
set your imscp database user & password, email and backup path
chmod + x /root/check-php-conf.sh
run /root/check-php-conf.sh via cron job (period 5-15 minutes)
## edit ##
added missing /root path to the awk line
Shell-Script
- #!/bin/sh
- ##
- # nano /root/check-php-conf.sh
- # chmod +x /root/check-php-conf.sh
- # run /root/check-php-conf.sh via cron job
- ##
- # imscp database details
- MyUSER="imscp_user"
- MyPASS="xxx"
- DB_NAME="imscp"
- # do not change MyHOST variable
- MyHOST="mysql"
- # Servername for email notification
- HostName="Host1"
- # mail package need installed (apt-get install mailutils)
- AdminEmail="hostmaster@mydaomin.tld"
- # backup path before files are deleted
- BackupPHP="/backup/php"
- # do not change
- CONNECT="$MyHOST -u $MyUSER -p$MyPASS $DB_NAME -e"
- # check connection result
- RCOUNT=$($CONNECT "SELECT COUNT(version_fpm_pool_directory_path) FROM php_switcher_version WHERE version_is_default = 0;" -B --skip-column-names)
- if [ $RCOUNT -gt 0 ]
- then
- # get pathes
- paths=$($CONNECT "SELECT version_fpm_pool_directory_path FROM php_switcher_version WHERE version_is_default = 0;" -B --skip-column-names)
- # get stored config paths from database
- $CONNECT "SELECT e.version_id, e.admin_id, e.domain_name, u.version_fpm_pool_directory_path FROM php_switcher_version_admin AS e, php_switcher_version AS u WHERE e.version_id = u.version_id AND u.version_is_default = 0;" -B --skip-column-names | while read -r data
- do
- data1=$(echo $data | awk '{print $4}')
- data2=$(echo $data | awk '{print $3}')
- domain="$data1"/"$data2"\.\conf
- echo $domain >> /root/fpm-db.txt
- done
- # get paths of existing fpm-files
- find $paths -type f \( -iname "*.conf" ! -iname "www.conf" \) > /root/fpm-files.txt
- # store results in /root/fpm-files.txt
- awk 'NR==FNR{lines[$0];next} !($0 in lines)' /root/fpm-db.txt /root/fpm-files.txt > /root/fpm-del.txt
- # count results from /root/fpm-files.txt
- a=$(awk 'END{ print NR }' /root/fpm-del.txt)
- # if result > 0 delete obsolete files and restart services
- if [ $a -gt 0 ]
- then
- # create backup
- if [ -d "$BackupPHP" ]; then
- xargs -r </root/fpm-del.txt cp --target-directory=$BackupPHP
- else
- mkdir -p $BackupPHP
- xargs -r </root/fpm-del.txt cp --target-directory=$BackupPHP
- fi
- # delete php-fpm config files from list
- if [ ! -f '/root/fpm-del.txt' ]
- then
- echo "File does not exist. Skipping..."
- exit 1
- else
- cat /root/fpm-del.txt
- rm -f `cat /root/fpm-del.txt`
- # email list to admin
- cat /root/fpm-del.txt | mail -s "PHP config deleted - $HostName" $AdminEmail
- # restart php services
- $CONNECT "SELECT version_name, version_is_packaged FROM php_switcher_version WHERE version_is_default = 0;" -B --skip-column-names | while read -r version
- do
- version1=$(echo $version | awk '{print $1}')
- version2=$(echo $version | awk '{print $2}')
- if [ $version2 -eq 0 ]
- then
- restart="/bin/systemctl restart psw'$version1'-fpm.service"
- echo $restart >> /root/fpm-restart.sh
- chmod +x /root/fpm-restart.sh
- /root/fpm-restart.sh
- echo restart psw"$version1"-fpm service
- if [ ! -f '/root/fpm-restart.sh' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-restart.sh'
- fi
- else
- restart="/bin/systemctl restart php'$version1'-fpm.service"
- echo $restart >> /root/fpm-restart.sh
- chmod +x /root/fpm-restart.sh
- /root/fpm-restart.sh
- echo restart php"$version1"-fpm service
- if [ ! -f '/root/fpm-restart.sh' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-restart.sh'
- fi
- fi
- done
- fi
- fi
- # delete all temp compare files
- if [ ! -f '/root/fpm-files.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-files.txt'
- fi
- if [ ! -f '/root/fpm-db.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-db.txt'
- fi
- if [ ! -f '/root/fpm-del.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-del.txt'
- fi
- # exit on errors
- else
- # show error message if temp file exist or mysql query fails & exit
- echo "mysql error occurred or temp files exist"
- # delete tmp compare files
- if [ ! -f '/root/fpm-files.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-files.txt'
- fi
- if [ ! -f '/root/fpm-db.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-db.txt'
- fi
- if [ ! -f '/root/fpm-del.txt' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-del.txt'
- fi
- if [ ! -f '/root/fpm-restart.sh' ]
- then
- echo "File does not exist. Skipping..."
- else
- rm '/root/fpm-restart.sh'
- fi
- # send error message email
- echo "an error occurred when deleting PHP config files" | mail -s "PHP config del error - $HostName" $AdminEmail
- exit 1
- fi