0

I have a script that is run by cron to create backup of a MySQL database and some files. After creating a tar ball and encrypting it with openSSH I have to put it on a remote ftp server. Following is the code for ftp part:

    HOST='abcd.dyndns.biz'
    USER='username'
    PASSWD='password'
    FILE='myBack-'${LOCAL_HOST}'-'${DATENAME}'.enc.tar.gz'
    DIRNAME='/usr/local/backups/'

    cd ${DIRNAME}

    ftp -n ${HOST} <<END_SCRIPT
    quote USER ${USER}
    quote PASS ${PASSWD}
    cd backup
    lcd ${DIRNAME}
    put ${FILE}
    quit

END_SCRIPT

If i directly run the script from command line > sudo ./mybackup.sh it runs smoothly and put the backup file on ftp, but when it is running from cron it has never put the file on ftp though other pre-ftp and post-ftp (like consolidating log file and emailing outcome) steps work fine. I am not able to get anything from any log files also or to pinpoint the cause.

NOTE:- Our dyndns.biz ip does not change as we have paid plan.

4
  • 1
    Don't cron mail you a report after task? You may find logs with the mail command. Did you checked that cron run with the right permissions?
    – M'vy
    Jun 16, 2011 at 8:15
  • @M'vy cron is running the job as root. Jun 16, 2011 at 9:02
  • What does the relevant section of crontab -l look like?
    – Kirk
    Jun 16, 2011 at 12:22
  • sudo crontab -l ` . # m h dom mon dow command 0 22 * * * /usr/local/backups/osback.sh` Jun 16, 2011 at 15:38

3 Answers 3

3

Use Curl's upload functionality instead:

curl --upload-file "$FILE" --user "$USER:$PASSWD" "ftp://$HOST/backup/"
3
0

Umm.. I see several points why it could fail.

  1. you don't know that ftp has opened a connection when you are sending input, but maybe it takes care of that for you (im not familiar with your ftp prog.. it could be anything afaik).
  2. you are not separating your ftp:// commands with semicolons or newlines - try adding \n or ;. again idk what ur app wants

  3. you are not checking that the file actually exists before trying to put it.

6
  • If it cannot take commands from batch, then how sudo ./mybackup.sh succeeds? The file does exists as it is still there after the script finishes. Jun 16, 2011 at 15:28
  • Just because your ftp command doesn't crash or return an error when you feed it a string of of text, doesn't mean it accepts the tokens as arguments or commands. I mean, if you have seen somewhere that it can be done like that with the ftp command you are using then go ahead, otherwise I'm telling you you can't assume it will work because text protocols like ftp often return "ok" after doing authenticate, cd, etc. And you have to deal with that - a simple redirect from stdin does not deal with it, expect does. Jun 17, 2011 at 2:46
  • Next - your variables are probably failing. Where are ${LOCAL_HOST} and ${DATENAME} defined? cron executes commands under a "naked" environment, definetly not the same as sudo. Whats wrong with the standard ${HOSTNAME}? Nevermind.. Just verify that ${FILE} is there and that its not something like `myBack--.enc.tar.gz' like I already mentioned. Jun 17, 2011 at 2:55
  • @Ярослав Рахматуллин I have not putup whole script the variables are there and all the files are created and there only this ftp thing fricks up the whole show Jun 17, 2011 at 12:19
  • so try running a different tool (another ftp) or run ftp with expect. Good luck. Jun 17, 2011 at 15:49
0

I had similar problem.The solution for me: to refer directly to the directories without variables like this:

lcd /mpla/mpla2/dir1
cd  /mpla3/mpla4/dir2

and it worked. Only this change. The quote USER $username and quote PASSWD $password were left as is but without the curly braces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .