Update FTP without the file being removed while overwriting

Asked

Viewed 107 times

2

The FTP Whenever you send a file it is unusable for a few milliseconds, until the entire file is in place. The problem of this is that the file for a few milliseconds does not exist or is corrupted, which causes files to include() stop working for a few moments.

Is there any way to update a file and is there how to recover a backup DURING THOSE "milliseconds"?

I thought of a solution, but I don’t have much knowledge to do this and I didn’t know how to research it and I believe that there must already be some solution, it’s not possible that only I have this problem!

Nginx/php-fpm says, for example:

Call to undefined function minha_funcao()

This occurs during these few minutes that the file is overwritten, but that is enough to fill up the logs and also enough to lose visits.

My idea would be to have some "backup".

If "include.php" does not exist then get from "backup/include.php". When you finish sending "include.php", then copy "include.php" (which is OK to copy) and send it to "backup/include.php" (which no one is using now).

But I have no idea:

  1. How to move files by checking that they are not being used (perhaps by the modification date? But...)

  2. How to point an alternative "include()", i.e., how to make you use the backup or not.

And....

Is there any software that already does this?

  • 2

    Go up and rename, much faster. Upload as.new file, and when finished, delete the old one and rename the new one to the right name. It won’t prevent you from being unavailable for a brief moment, but it’s fast enough for most cases.

1 answer

1


Usually you make the changes with the system paused, without executions. Because often an update can also modify the database, authentication rules, etc. So in these more critical cases, it is more recommended to stop all accesses.

Alternatively, for smaller cases where you don’t necessarily have to drop any connection, you could create a mirror of that system within the same server. So I would always do the updates on the "mirror" that is "sleeping".

Technique with Documentroot

Example, in Apache the original site is in the directory

/www/site/

The mirror, that is, an exact copy of all these files from the original is in another folder

/www/mirror/site/

Suppose that right now the site is running with 200 simultaneous connections and you have no loadbalance or anything. All you have is one server. So you have to deal with what you have.

You need to update 200 files, but without interrupting anything and also not causing data corruption.

So what you could do is update the files in the mirror directory

/www/mirror/site/

When completed, go to Apache and change the DocumentRoot to the location of the mirror

<VirtualHost 127.0.0.1:80>
#DocumentRoot "/www/site/"
DocumentRoot "/www/mirror/site/"
</VirtualHost>

Save the change and give a Reload or Restart.
Now the site will be running under the system with the current files.

This is just one of several techniques and has some points to note as possible conflict with some other system that is depending on the original location of the DocumentRoot. You may also have some loss with cache data. But they are usually small losses that recover in the first runs. Just be aware of dependencies that can cause conflict, but in a normal environment, it has no problems.

Note that there will also be loss of connections due to Restart in Apache.

Technique with Directoryindex

Another less invasive mode that follows the same logic of the root Document swap, is to do this by just changing the htaccess. But this will depend on how the structure of the system files is. If it is well organized you can perform the same scheme mentioned above, but using only the htaccess, without having to restart Apache.

The logic is simple, usually the system has only the file index.php in the public folder. You can have a second file, let’s call foo.php.

In the htaccess, just change the DirectoryIndex to that other file foo.php. The archive foo.php includes the mirror directory files.

All you need to do is follow the same logic as the virtualhost, only that instead of mecher directly in Apache, would change only the DiretoryIndex. A simple upload of htaccess would already change one directory to another.

And so you follow alternately. In the next update, you make the changes in the directory that is "sleeping".






Obs: In the examples I used Apache and PHP because they are more popular but can be applied in any other environment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.