How to upload multiple files to FTP from Linux server?

8

1

I have for example database backups on my Linux server, and I would like to write a script to upload it to remote FTP. I tried the put command, but it can only transfer one file at a time. Then I tried the mput command, but it just shows me question marks for files I want to transfer (maybe I'm using it wrongly?..)

I tried this:

#!/bin/bash
ftp -n <<EOF
open ftp.server.com
user name password
cd backup
mput /backup/*
EOF

But it won't work. It outputs names of files I want to transfer with question marks at the end of names. Am I missing something or maybe there is better simpler way?

Andrius

Posted 2013-10-11T07:04:08.643

Reputation: 477

Answers

9

Take a look at ncftp, it comes with an utility (ncftpput) that can be scripted for this purpose:

$ ncftpput -R ftp.server.com /remote/path /backup

The -R flag means recursive mode. You can have your authentication details in a separated file. See the manpage (ncftpput(1)) for details.

dawud

Posted 2013-10-11T07:04:08.643

Reputation: 1 305

I was about to suggest ncftp too. Use it all the time (and its bookmarks feature is great :)). – a CVn – 2013-10-11T07:31:14.283

For some reason it wronlgy installed or didn't install at all. It gives me error -bash: ncftp: command not found. In install log it said it succesffuly installed, but I also saw warnings like these -" insserv: warning: script 'NcFTPd' missing LSB tags and overrides insserv: There is a loop between service redmine and NcFTPd if stopped insserv: loop involving service NcFTPd at depth 2 insserv: loop involving service redmine at depth 1 insserv: Stopping NcFTPd depends on redmine and therefore on system facility `$all' which can not be true!" – Andrius – 2013-10-11T07:38:26.840

2@Andrius Are you sure that you installed the client and not the server? The error message definitely suggests you installed the server. – a CVn – 2013-10-11T07:42:44.503

Right.. How I didn't see that :) – Andrius – 2013-10-11T07:48:09.450

3

Another alternative might be to use CurlFtpFS, a FUSE file system driver that exposes a FTP host as a mounted directory (allowing you to access those files with any regular tools, including cp). I don't know how well it works, but from a quick googling around it looks very promising. CurlFtpFS goes back further than does FUSE actually, so should be stable.

To use it, install the appropriate package for your distribution (for example, Arch and Debian use the package name curlftpfs), load the fuse module, and sudo curlftpfs ftp.example.com /mnt/ftp/ -o user=username:password,allow_other, substituting appropriate values for server name, mount point, user name and password.

There are further details at the linked page but that should get you started.

a CVn

Posted 2013-10-11T07:04:08.643

Reputation: 26 553

3

You could use lftp

lftp ftp://user:password@domain.ltd -e "mirror -e -R /local/folder /remote/folder ; quit"

TheFoOL

Posted 2013-10-11T07:04:08.643

Reputation: 141