Create an auto cPanel backup script - bash

I want to create a script at a cPanel / linux server.
I want it to check on all possible errors.
#/bin/bash
cpanel_username=$1
domain=$2
if [ -z "$cpanel_username" ]
then
echo "no username given"
elif [ -z "$domain" ]
then
echo "no domain given"
else
/scripts/pkgacct $cpanel_username /backup/
mv /backup/cpmove-$cpanel_username.tar.gz /backup/$cpanel_username.tar.gz
FILE="/backup/$cpanel_username.tar.gz"
FTPFILE="$cpanel_username.tar.gz"
if [ -f "$FILE" ]
then
### FTP login credentials bellow ###
FTPU="ftpuser"
FTPP="ftpass"
FTPS="host"
lftp -u $FTPU,$FTPP -e "cd /hosting_backups;put $FILE;quit" $FTPS
######HERE I WANT TO CHECK IF THE FILE IS UPLOADED
rm /backup/$cpanel_username.tar.gz
else
echo "couldnt take backup of $domain"
fi
fi
Also i would like to know if i can check if there is enough space for me to take backup. I want this to be done before the backup command.
I have tried many hours , but i couldnt make it . Any help is appreciated

Related

How do I use an if statement inside a for loop to check multiple servers to see if a directory exists/to create a directory

I am trying to get my script to ssh to multiple servers and check to see if a directory needs to be created or already exists.
I noticed the script that I have created runs with no errors but it creates the directory needed on "server1" but then when it reaches "server2" is says (echo)"directory is already created" but the directory is not present in server2.From my understanding after it creates the directory on "server1" it then recognizes that it was created on "server1" so it falls to (echo)"directory is already created".
I want to use a for loop with an if statement to make it run through all the servers specified so It can tell me if each server has that directory or needs that directory to be created.
#!/bin/bash
host=(server1 server2)
HOME_PATH=/opt/test
for my_hosts in "${host[#]}"
do
if [ ! -d "$HOME_PATH" ]
then
echo "${my_hosts}"
ssh -o "StrictHostKeyChecking no" root#${my_hosts} 'mkdir -p '${HOME_PATH}''
echo "${HOME_PATH} was created"
else
echo "${my_hosts}"
echo "'${HOME_PATH}' directory is already created"
fi
done
Thank You for the help. I figured it out.
#!/bin/bash
host=(server1 server2)
HOME_PATH=/opt/test
for my_hosts in "${host[#]}"
do
ssh -q -o "StrictHostKeyChecking no" root#${my_hosts} "
if [ ! -d "$HOME_PATH" ]
then
echo "${my_hosts}"
mkdir -p '${HOME_PATH}
echo "${HOME_PATH} was created"
else
echo "${my_hosts}"
echo "'${HOME_PATH}' directory is already created"
fi
"
done

How to know if file exsits ina Samba share

I wrote a shellscript as follows, to check for a file in a samba share:
date_gen=$(date --date="3 days ago" +"%-Y%m%d")
fileName=${date_gen}"_Combined Reg Report.xlsx"
if [ ! -f smb://nfs/carboard/"${fileName}" -U ]
then
echo "File does not exist in Bash"
else
echo ${fileName}
fi
exit 1
Can someone please help me what is wrong with this, I am always getting "File does not exist in Bash". File is there in the folder.
Thanks,
Art
You should check if it's mounted and then check for the file
if mount | grep -q /nfs/cardboard
then
if [[ ! -f /nfs/cardboard/"${fileName}" ]]
then
...
fi
else
echo "not mounted"
fi
Checking the existence of a file with smbclient:
filename="$(date --date='3 days ago' '+%Y%m%d')_Combined Reg Report.xlsx"
if smbclient -A smbauth.conf '//nfs/carboard' -c "ls \"$filename\"" > /dev/null 2>&1
then
echo the file exists
else
echo the file is not there
fi
where smbauth.conf is a file storing your credentials in the following format:
username=myuser
password=mypassword
domain=MYDOMAIN
I don't know how escaping exactly works with smbclient (it seems like some characters like " are impossible to escape), but in your case, double-quoting is enough.

How do I check to see if a file exists on a remote server using shell

I have done a lot of searching and I can't seem to find out how to do this using a shell script. Basically, I am copying files down from remote servers and I want to do something else if it doesn't exist. I have an array below, but I tried to reference it directly, but it is still returning false.
I am brand new at this, so please be kind :)
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"
do
if [ -f "$i:/home/user/directory/file" ];
then
do stuff
else
Do other stuff
fi
done
Try this:
ssh -q $HOST [[ -f $i:/home/user/directory/file ]] && echo "File exists" || echo "File does not exist";
or like this:
if ssh $HOST stat $FILE_PATH \> /dev/null 2\>\&1
then
echo "File exists"
else
echo "File not exist"
fi
Assuming you are using scp and ssh for remote connections something like this should do what you want.
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"; do
if ssh -q "$i" "test -f /home/user/directory/file"; then
scp "$i:/home/user/directory/file" /local/path
else
echo 'Could not access remote file.'
fi
done
Alternatively, if you don't necessarily need to care about the difference between the remote file not existing and other possible scp errors then the following would work.
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"; do
if ! scp "$i:/home/user/directory/file" /local/path; then
echo 'Remote file did not exist.'
fi
done

Bash: If statement nested in for loop

I am writing a simple script to check if an entered directory path exists. This is what I have
echo "Please specify complete directory path"
read file_path
for file in $file_path; do
if [[ -d "$file" ]]; then
echo "$file is a directory"
break
else
echo "$file is not a directory, please try again."
fi
done
What I need is if it is not a directory to go back and ask for the file path again.
Thanks.
How about this?
echo "Please specify complete directory path"
while read file; do
if [[ -d "$file" ]]; then
echo "$file is a directory"
break
fi
echo "$file is not a directory, please try again."
done
No need to split the path into its parts, testing the entire path with -d will tell you whether or not it is a directory. You need to put the entire test into a while loop until the user gets it right:
#/bin/sh
set -e
file_path=''
while [ ! -d "$file_path" ]
do
echo "Please specify complete directory path"
read file_path
if [ ! -d "$file_path" ]
then
echo "$file_path is not a directory, please try again."
fi
done
I can use it like this
$ sh /tmp/test.sh
Please specify complete directory path
foobar
foobar is not a directory, please try again.
Please specify complete directory path
/var/www

.SH/.COMMAND Bash File Mac OSX Not working

I keep getting this error for my script below when I try to run it
The file “test.command” could not be executed because you do not have appropriate access privileges.
Tried changing the read only to read/write. Didn't work
for domain in $(pwgen -1A0B 6 10000); do echo -ne "$domain.com "; if [ -z "$(whois $domain.com | grep -o 'No match for')" ]; then echo -ne "Not "; fi; echo "Available for register"; done >> domains.txt
Any ideas?
You need to give execute permission as well to execute it.
chmod u+x file

Resources