making shell script

Hi ,
I am new to shell scripting I want to make script as to execute followng command

mysqldump -u (user name) -p(password) database name>filename.sql

this file saves with current date and time

and execute automatically at particular time which I give

Will it execute the same time every day? Then you should add it to cron. (You'll need to search the forums or FAQ for how to do that.)

To make the script, just edit a file and name it "backupmysql.sh" or something. Then cut-and-paste and edit the fields to suit your needs:

#!/bin/sh
USER="www"
PASS="xxxx"
DB="zzzz"
DATE=`date +%y-%j`
mysqldump -u $USER -p$PASS $DB >backup-$DATE.sql

I made shell script but when I execute through shell command it execute successfully but when when i use crontab it execute the backup but shows 0 kb I did following step

step 1 : my shell script is

#! /bin/sh
USER="root"
PASS="nxsmasc"
DB="sme"
DATE=`date +%y-%d-%m`
#date=`date +%F%R` #space between date and +
#longdate=`date +%m%d%y%h%mm%ss`
mysqldump -u $USER -p$PASS $DB>/home/today_sme-$DATE.sql
echo "done"

I stored this file as /usr/bin/bck_sme.sh

step 2: when i execute comand sh bck_sme.sh it excutes successfully

step 3: now through crontab

typed command crontab -e

following entry
29 19 * 12 0-6 /usr/bin/bck_script.sh

but when it execute it shows 0 k file

Please tell where could be problem

check your mail box, read error message. maybe you will find a way

as i checked mail following found

Subject: Cron <root@ninex> /usr/bin/bck_script.sh
Date: Mon, 15 Dec 2008 20:01:01 0530 (IST)
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
/usr/bin/bck_script.sh: line 12: mysqldump: command not found
done

but this execute by command sh bck_script.sh
not through crontab

As always, read the FAQ first!

You just have to fix your PATH variable. When you are the command prompt, type:

echo $PATH
echo $LD_LIBRARY_PATH

You'll need the contents of these either inside your script or inside your crontab.

ALSO, you're outputting to a file in /home. That's normally INCORRECT. Your output file should be something like:

 ...  >$HOME/today_sme-$DATE.sql

Thanks to you all

I was missing export PATH=$PATH:/usr/bin/
in my shell script thus during contab it could not identify mysqldump command

I made the script

############################################################
#! /bin/sh
export PATH=$PATH:/opt/mysql/bin/
#echo $PATH
echo $LD_LIBRARY_PATH
USER="root"
PASS="qwa"
DB="shell"
DATE=$(date +%d%b%y_%k.%M.%S)
#DATE=`date +%y-%d-%m`
mysqldump -u $USER -p$PASS $DB | gzip -9>/home/backup/today_sme-$DATE.sql.gz
find /home/smebackup -mtime +60 -exec rm -f {} \;
# script to send simple email
# email subject
SUBJECT="live sme database backup"
# Email To ?
EMAIL="xyz@wqs.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "Backup of shell database is done successfully"> $EMAILMESSAGE
# echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

echo "done"
############################################################

It does
1=takes backup
2=delete file before 60 days
3=send a mail

But when I channged mysqldump-->sqldymp (just for test purpose) still it send mail "backup done successfully" while mysqldump command doesnot execute

I want If command execute successfully then only mail should be sent How can I do this

After the dump, test the exit status of mysqldump.

When cron runs any job, it does so in a minimal environment, but when you run a job manually, your whole environment is usually setup with .profile (Korn, Bourne and Bash, I believe) or .login/.cshrc (CSH).

In cron, you can add an entry such as...

00 12 * * * . /home/.profile; <command>

so that you get the whole normal profile available to the cronned command.

Jerry