Jump to content

Will I ever get to grips with linux? Running a script .sh


Recommended Posts

Posted

Ok thanks to webman and others have my Zimbra installation up and running. I can run a number of commands to backup the aforementioned zimbra but I can't save those commands as a script .sh and then run that?

 

I have saved my script as zbackup.sh

 

I then tried runnig it but no go.

I have a read round and find I need to chmod it.

 

I tried chmod it like so

 

$ chmod ugo+rx zbackup.sh

 

but I still get this error when I type "zbackup.sh" at the prompt

 

-bash: zbackup.sh: command not found

 

Am I missing an abvious, to linux, step?

 

Thanks

Posted

Couple of things. Is the file in the same directory you are trying to run the command from? ie is it in your /home/user and your console at that location too?

 

Next, is the file secretly named 'zbackup.sh.txt' or something?

Posted

It's a security feature. As you have already applied the execute permission (chmod +x) you can run the script like this:

 

./zbackup.sh

 

If it doesn't have the execute permission, you need to run it as a parameter:

sh zbackup.sh

  • Thanks 1
Posted

Can I come back in with my next problem now?

 

I have my script, well webmans with some different paths, and I can execute it by typing "sh zbackup.sh" at the prompt BUT when I do execute it I get a response as follows...

 

 

: command not found

: command not found

: command not found

: command not found:

: command not found:

: command not found:

: command not found:

: command not found:

: command not found:

'backup.sh: line 27: syntax error near unexpected token `{

'backup.sh: line 27: `function out {

 

I'm not sure how I have managed to cock up since it works fine if I paste the text straight in to the prompt!

 

#!/bin/bash

#
# Zimbra Backup Script
#


# Variables
TIME=`date +%Y-%m-%d`
DOWN=`date +%u`
DOWD=`date +%A`

SOURCE="/opt/zimbra/"
DEST="/tmp/zcsbackup"

NFSHOST="10.60.28.53"
NFSPATH="/Volumes/Promise/zimbra"
NFSMOUNT="/mnt/mac-1"

ARCHIVENAME="zcsbackup-$DOWN-$DOWD.tar.gz"
ARCHIVE="/tmp/$ARCHIVENAME"

LOG="/var/log/zmbackup.log"
EMAIL="[email protected]"


function out {
echo "["`date +"%Y-%m-%d %T"`"] $1"
}


#echo ""
#echo "* * * * * * * * * *"
#echo "Zimbra backup"
#echo `date +%Y-%m-%d`
#echo ""


echo ""
out "Zimbra backup"
echo ""


echo ""
out "Stopping services"
echo ""


# Stop Zimbra Services
su - zimbra -c /opt/zimbra/bin/zmcontrol stop
sleep 5


echo ""
out "Zimbra status:"
echo ""
su - zimbra -c /opt/zimbra/bin/zmcontrol status
sleep 10


echo ""
out "Making directory and executing Rsync"
echo ""

# Make directory and sync to it
mkdir $DEST
rsync -aHK --exclude=*.pid $SOURCE $DEST


echo ""
out "Restarting Zimbra services"
echo ""

# Restart Zimbra Services
su - zimbra -c /opt/zimbra/bin/zmcontrol start
sleep 5


echo ""
out "Zimbra status:"
echo ""
su - zimbra -c /opt/zimbra/bin/zmcontrol status


echo ""
out "Creating archive"
echo ""

# Create archive of backed-up directory for transfer
tar -zcf $ARCHIVE -C $DEST .


echo ""
out "Copying to NFS server"
echo ""

mkdir $NFSMOUNT
mount -t nfs $NFSHOST:/$NFSPATH $NFSMOUNT
cp -fv $ARCHIVE $NFSMOUNT/
ls -lah $NFSMOUNT
umount $NFSMOUNT
rm -rfv $NFSMOUNT


echo ""
out "Removing $DEST and $ARCHIVE from local disk"
echo ""

# Remove temp backup destination, samba and archive
rm -rf $DEST
rm -fv $ARCHIVE

echo ""
out "Backup complete!"
echo ""
echo "----------"


cat $LOG | mail -c '' -s "[Zimbra backup] `hostname --fqdn` $TIME" $EMAIL

Posted (edited)

For background, the first line of the script (#!/bin/bash) tells you which interpreter should be used to run it.

 

It's in that weird format so that the shell can also tell, so if you just call ./zbackup.sh instead of bash zbackup.sh, things work as expected. Being able to do this is more convenient, since you don't have to dig into the script to find out what interpreter should be using it.

 

By convention, if you have that first line in a script (it's called a 'shebang' or 'hashbang') and the file has the execute bit on, you usually don't put a suffix on the script name. So yours would just be called zbackup. This makes it obvious that the file has a shebang and can be executed directly.

Edited by powdarrmonkey
  • Thanks 1
Posted

with either sh or bash I still get the error I mentioned before?

 

On a separate note thanks to everyone who has helped me with this so far. I would be nowhere without your help.

I'm enjoying this long and winding road which is linux. :D

Posted
Have you tried changing to the directory the script is in and running it using the ./ method? As long as it has the +x attribute it should work as powdarrmonkey said.
Posted

Yep in in the directory that the zbackup.sh is in. If I type ./zbackup.sh I get a different error!

 

-bash: ./zbackup.sh: /bin/bash^M: bad interpreter: No such file or directory

 

 

Does that mean anything?

Posted

The file is in DOS CR/LF (the ^M is the CR character) rather than UNIX LF style. Whatever you used to transfer the file from your windows PC mangled it. I use WinSCP here and it will detect and do the right thing™.

 

Use 'dos2unix ' to fix it.

 

You might want to try editing/creating scripts directly on your Linux machine in future. I do it via a SSH session using 'nano'. If you use Putty as your SSH client you can right click to mass paste text into the editor.

  • Thanks 2
Posted

You might want to try editing/creating scripts directly on your Linux machine in future. I do it via a SSH session using 'nano'. If you use Putty as your SSH client you can right click to mass paste text into the editor.

 

I am using Putty and I pasted directly from a notepad file!

 

I am learning quickly here.

 

thanks

Posted

Sorry to but in but has anyone here tried importing all their users from a CC3 network (windows Server 2003) into zimbra?

 

I'm trying to use a command from the zimbra site called ldap2zm.pl but i cant for the life of me get it working!

 

Got almost everything else up and running.

 

Any ideas?

Posted

Use 'dos2unix ' to fix it.

.

 

dos2unix did the trick.

 

Thanks again for everybodies help. I believe I now have a fully functional and backed up zimbra. :D

Posted
Sorry to but in but has anyone here tried importing all their users from a CC3 network (windows Server 2003) into zimbra?

 

I'm trying to use a command from the zimbra site called ldap2zm.pl but i cant for the life of me get it working!

 

Got almost everything else up and running.

 

Any ideas?

 

Are you trying to import mail as well ?

 

We use zimbra auth'ed against the CC3 AD, I knocked up an quick php script to create a csv of all the users on CC3 AD then did a bulk create on zimbra.

 

This was a couple of years ago though.

 

Martin

Posted

@budgester

 

Do you have any more info about how you did this?

 

At the moment i am just exporting users from sims in a csv and then importing

Posted

Sorry, to get back on topic!

I can now run the script by typing ./zbackup.sh but It doesn't run in crontab.

 

I have ..

00 23 * * Mon-Fri /usr/local/bin/backupscripts/zbackup.sh

 

Do I need to change this somehow so it has the ./ in there?

 

Could I change to the correct directory with the crontab and then execute weith ./ in front?

 

Thanks (yet again) :o

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...