Newbie Linux shell script (using for cron)

Newbie Linux shell script (using for cron)

Post by Gok » Thu, 14 Feb 2002 18:20:59



Been trying to figure this out for an hour now. Here's the thing: I'm
trying to setup a cron job so that it automatically backsup my sql db
everyday of the week sun-sat. the problem is that i cant seem to get
the var passed in the path. below is a little bit of the code (my goal
here is to get the $day var passed into the path)

#!/bin/bash
#
sun='1. Sun'
mon='2. Mon'
tues='3. Tues'
wed='4. Wed'
thurs='5. Thurs'
fri='6. Fri'
sat='7. Sat'

mysqldump -u USERNAME -pPASS --opt MYSQLdB >
/home/me/backups/$day/SQLdB.sql

<rest of code here to change the $day=$mon etc>

whenever this much is executed it gives me
backups.cron: /home/me/backups/$day/SQLdb.sql: ambiguous redirect
even if i initialize $day as $sun ($day = $sun) or use $sun in the
code
(mysqldump -u USERNAME -pPASS --opt MYSQLDB >
/home/me/backups/$sun/SQLdb.sql)

hmm, just realized something. how would i even create a script
(ignoring the idea if i hardcoded $sun to the backup path) that can be
used to rotate days if it executes, dies, then rerun the next day; it
cant change its own code and doesnt say in memory forever.

 
 
 

Newbie Linux shell script (using for cron)

Post by Gareth Bezet » Fri, 15 Feb 2002 07:28:49


Would (as a newbie) is not be simpler to setup seven scripts and execute
them from the crontab file?

I may be strange, but I like to get a simple solution working first, then
make it 'better'

0 4 * * sun <script for Sunday>
0 4 * * mon <script for Monday>
etc

I'm assuming you want to only keep the last seven days' backups.  Otherwise
you could take to output file and give it a name based on the date.  I'm not
sure of a simple way to do it, but you could tarball it and give the file a
name from the date.

Gareth


Quote:> Been trying to figure this out for an hour now. Here's the thing: I'm
> trying to setup a cron job so that it automatically backsup my sql db
> everyday of the week sun-sat. the problem is that i cant seem to get
> the var passed in the path. below is a little bit of the code (my goal
> here is to get the $day var passed into the path)

> #!/bin/bash
> #
> sun='1. Sun'
> mon='2. Mon'
> tues='3. Tues'
> wed='4. Wed'
> thurs='5. Thurs'
> fri='6. Fri'
> sat='7. Sat'

> mysqldump -u USERNAME -pPASS --opt MYSQLdB >
> /home/me/backups/$day/SQLdB.sql

> <rest of code here to change the $day=$mon etc>

> whenever this much is executed it gives me
> backups.cron: /home/me/backups/$day/SQLdb.sql: ambiguous redirect
> even if i initialize $day as $sun ($day = $sun) or use $sun in the
> code
> (mysqldump -u USERNAME -pPASS --opt MYSQLDB >
> /home/me/backups/$sun/SQLdb.sql)

> hmm, just realized something. how would i even create a script
> (ignoring the idea if i hardcoded $sun to the backup path) that can be
> used to rotate days if it executes, dies, then rerun the next day; it
> cant change its own code and doesnt say in memory forever.


 
 
 

Newbie Linux shell script (using for cron)

Post by Matthew Clin » Fri, 15 Feb 2002 13:49:37


Goku was touched by the minds of the terrible Old Ones, and imparted unto
us these blasphemous ravings:

Quote:> Been trying to figure this out for an hour now. Here's the thing: I'm
> trying to setup a cron job so that it automatically backsup my sql db
> everyday of the week sun-sat. the problem is that i cant seem to get
> the var passed in the path. below is a little bit of the code (my goal
> here is to get the $day var passed into the path)

> #!/bin/bash
> #
> sun='1. Sun'
> mon='2. Mon'
> tues='3. Tues'
> wed='4. Wed'
> thurs='5. Thurs'
> fri='6. Fri'
> sat='7. Sat'

> mysqldump -u USERNAME -pPASS --opt MYSQLdB >
> /home/me/backups/$day/SQLdB.sql

It might be because there's a space in the destination file; try putting
double quotes around it.

Also, it might be better to do:

  day=`date +%w-%a`

Which will gived you "0-Sun", "1-Mon" and so on; this way you won't have to
figure it out yourself.

Quote:> whenever this much is executed it gives me
> backups.cron: /home/me/backups/$day/SQLdb.sql: ambiguous redirect
> even if i initialize $day as $sun ($day = $sun) or use $sun in the
> code

You shouldn't do anything like

   $day = $sun

but

   day=$sun

 
 
 

Newbie Linux shell script (using for cron)

Post by Gok » Fri, 15 Feb 2002 14:29:39


Actually, that's how i have it setup now--7 different cron jobs for
each day of the week. the only difference between all 7 diff scripts
is the day directory such as "1. Sun" or "2. Mon" etc. I'd like to
make it a little more professional and a lot easier should i want to
backup more stuff (which i do) such as my sites and other parts in my
webhost (my webhost supports multiple websites in one account. ex:
/home/me/domain1.com, /home/me/domain2.com, /home/me/domain3.com,
etc).

this way i only need to type in the shell command once, in one file,
and only have to stick a variable which changes the day for me. doing
it 7 different times is too much work when i know there's a way to do
it once.


> Would (as a newbie) is not be simpler to setup seven scripts and execute
> them from the crontab file?

> I may be strange, but I like to get a simple solution working first, then
> make it 'better'

> 0 4 * * sun <script for Sunday>
> 0 4 * * mon <script for Monday>
> etc

> I'm assuming you want to only keep the last seven days' backups.  Otherwise
> you could take to output file and give it a name based on the date.  I'm not
> sure of a simple way to do it, but you could tarball it and give the file a
> name from the date.

> Gareth



> > Been trying to figure this out for an hour now. Here's the thing: I'm
> > trying to setup a cron job so that it automatically backsup my sql db
> > everyday of the week sun-sat. the problem is that i cant seem to get
> > the var passed in the path. below is a little bit of the code (my goal
> > here is to get the $day var passed into the path)

> > #!/bin/bash
> > #
> > sun='1. Sun'
> > mon='2. Mon'
> > tues='3. Tues'
> > wed='4. Wed'
> > thurs='5. Thurs'
> > fri='6. Fri'
> > sat='7. Sat'

> > mysqldump -u USERNAME -pPASS --opt MYSQLdB >
> > /home/me/backups/$day/SQLdB.sql

> > <rest of code here to change the $day=$mon etc>

> > whenever this much is executed it gives me
> > backups.cron: /home/me/backups/$day/SQLdb.sql: ambiguous redirect
> > even if i initialize $day as $sun ($day = $sun) or use $sun in the
> > code
> > (mysqldump -u USERNAME -pPASS --opt MYSQLDB >
> > /home/me/backups/$sun/SQLdb.sql)

> > hmm, just realized something. how would i even create a script
> > (ignoring the idea if i hardcoded $sun to the backup path) that can be
> > used to rotate days if it executes, dies, then rerun the next day; it
> > cant change its own code and doesnt say in memory forever.

 
 
 

Newbie Linux shell script (using for cron)

Post by Gok » Thu, 21 Feb 2002 10:16:20


Thanks for the info. i'll try that out. one question, are %w and %a
auto system variables? because i only want to save one  or two week's
worth for backup.

ex:

0-sun
1-mon
2-tues
3-wed
4-thurs
5-fri
6-sat

then when next week rolls along it starts all over which overwrites
the previous week

0-sun
1-mon
2-tues
3-wed
4-thurs
5-fri
6-sat

thanks for all the help

goku


> Goku was touched by the minds of the terrible Old Ones, and imparted unto
> us these blasphemous ravings:

> > Been trying to figure this out for an hour now. Here's the thing: I'm
> > trying to setup a cron job so that it automatically backsup my sql db
> > everyday of the week sun-sat. the problem is that i cant seem to get
> > the var passed in the path. below is a little bit of the code (my goal
> > here is to get the $day var passed into the path)

> > #!/bin/bash
> > #
> > sun='1. Sun'
> > mon='2. Mon'
> > tues='3. Tues'
> > wed='4. Wed'
> > thurs='5. Thurs'
> > fri='6. Fri'
> > sat='7. Sat'

> > mysqldump -u USERNAME -pPASS --opt MYSQLdB >
> > /home/me/backups/$day/SQLdB.sql

> It might be because there's a space in the destination file; try putting
> double quotes around it.

> Also, it might be better to do:

>   day=`date +%w-%a`

> Which will gived you "0-Sun", "1-Mon" and so on; this way you won't have to
> figure it out yourself.

> > whenever this much is executed it gives me
> > backups.cron: /home/me/backups/$day/SQLdb.sql: ambiguous redirect
> > even if i initialize $day as $sun ($day = $sun) or use $sun in the
> > code

> You shouldn't do anything like

>    $day = $sun

> but

>    day=$sun

 
 
 

1. Newbie help with Shell scripts (using Korn shell)

Hi,

I am fairly new to UNIX, but keen to learn. I have a few scripts I am trying
to write and was wondering if anyone could be of some assistance?

Script 1.
I am trying to display/list the top 15 users of HDD space, and if they
exceed the quota (5000KB) then display "Yes" next to their name, and "NO" if
they haven't.

Script 2.
List any inactive users or users that haven't logged on for 3 months.

Script 3.
Make a script called "IP". Then if I run IP device_name  (where device_name
is a device on the network) it returns the IP address of the device.

I have had a good go at the 3 scripts, but not having too much luck. Any
help would be greatly appreciated.

Milan.

2. Backout patch on Solaris 2.4

3. cron - shell script would not generate output in cron

4. ^Z in login script

5. Cron Problems - Script runs from shell but not cron

6. logout/exit/ctrlD

7. crontab, shell script, Linux; weird cron failure.

8. processes using fork() vs threads in other languages

9. Pointers/Help with a basic shell script (Shell Newbie alert)

10. NEWBIE-Shell scripting - When to use script variable vs. create tmp file???

11. Copy files using filenames from text files with shell script or bash script

12. using born shell script as login shell?