Server Automation Tips on cPanel server

Server Automation

Server Automation Tips on cPanel server

Server Automation at some point is required to execute critical operational task when you really don’t have any option doing it from the control panel itself. cPanel is an amazing control panel with almost all the tools and scripts available to perform the task as per your needs. However, there are many such automation tasks which we cannot perform or don’t have it in cPanel. Today we will discuss some small tips about server automation for cPanel in this article.




So, what is Server Automation? 

It’s basically a small function or task where the DevOps engineer will build a particular script using bash or Perl language. Bash Shell provide us feature to run the many commands sequentially in a format of script. This script is known has bash or shell script. Bash shell is succeeded by Bourne shell (sh) created by Brian Fox and Initially released on 1989.

The script will perform various administration operations inside the server environment in an automated way thus allowing us to save time and repetitive work. In a shell script we are able to create a logic for the various administrator challenges and invoke them automatically without any user interaction. It will also increase efficiency of work and reduces human error by managing the workflows in a way that they work automatically without any interaction of administrators.

The traditional server management tasks are quite complex, consisting of hundreds of infrastructure components. This is where server automation take place to be able to reduce various administration task and respond quickly by creating proper logic to challenges

Bash shell provide us different features such as user defined Variable, System defined Variable, Command Substitution, Positional Parameters, Parameter Expansion, Subshells, Flow Control, Test Condition, Iteration statement, Function, Array, Dictionary, Case and select statement, Parallel execution and etc.. to build powerful and robust scripts to solve many server level changes.

Imagine a security situation where you want to reset account passwords for all users at one go. We know It’s not a good practice to reset passwords using a script, however it can be challenging to ask every user to reset the password considering urgent security situation.

Here is one example of a particular bash script which allows us to forcefully reset the cpanel account passwords for all domains and store it file which later can be downloaded and providing passwords.
Note:- We will recommend testing the scripts on your staging servers first before executing on live servers.

#!/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 situation where you are doing a server restoration and wanted to generate backups of all accounts through command line.  The following script can be used to generate the backups of all cPanel user accounts via cli and store it on /backup location:

#!/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 can arise while doing server restoration is that you want to have backup of only the databases. The following script can be used to generate the backup of all user accounts databases on 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

Below script is used to the restore all user accounts from the cPanel backup, the backup path location will be asked interactively during execution. The script requires location of account backup files which are stored on archive files 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

Thinking about taking the next step in your business? Contact 24x7servermanagement to get in touch for more details about how server automation, devops services can be easily implemented at your infrastructure.

 

Share this post


24x7servermanagement