Website file Uploads With lftp

Keeping your website updated is tedious job,especially when you have lot of files,which you edit quite often.And having to do the such work over ftp makes it worse.

In this article we’ll use a command line based file transfer program lftp(http://lftp.yar.ru/) written by Alexander .V. Lukyanov to do website file uploads.Though there are many other ftp clients,lftp is simpler to use,feature rich,can upload several files in parallel and it comes bundled up with most Gnu/Linux distributions. lftp is a powerful tool, its built-in mirror facility is suitable for keeping your website updated and secure by replicating permissions on the uploaded files.It checks for modified files by checking the file sizes and file modification times.

Using lftp on Command Line

Now you have a local directory on your computer where you keep all your website files(/home/joe/site_files).Let upload these files to your remote webserver directory using lftp on command line.First lets connect to your hosting service providers webserver with username and password.

$ lftp -u username ftp.hostingcompany.net
Password:

The lftp program will prompt for the password,type your secret password and you will be logged in.

Now issue the ‘lcd’ command to change directory to local directory where your all website files reside.

lftp username@ftp.hostingcompany.net:~> lcd /home/joe/site_files
lcd ok, local cwd=/home/joe/site_files

Now use the lftp mirror built-in’s reverse option (-R) of lftp to
upload all files local files into remote directory on the webserver.

lftp username@ftp.hostingcompany.net:~> mirror -R
Total: 12 directories, 36 files, 0 symlinks
Modified: 13 files, 0 symlinks

You can use “ mirror -R —only-newer “ command if you want only new files to uploaded.Once the upload is finished lftp will print out a report.Now you can end the connection by typing quit.

lftp username@ftp.hostingcompany.net:~> quit

Running lftp in a Script

Now that we have seen lftp in action.Let run all the commands from script instead.Luke Vanderfluit provides this nice little script.

lftp -u username,password ftp.hostingcompany.net <<EOF
cd / # remote directory on webserver
lcd /home/joe/site_files # local directory on your computer
mirror -R # Upload the files to remote directory
quit 0 # Drop the connection
EOF

Save all the commands in a file ‘upload_mysite’ and make it executable with ‘chmod +x upload_mysite’.Run the script by typing ‘./upload_mysite’ on the command line.As your username and password are stored it file,its a better not let anyone else to view it.Changing file permissions with ‘chmod go-rwx upload_mysite’.

Maintaining Multiple Websites

Running lftp from script is not a good idea if you have to maintain multiple sites.Instead you can use lftp’s ‘-f’ option.You can store settings of different websites into separate files.Suppose you have two websites site1 and site2.

The settings of the both the sites are stored in files ‘mysite1.lftp’ and ‘mysite2.lftp.

#####mysite1.lftp#######################
open ftp.myisp.net
user username password
lcd /usr/joe/site1_files
mirror -R
exit #########################################

#####mysite2.lftp#######################
open ftp.hostingcompany.com
user username password
lcd /usr/local_site_location
mirror -R
exit #########################################

To update site1, we will run lftp with ‘-f’ option.

$ lftp -f mysite1.lftp

And to update the next website.

$ lftp -f mysite2.lftp

Some other usefull features provided by ‘mirror’ built-in are the —parallel = N where in it will use N simultaneous connections to upload the files,this speeds up the upload considerably.If you want to exclude some files from being uploaded use (- x , – X) . And —delete to remove those files which are not present in local directory.

lftp can also be run as a cron job . Jani Reinikainen shows an interesting use of lftp to periodically mirroring a directory to a remote computer.

References

lftp manual page [http://lftp.yar.ru/lftp-man.html ]
lftp FAQ [http://lftp.yar.ru/FAQ ]
lftp Download page [http://lftp.yar.ru/get.html ]

3 comments:

  1. thanks a lot! will it go recursively to upload the sub-folders as well??

    and how to exclude multiple files?

    regards,

    ReplyDelete
  2. Ben, you can exclude folders and files using the lftp -X option. Please do refer to lftp manual for more detailed explanation.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

You can leave a comment here using your Google account, OpenID or as an anonymous user.