简体   繁体   中英

Commands Working In Terminal, But Not In Bash Script

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 !!

${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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM