views:

47

answers:

2

I have two shell scripts to write that will be executed nightly via cron schedule.

On my production server:

  1. mysqldump -ufoo -pbar --opt --routines db > ~/sqldump/{$todays_date}.sql
  2. ln -s ~/sqldump/{$todays_date}.sql latest.sql

On my development server:

  1. scp [email protected]:~/sqldump/latest.sql ~/sqldump
  2. mysql -ufoo -pbar db < latest.sql

I have two questions:

  1. In the production server job, how do i get $todays_date to fill in the date? e.g., "2010-07-21"
  2. How do I make step two wait for step 1 to finish in each job?
+3  A: 

To get today's date, use date +%F:

command > ~/sqldump/$(date +%F).sql

To read more about the various options you can use with date's + formatting, check out the date man page.

As for waiting, I assume you mean you don't want to run the command on your dev server until the commands on your prod server are done. The best idea would be to have the dev server start the command using ssh (i.e. ssh user@host "mysqldump ... > ... && ln -s ...).

If you don't want to do that, the best I can think of is to use a temp file so that the dump file creation is atomic:

mysqldump ... > ~/sqldump/tmp$$ && mv ~/sqldump/tmp$$ ~/sqldump/$(date +%F).sql

Then if you have your dev server request's the dump from today's date, then you can loop until the scp succeeds:

while ! scp [email protected]:~/sqldump/$(date +%F).sql ~/sqldump; do
    # file not present yet, sleep 1 minute and try again
    sleep 60
done
mysql -ufoo -pbar db < latest.sql
R Samuel Klatchko
+1, Thing to note, between commands instead.
Anders
Curiously, you've answered something useful but not exactly what the question states.
Darron
+1  A: 

Unless you use an "&" at the end of the command for step one, step two will automatically wait for it.

Darron
Anders
Darron