Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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 '11 at 8:15
    
@M'vy cron is running the job as root. –  TheVillageIdiot Jun 16 '11 at 9:02
    
What does the relevant section of crontab -l look like? –  Kirk Jun 16 '11 at 12:22
    
sudo crontab -l ` . # m h dom mon dow command 0 22 * * * /usr/local/backups/osback.sh` –  TheVillageIdiot Jun 16 '11 at 15:38

2 Answers 2

up vote 3 down vote accepted

Use Curl's upload functionality instead:

curl --upload-file "$FILE" --user "$USER:$PASSWD" "ftp://$HOST/backup/"
share|improve this answer
    
again same problem works from command line but fails with cron :( –  TheVillageIdiot Jun 17 '11 at 12:19
    
working from script also but got problem with same script on another system please see this if you can get any clue what is happening: superuser.com/questions/299496/curl-not-uploading-file-to-ftp –  TheVillageIdiot Jun 20 '11 at 7:11
    
Try specifying path as cron uses a very restricted path by default: /usr/bin/curl ... –  LatinSuD Jun 6 at 12:48

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.

share|improve this answer
    
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. –  TheVillageIdiot Jun 16 '11 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 '11 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 '11 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 –  TheVillageIdiot Jun 17 '11 at 12:19
    
so try running a different tool (another ftp) or run ftp with expect. Good luck. –  Ярослав Рахматуллин Jun 17 '11 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.