Server Automation Tips on cPanel Server

Are you happy with your current hosting provider ?

In this article, let’s discuss Server Automation Tips on cPanel Server.

Server Automation is essential at times to undertake critical operational tasks when there is no other way to do so from the control panel. cPanel is a remarkable control panel with nearly all the available tools and scripts to do the desired operation. cPanel does not support or facilitate the execution of a large number of such automation operations. In this article, we will explore some small techniques for server automation with cPanel.

What exactly is Server Automation?

It is essentially a little function or task in which the DevOps engineer writes a specific script in bash or Perl. Bash Shell provides the ability to execute many commands sequentially in script format. This script is commonly referred to as a bash or shell script. Bourne shell (sh), designed by Brian Fox and initially released in 1989, superseded the Bash shell.

The script will automate a number of server administration tasks, saving us both time and effort. In a shell script, we are able to generate logic for the various administrator challenges and automatically trigger them without requiring user input. It will also boost work efficiency and decrease human mistakes by managing workflows in such a way that they function automatically without the intervention of administrators.

Traditional server management responsibilities consist of hundreds of infrastructure components and are therefore highly difficult. This is where server automation occurs in order to simplify administrative tasks and respond rapidly to difficulties by developing the appropriate logic.

Bash shell provides various features, such as user-defined Variables, System-defined Variable, Command Substitution, Positional Parameters, Parameter Expansion, Subshells, Flow Control, Test Conditions, Iteration statements, Function, Array, Dictionary, Case and select statements, Parallel execution, etc., to enable the development of robust and powerful scripts to address numerous server-level changes.

Imagine a security emergency in which you need to reset the passwords of all users’ accounts simultaneously. We are aware that it is not a best practice to reset passwords with a script, but it can be difficult to ask every user to reset their password in an emergency security issue.

Here is an example of a bash script that enables us to reset the cpanel account passwords for all domains and store them in a file that can be subsequently downloaded and used to provide the passwords. 

#!/bin/bash

for i in $(cut -f2 -d ‘:’ /etc/trueuserdomains)

do

p=$(tr -cd ‘[:alnum:]’ < /dev/urandom | fold -w32 | head -n1)

whmapi1 passwd user=${i} password=”${p}”

echo “${i} : ${p}” >> .cred

done

column -t .cred > user_pass_update.txt && rm -f .cred

Consider another scenario in which you are restoring a server and wish to generate command-line backups of all accounts. This script can be used to generate backups of all cPanel user accounts and store them in the /backup directory:

#!/bin/bash

cd /backup

mkdir ACCOUNT_BACKUP && cd $_

for i in $(cut -f2 -d ‘:’ /etc/trueuserdomains)

do

/scripts/pkgacct ${i} ${PWD} >> backup.log

echo “${i} – completed” >> status.log

done

Another situation that may emerge during server restoration is the need for a backup of only the databases. The script below can be used to create a backup of all user account databases on the current path.

#!/bin/bash

mkdir MYSQL_BACKUP && cd $_

mkdir etc dumps

cp -a /etc/my.cnf etc

cd dumps

for i in $(mysql -sse ‘show databases’ | egrep -v ‘sys|information_schema|performance_schema’)

do

mysqldump ${i} > ${i}.sql

done

The script below is used to restore all user accounts from a cPanel backup; the backup path will be prompted during execution. The script requires the location of account backup files placed on archive files such as EG: username.tar.gz

#!/bin/bash

read –p “Please enter the account backup files location [EG: /backup/date/accounts/]: ” location

cd ${location}

for i in *.gz

do

/scripts/restorepkg ${i} ${PWD} >> restoration.log

echo “${i} – completed” >> status.log

Done

Conclusion

Server Automation is essential at times to undertake critical operational tasks when there is no other way to do so from the control panel. cPanel does not support or facilitate the execution of a large number of such automation operations. It is essentially a little function or task in which the DevOps engineer writes a specific script in bash or Perl.