Send a file to an FTP with a single command

We have already seen how to connect to an FTP server and work with it (or its content) through the terminal, that is, without using graphic applications.

This time I bring you a plus or an extra ... let me explain.

Some years ago I left them a bash script used to make backups (saves) of data from a server. The script copied a series of folders (like / etc /), exported databases, etc ... and compressed it with a password into a .RAR or .7z file (I currently use 7z), the only thing that the script lacked was to be able to upload then that compressed file to some FTP server, in this way the save from the server would be copied to another location.

These days I took up the script to optimize it a bit, improve it and obviously the need for the latter that I mentioned to you came to light, uploading the compressed archive to an external FTP.

How to upload to an FTP with a single command?

What I needed was through a single command to connect to an FTP with username and password, and well; upload the file to a specific folder.

Terminal applications that allow me to connect to an FTP, put user & password and upload files there are lots of them, but… that allow me to do this all in one line, with all the parameters already specified…. hey there the question.

After reviewing 4 or 5… I thought, huh !! ... but it exists curl

Upload to an FTP with curl

With curl I can do an infinite number of things, maybe I can do what I want… and that's it!

With the -u parameter I can specify the username and password, also with the -T parameter I can tell it to upload a file, and finally to tell it to which FTP and which folder I want to upload it, at the end I just put the full path, more or less like this :

curl -u usuario:password -T archivo-backup.7z ftp://192.168.128.2/SERVER_BACKUPS/

What this does is connect to the FTP 192.168.128.2, with the user user and password Password and upload to the folder SERVER_BACKUPS the file called file-backup.7z

And ready!

Simple right? ...

Of course, this can be useful to us as well the command alone, however, the best way to use it is with a script such as ... the one I mentioned before

And what about that script that is mentioned?

I am making improvements to the script, especially incorporating some requests or suggestions from users.

  • The first thing I wanted to do was precisely this that I just explained to you, with a single command to upload the save file to an FTP.
  • The other thing that a user recommended me was to send an email when the backup was ready, for that I can use sendmail or external script, I will preferably use sendmail. The detail of using sendmail is that you can use your GMail account (or any other) to send the email, with encryption ... SSL and whatever.
  • Also, a user recommended that also, as a more dynamic form of notification, a message be sent by IM using GTalk's XMPP or Hotmail's (Live or something like that, I don't even know what it's called). I will try to do it with GTalk first, because for Hotmail I would have to remember or support myself somewhere to create a Hotmail account, because with so much change-change that Microsoft has, no idea what it is like.
  • Another variant of the latter would be to use notifications or messages that are sent by Facebook or Twitter. For Twitter you could use Twidge while for Facebook you could use fbcmd. Both applications allow me to interact with these social networks from the terminal.
  • I'm also thinking of checking the integrity of the sql that I export, but this already requires a little more time :)

ftp server

The end!

Well, nothing more to add ... for the moment, I am taking up several of my scripts made in Bash to optimize and improve them, I hope it won't take me too long to bring news 😀

regards


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   barnarasta said

    Great project,
    I will follow it with great interest.
    - Any notice @ if the server is down or the delivery could not be made?

    Tremendously enjoyable to read articles from terminal / console lovers.

    1hello2

    1.    KZKG ^ Gaara said

      The pleasure is mine 🙂

      Good idea, to verify if the FTP server is online and if it is not, to send an email ... I will take it into account ^ _ ^

  2.   Moses Serrano said

    I had adapted your backup script and adapted a project that allows you to upload the final file to Dropbox (https://github.com/andreafabrizi/Dropbox-Uploader) and send an email at the end by sendmail.

  3.   dhunter said

    Gaara you have to try the right tool for this: lftp

    It even supports mirroring, to sync repos from ftp is priceless.

    http://www.cyberciti.biz/faq/lftp-mirror-example/

  4.   George said

    Very interesting, it is the beauty of this system, you can obtain the same result in multiple ways; I show you the method in which I was managing to upload files to an ftp server, it is a bit rustic but it still works:

    {
    echo user user password
    echo bin
    echo prompt
    echo cd / directory / from / server / ftp
    echo put file
    echo close
    echo bye
    } | ftp -n server.ftp

    1.    KZKG ^ Gaara said

      OOOHHH interesting, I didn't know what could be done like this 😀
      Thank you!!

  5.   Sephiroth said

    Other ways, for example with wput:

    wput file_to_upload ftp: // USER: PASS@123.123.123.123: 21

    or for those who prefer simplicity in a script using the old telnet:

    ftp -n server_ip << EOF
    user anonymous test@test.cu
    send FILE.txt
    exit
    EOF

  6.   Javier said

    Hello, I am a beginner in Linux and I do not know computing - only at the user level - or programming, or anything like that, I am practically ignorant about this. I was reading this article and I read at the end of the second paragraph the word "location"; that word is misused, you have meant: location, location, location, place. The word location means something else as the RAE says "http://dle.rae.es/?id=NXeOXqS".