0

--- file makebackup.sh

#!/bin/bash
mysqldump --all-databases | gzip -9 > /backup/temp_db.gz
tar -Pcf /backup/temp_ftp.tar /public_html/
tar -Pcf /backup/temp_backup.tar /home/temp_db.gz /backup/temp_ftp.tar
wait %%
/backup/upload.sh

--- file upload.sh

#!/usr/bin/expect -f

# connect via scp
spawn scp /backup/temp_backup.tar root@mybackup.com:/home/backup.tar
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "mypassword\r"
}
}
interact

When i look at /backup folder i see all files are there. In proper size When i look at destination site i see file that is 18 times less than what i have in source server in /backup folder.

When i just run it as ./makebackup.sh it works properly when i run trough cron it does not. Is there way to execute backup/upload.sh ONLY once prior commands are finished?

15 12 * * 1,3,5 /backup/makebackup.sh 2>&1 >> /var/log/cron_makebackup.log

Cron log does not say anything at else than when i run it manually.

JohnA
  • 1,875
  • 8
  • 28
  • 34

1 Answers1

1

I don't know what wait %% does, but this should help:

#!/bin/bash
mysqldump --all-databases | gzip -9 > /backup/temp_db.gz
tar -Pcf /backup/temp_ftp.tar /public_html/
tar -Pcf /backup/temp_backup.tar /home/temp_db.gz /backup/temp_ftp.tar && \  
/backup/upload.sh || \
/backup/upload.sh

NOTE: never forget you can set up scp (and ssh too) to work without passwords! I'd consider that instead of expect (which is a great tool).

This way upload.sh will be executed only after the last tar command finished either successful or with error exit code(s).

Some more info:

  • If you write test && command, the command will only be executed if the test succeeds.
  • If you write test || command, the command will only be executed if the test fails.

Run these tests:

$ true && echo "Yes."
Yes.
$ false || echo "Yes."
Yes.

Notice that the outcomes are entirely in keeping with one's intuition about such logical comparisons, and all is well as long as you don't think about the fact that true equals 0. :) Here's another scheme commonly seen in shell script programming and interactive sessions:

$ command1 && command2 && command3 && command4

This line of code will not run the next command in the sequence unless the prior command has returned "true", meaning no errors. It is a way to avoid running a command if a required prior outcome is not present.

Also see this article on && and || testing.

HTH

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • wait %% supposedly waits for last triggered PID to finish. Can you tell me how this && \, || \ technique called so i can find more info on it? – JohnA Dec 12 '11 at 20:50
  • Is there particular reason why you use symbol "\" e.g. "&& \ /backup/upload.sh" Zsolt? – JohnA Dec 15 '11 at 20:14
  • Is there particular reason why you use symbol "\" e.g. "&& \ /backup/upload.sh" Zsolt? i if i have 4 commands how can i nest em e.g. "(((command1 && command2) && command3) && command4" – JohnA Dec 15 '11 at 20:24
  • Okay it works with simple stuff like echo but it does NOT work for above scripts i used please verify yourself. – JohnA Dec 16 '11 at 01:14
  • Sorry, but what you mean by not works? That's not an error riport. By the way: it should (must) work: if testing exit codes of previous commands fails in a *nix shell, then we have a problem :-) ... And I had added some suggestion to the answer as well – Zsolt Botykai Dec 16 '11 at 14:17