Results 1 to 10 of 10

Thread: Copying a file to a remote ftp server with bash

  1. #1
    Join Date
    Apr 2008
    Beans
    968

    Copying a file to a remote ftp server with bash

    I'm hoping to set up a cron job that takes a file and copies it to a remote password protected FTP server. I've got a command that formats the file with the correct name and I've put it in the anacron file in /etc/cron.d (which I think is right, haven't tested it yet).

    I'm not sure how to copy the file to a remote server though. I do actually have the ftp server bookmarked in my places menu. So is there a simple way of suppling a file path that will put it straight into that folder? The only problem I can see with this is that the connection won't be open continuously, so would need to be re-opened when needed (I could presumably save the password in the keyring so that I don't need to be there to type it in).

    Or maybe set up a cron job that connects to and mounts the ftp server a minute before it has to copy the file over?

  2. #2
    Join Date
    Jul 2006
    Location
    London, UK
    Beans
    818
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Copying a file to a remote ftp server with bash

    My suggestion:

    Setup ssh for public / private key based logins (no password needed, more secure than a password). A HOWTO is here

    Then just use scp to copy the file
    /usr/bin/scp sourcefile user@server:/path/to/destination/

  3. #3
    Join Date
    Apr 2008
    Beans
    968

    Re: Copying a file to a remote ftp server with bash

    That sounds brilliant, but I don't have ssh access to this server, unfortunately FTP only

  4. #4
    Join Date
    Jul 2006
    Location
    London, UK
    Beans
    818
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Copying a file to a remote ftp server with bash

    No ssh ?
    For a server ???
    How can you securely manage a server without ssh ?
    Please don't tell be you are using telnet !
    Can't you install ssh or ask for it to be installed ?

  5. #5
    Join Date
    Apr 2008
    Beans
    968

    Re: Copying a file to a remote ftp server with bash

    I'll clarify my post a little!

    I'm not actually managing a server myself. I have just bought some space on a shared server from web hosting company. If I want SSH access then I'd have to pay a lot more and get a VPS or dedicated.

    Most of the management is done through cpanel and FTP software. I've created an extra folder on my space and associated an FTP account with it. I'm just hoping that there is a way of automating the copying of a file to that FTP account so I don't have to do it manually each day.

  6. #6
    Join Date
    Dec 2008
    Location
    London, UK
    Beans
    53
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Copying a file to a remote ftp server with bash

    Save the script below and change to your needs. Make it executable and run as a cron job.

    Code:
    #!/bin/sh
    USERNAME="username"
    PASSWORD="password"
    SERVER="hostname or ip here"
    # Directory where file is located
    DIR="/some/directory"
    #  Filename of backup file to be transfered
    FILE="filename"
    # login to ftp server and transfer file
    cd $DIR
    ftp -n -i $SERVER <<EOF
    user $USERNAME $PASSWORD
    binary
    mput $FILE
    quit
    EOF
    # End of script
    What's the point of a window if it can't be opened

  7. #7
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    Re: Copying a file to a remote ftp server with bash

    If you have curl installed, it also uploads by FTP.
    I use it to upload regularly to my rented host.
    (I really like mickwombat's script, though)
    If you need something that fits onto one line (for example, for use as a .bashrc alias or a cron job), this can be a convenient way to do it.

    Code:
    # The next 9 lines are lifted from mickwombat's script 
    #!/bin/sh
    USERNAME="username"
    PASSWORD="password"
    SERVER="hostname or ip here"
    # Directory where file is located
    DIR="/some/directory"
    #  Filename of backup file to be transfered
    FILE="filename"
    # login to ftp server and transfer file
    
    curl -T $FILE -u $USERNAME:$PASSWORD $SERVER/$DIR
    -T is the Upload flag.
    -u is the user authentication flag.

  8. #8
    Join Date
    Apr 2008
    Beans
    968

    Re: Copying a file to a remote ftp server with bash

    Thanks for your efforts both. That curl line looks perfect, but every time I run it it tells me I don't have permission to upload to the server and gives me a 403 forbidden message.

    Now I would've thought I must be putting something wrong in to get this kind of error, but I've tried connecting using a few different accounts and I've been double checking all of my details and tried every combination possible with no luck.

    Is there any other reason why this would happen?

  9. #9
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,382

    Re: Copying a file to a remote ftp server with bash

    Howdy,

    Here is another guide that explains how to script an FTP session:
    http://www.aeronetworks.ca/howtos/windows-backup.html

  10. #10
    Join Date
    Apr 2008
    Beans
    968

    Re: Copying a file to a remote ftp server with bash

    Thanks again all. After a bit more fiddling with scripts I have managed to put something together that so far is working nicely.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •