shell script to run a few commands help!

Hi friends this is first post i am very new to shell scripting so i require your expertise to do the following thank u

I need to write a shell script which will run the following commands

pg_dump bank > backup(Enter)
Wait for bash prompt to appear coz it indicates that the command is executed

dropdb bank(Enter)
Wait for bash prompt to appear coz it indicates that the command is executed

createdb bank(Enter)
Wait for bash prompt to appear coz it indicates that the command is executed

psql bank < backup(Enter)
Wait for bash prompt to appear coz it indicates that the command is executed

I need to run it as a cron demon plz also let me know how to put this as a cron so that it executes every time system starts Thanks in advance

U can Put all those in a file and give it Execute Permission

"Wait for bash prompt to appear coz it indicates that the command is executed" Only when a commond get executed it goes to the next command

And of Corntab

u can add an entry this way

@reboot run_script

where run_script is ur script...

First of all thanks

How do i do it

let me ask one question

what does ur "pg_dump bank > backup" do??

is pg_dump is a shell script with "bank" as command line arg... ??

"pg_dump bank > backup" takes the backup of the database which may take some time only after the process is complete can i run the run the next command that is "dropdb bank" which deletes the database.

u can run all those on promt right??

u can do this

File run_script----

#! /bin/ksh
pg_dump bank > /input_path/backup
dropdb bank
createdb bank
psql bank < /input_path/backup

$chmod u+x run_script

$crontab -e
It Opens a file in that at the end give the following entry

@reboot /full_path_where_ur run_script_is_saved/run_script

"input_path" means where u want to take the backup right ?

Aneways thanx a bunch budy:) i'll try it out and let u know. 1 more thing i wanted to know about cron do i have to put my file in the cron.hourly or cron.daily folder or just crontab -e and do what u said and how do send myself an email from cron - the tasks the script has performed.

yes ofcourse...

1 more thing i wanted to know about cron do i have to put my file in the cron.hourly or cron.daily folder or just crontab -e and do what u said and how do send myself an email from cron - the tasks the script has performed.

sorry once again

before i execute "pg_dump bank > backup" i want to check if the backup file already exists if it does delete it else continue as u normally would.

pg_dump bank > backup { This will overwrite the file if exists and creats if not exits }

u need not put any specfic check to delete if exists

and for mailing after finishing

just put this line at the end of ur run_script

echo "BackUp Done"| sendmail ur_id

thanx is it possible for u 2 tell me how to check wether the file exists i do not want to overwrite it every time i would delete it every time if only u could tell me how

if [ -e file_name ]; then

echo "what ever u want to do"

fi

This is the script i wrote and saved it as "u.sh"
--------------------------------------------------
#!/bin/bash
PGUSER=postgres
PGPASSWORD=""
export PGUSER PGPASSWORD

pg_dump bank > backup
echo "BackUp Done"| sendmail parikshit@belgaum.tbdnet
--------------------------------------------------------------

This the output when i executed it

------------------------------------------------------------------

-bash-2.05b$ bash u.sh
': not a valid identifierPASSWORD
:command not found
pg_dump: [archchiver <db>] connection to database "bank" failed: FATAL: user "pos
" does not exist
u.sh: line 7: sendmail: command not found
: command not found
: command not found
-bash-2.05b$
----------------------------------------------------------------------

I don't know what's wrong but i can tell u when i run the following script it works fine

-------------------------------------------------------------------------------------------
#!/bin/bash
PGUSER=postgres
PGPASSWORD=""
export PGUSER PGPASSWORD
psql bank <<EOFMYSQL
vacuum analyze;
EOFMYSQL
-------------------------------------------------------------------------------------------

u have to connect to ur plsql to do th pg_dump

thats y i asked u what does pg_dump do???

and i said "u can run all those on promt right??"

u have not replied for those...

do u run pg_dump after logging on to plsql????

no i'll tell u normally this is how i do it i iniate a telnet session using windows run command

it asks me for a login : postgres

after which it does not ask for password as no password is required and it shows the bash prompt where i execute the following command pg_dump

Now i dont think psql comes into the picture anywhere

then y these in picture..

PGUSER=postgres
PGPASSWORD=""
export PGUSER PGPASSWORD

and instead of sendmail u can use mail in linux
echo "BackUp Done"| mail parikshit@belgaum.tbdnet (For this to run ur SMTP should be configured as ur sending to external system)

This is an example script which works perfectly and executes the command "pg_dump" even though the mentioned password is wrong

----------------------------------------------------------------------------
#!/bin/sh
##############################
#Script by Anas V
##############################

#Substitute your postgresql root username in the below given line
PGUSER=postgres
#Substitute your postgresql root password in the below given line
#PGPASSWORD=mypostgre
PGPASSWORD=pearls
export PGUSER PGPASSWORD

tdate=`date +%d-%b-%Y`

if [ $# -lt 1 ]

# Check if there is atleast one argument [i.e the database whose dump is to be taken]
#First argument is mandatory - Databse name
#Second argument is optional - Destination path to save dump
then

    echo "Bad Arguments"
    echo "-----------------------------------"
    echo "USAGE : pg_dmp.sh &lt;databasename&gt; [outputfile]"
    echo "-----------------------------------"
    exit 1

else

#if one or more arguments were provided
if [ $# -ge 2 ]
#if arguments provided is greater than or equal to 2
then
#Comment out the below given file exist check and it's associated messages
# if you want to run the pg_dmpsh script to run silently. i.e from a cron or at job
# without any user interaction. Then the output dump file will be rewritten if a file
# already exists.
if [ -f $2 ]
# if destination file ie argument 2 is already existing
then
#Show confirmation message to confirm whether replace file with new one or exit
dialog --title "Confirm File Replace" --backtitle "pg_dmp.sh"\
--yesno "\nFile already exist, Do you want to replace it with '$2' file" 7 90
sel=$?
case $sel in
#if Yes then take dump and replace the existing file with new dump
0) pg_dump $1 -f $2 -i -x -O -R;;
#if No then exit
1) exit 1 ;;
#if escape then exit
255) exit 1;;
esac
else
#if destination file does not exist then create and save the dump in destination path
pg_dump $1 -f $2 -i -x -O -R
fi
else
if [ $# -eq 1 ]
#if arguments provided is equal to 1
then
if [ -d $HOME/pg_backup_$1 ]
#if folder name pg_backup_'databsename' exist in the current users home directory
then
if [ -f $HOME/pg_backup_$1/$1_$tdate ]
#if destination file name exist in pg_backup_'databasename' folder in current users home dierectory
then
#Show confirmation message for replacing the file with new dump
dialog --title "Confirm File Replace" --backtitle "pg_dump.sh"\
--yesno "\nFile already exist, Do you want to replace it with '$HOME/pg_backup_$1/$1_$tdate' file" 7 90
sel=$?
case $sel in
#if Yes then replace the file with new dump file
0) pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R;;
#if No then exit
1) exit 1 ;;
#if escape thenexit
255) exit 1;;
esac
else
#if destination file does not exist then create and save the dump
pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R
fi
else
#if folder pg_backup_'databsename' does not exist in the current users home dierectory then
#Create a new folder
mkdir $HOME/pg_backup_$1
#then create dump and save it
pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R
#if databse to take does not exist then Delete the folder created
if [ $? -ne 0 ]
then
rmdir $HOME/pg_backup_$1
fi
fi
fi
fi
if [ $# -gt 2 ]
#if arguments passed where greater than 2 then show message
then
echo "Extra Arguments ignored"
fi
fi

#reset PGUSER and PGPASSWORD
PGUSER=""
PGPASSWORD=""
export PGUSER PGPASSWORD
#End

I dont know i'm getting an error in my script which is almost the same! As u can see in the above example they are entering the username and password that i think that is necessary.

ok lets try this..

just put

pg_dump bank backup

in u.sh and run
tell me what happens..