3

Typically I upload updated files/scripts over Secure FTP to my server to replace existing copies of those files as I make changes. But this has the nasty behavior of writing directly to those files. This means that while the files are being uploaded, those files/scripts will be broken (due to being incomplete) for anyone who loads the web site during that time. How can I fix this?

I assumed the secure FTP/SSH daemon would be smart enough to keep in-progress downloads in a temporary location (like /tmp) until the transfer is complete, and then quickly copy the files to the intended location. Is there an easy way to make it do something like that?

Thanks!

DivideByHero
  • 371
  • 1
  • 3
  • 8

2 Answers2

4

On a per-file basis, rsync has this behaviour, so I'd recommend using that (for all sorts of other reasons, too). If you need to atomically swap out your entire site, then Josh's answer is the way to go.

womble
  • 96,255
  • 29
  • 175
  • 230
  • Funny how your answer was basically, "Josh's answer is correct", yet you get more upvotes ;-) -- I didn't know that rsync had the behavior you described. I know pure-ftpd does. Do you know about sftp? – Josh Nov 09 '09 at 17:48
  • @Josh: I don't see any mention of rsync in your answer. – womble Nov 09 '09 at 19:09
  • Ok. I misread your answer. I thought he was asking only about swapping entire directories, not individual files. Sorry! :-) – Josh Nov 10 '09 at 00:58
3

(S)FTP everything to a temporary directory. Upon completion, rename the destination directory and rename the temporary directory to take it's place.

For example, let's say you're uploading everything to public_html:

  1. Via SFTP make a new directory public_html_new at the same level as public_html

  2. Upload everything to public_html_new

  3. When the upoad finishes, rename public_html to public_html_old, and the rename public_html_new to public_html

Josh
  • 9,190
  • 28
  • 80
  • 128