2

I'm sysadmin for couple of webservers deployed with cpanel hosting panel. I'm trying to finish up with a backup script. There are two commands bundled with Cpanel, that will be used in this script. These commands are;

1. whmapi1 modifyacct user=USERNAME BACKUP=[01]

This Command has booleans to set, what it does is either enable or disable backup for a specific user.

2. /usr/local/cpanel/bin/backup --force

Once backup is enabled for a user/users, then this command starts the backup process on the server.

So here is my script logic & the script.

#!/bin/bash

Arrays

L=($( comm -23 <(du -h --max-depth=1 /home 2>/dev/null | grep G |  awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort) ))

Above Array contains all the account whose home directories have exceeded 1GB limit.

S=($(comm -23 <(du -h --max-depth=1 /home 2>/dev/null | egrep -v '(!G|.cp|cP|clamav)' | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort) ))

Above Array contains all the account whose home directories are less than 1GB limit.

whmapi1 modifyacct user=${L[@]} BACKUP=0 && whmapi1 modifyacct user=${S[@]} BACKUP=0

Above command disables backup for all users for start, to start from scracth.

whmapi1 modifyacct user=${S[@]} BACKUP=1

T

his command enables backup for all accounts whose home dirs are less than 1 GB

/usr/local/cpanel/bin/backup --force

This command starts backup process for all enabled users.

The logic is, that I want to create backup of small accounts first, and then when it's finished, I'll run it for larger accounts.

PROBLEM: all commands execute successfully when run directly in terminal, but it doesn't when run via a script. Problem occurs at account enabling & disabling. It either disables all or enables all, and not the partial accounts, as intended by the logic of the script.

Can anyone point out, where & what I'm missing? Thanks in advance !!

4
  • Your script doesn't seem to define the arrays S and L anywhere; so they're empty. Commented Jul 15, 2018 at 7:06
  • @tripleee but there are 2 arrays at the beginning of file. Doesn't = sign define arrays in bash scripting? Commands after = sign fetch list of accounts tha meet my criteria. I've used echo command to get list of accounts from those arrays, and it works. Commented Jul 15, 2018 at 7:33
  • Running du -h twice is such a bottleneck. So L is an array of usernames. How should the whmapi line look like, like this: whmapi ... user=user1 user=user2 user=user3 user=... or whmapi .... user=user1 user2 user3 user3 .... or foreach user run whmapi .. user=user_i? Commented Jul 15, 2018 at 7:40
  • @KamilCuk it should enable/disable backups for every user in arrayed list, one by one, like a loop. Commented Jul 15, 2018 at 7:51

1 Answer 1

2

${l[@]} exands to user1 user2 user3 ..., so user=${L[@]} expands to user=user1 user2 user3 ..., if you want to fun foreach user, you need to loop over users.

du_buff=$(du -h --max-depth=1 /home 2>/dev/null)
lock_buff=$(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort)
L=($(comm -23 <(echo "$du_buff" | grep G |  awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(echo "$lock_buff") ))
S=($(comm -23 <(echo "$du_buff" | egrep -v '(!G|.cp|cP|clamav)' | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(echo "$lock_buff") ))

# for every user in L and S
for user in "${L[@]}" "${S[@]}"; do
     whmapi1 modifyacct user=$user BACKUP=0
done
# for every user in S
for user in "${S[@]}"; do
     whmapi1 modifyacct user=$user BACKUP=1
done
/usr/local/cpanel/bin/backup --force
Sign up to request clarification or add additional context in comments.

5 Comments

Ok the step where accounts backup is enabled/disabled, commands still don't work as desired. It disables backup for all accounts, then enables for all, instead of just small accounts, I.e, ${S[@]}. I think there's something wrong with using 2 loops in one script.
There's nothing wrong in using 2 loops. Check if the users in S and L are correct. Maybe do set -x on the beginning of the script to see what is happening.
thanks Kamil, found out where problem lies. Your code is valid, I'm just greping improper list.
may I ask for one more help? That backup command in script is followed by a loop disabling backup for all, and another loops enabling for 1G backup, and then backup command. But it seems the 1st backup command doesn't go to process tree, when next loop starts. My question is, is it skipping/terminating backup command? If terminating, how can I enforce it to finish complete backup task after starting loop after backup command?
What? Do set -x to see what is happening in your script. Does whmapi1 work as an asynchronous tool? If so, maybe add a sleep, or search on how to create a synchronization point between whmapi1 and your script, for example i guess by querying if backup is enabled or disabled before doing the backup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.