Auto upload a website via FTP

You know how boring can be site upload over and over again. Site is like a house pet, always needs some attention and after each change it should be uploaded to the Web vie FTP. Even doing this over and over again it is easy to make a mistake a miss one important file, so site can become malfunction. I use simple lftp command line tool to automate project deployment via FTP and eliminate any possible deployment errors. Lftp - easy upload and sync website via FTP lftp is a program that allows sophisticated ftp and http connections to other hosts. lftp can handle six file access methods - ftp, ftps, http, https, hftp.   Every operation in lftp is reliable, that is any not fatal error is ignored and the operation is repeated. So if downloading breaks, it will be restarted from the point automatically. lftp has shell-like command syntax allowing, it accepts on the input script like program. Each command tells lftp what to do. This gives perfect control of what is going to be uploaded or deleted. Installing under Ubuntu is pretty straightforward: apt-get install lftp. There is also windows version available.

Usage example

In this example, lftp script is used for uploading CakePHP project, which is not a simple upload task. In the project folder I created file sync.sh which handles all the dirty job, below is the content of the file. www is local folder. public_html is remote folder.
#!/bin/bash
lftp << EOF
set ftp:ssl-allow no
set ftp:passive-mode true
set ftp:list-options -a
open -u [USERNAME],[PASSWORD] ftp.host.com
# Upload site configuration
put -O public_html/dev/app/config/ www/app/config/bootstrap.php
put -O public_html/dev/app/config/ www/app/config/routes.php
# Mirror program code
mirror -R --delete --exclude config --exclude tmp --exclude webroot www/app public_html/dev/app
# Mirror webroot (not delete remote user files)
mirror -R --exclude user_data www/app/webroot public_html/dev/app/webroot
cd public_html/dev/app/tmp/cache/models
mrm *
cd public_html/dev/app/tmp/cache/persistent
mrm *
EOF
FTP username or password can be embedded into script itself, which is less secure. More secure way is to add additional lines of bash scripting to ask for password. put - uploads one sinlge file. First argument is REMOTE file name, second argument is LOCAL file name. mirror - synchronizes local and remote folder. Used swithes are:
  • -R - sync from local to remote (uploading, NOT downloading)
  • --delete - delete files on remote side which do not exist on local side
  • --exclude - exclude directory by name
  • cd - remote change directory command
  • mrm - remove multiple files

Debugging

When lftp is run with "-d" switch it shows all FTP commands, which is good for debugging. One more good switch of mirror command is "--dry-run", it just shows what is going to be uploaded without any actual action performed. Beware! Do not do any experiments on live project with uncommitted changes!!! You may loose your changes. Do a project copy or commit all changes before.

Conclusion

Now I am sure that after development round all changes will be uploaded to the server and nothing left behind. Upload script is easy customizable for future projects.

Resources

1 comment

  1. MauricioAugust 7, 2012 Got it because IPv6 Ready! CentOS Version 6.2 is the optinerag system of choice for those looking for a highly stable version of Linux that offers enterprise-level reliability.

Leave a Reply

Your email address will not be published.