setcooki
ALLROUND WEB DEVELOPER

06
Dec 11

PHP/XML based staging script

  
  
  

There are many software clients that let you sync content from your local environment to a remote server via (s)ftp but what to do if you have a multiple remote environments/server in form of a test/staging server in and live productive server. In this case you could of course upload your changes first to the test server and then do the same to the productive server. That of course has a major disadvantage since you may have made local changes which are not reflected/tested before syncing the two. Suppose you have others testing the application on the test server you are bound to not change anything while in test modus. So if you dont use any sort of cvs/svn system the only reliable way must be to sync the approved changes from the test server to the productive server while you can still locally develop further.

If you want to keep control of the sync up to the last bit then i am at least not aware of any linux library the gives you full control over the process, logging, test running and so on. Since in one of my projects we needed to find a way to sync multiple servers via a XML configuration file which is created via an admin panel. Since PHP already includes everything needed for a syncing content i set up a sync script which will copy content from one server to another by using PHPs built in sftp functions. Both servers run SSH with passwords so it was rather easy to sync the two. A ssh connections based on keys is also possible but in this case we didnt bother in the first version of the script. When using PHP as a basis for such a script you will not miss anything that a local client like Transmit or FileZilla would also do. The features in the current built:

  • compare by date or file size
  • skip files by extension, name or folder
  • exclude folders and subfolders
  • sync only files since modified date
  • set global chmod value for all files
  • resync by looking for abandond files
  • define multiple sync jobs in on .xml configuration file
  • run in test modus to output/logfile sync logs before running live

The script will only run on PHP > 5.3 and cli command line on Linux and needs SSH2 installed. Its is known that PHPs SSH2 lib is not running stable under each environment. I can not say if its true but the difficulties that came up where in connection with the most crucial part of the script – copying the files and setting the permission. In the end none but the “ssh2_scp_send” function really worked. All other wrapper methods did not work in the sense that you can not copy a file and change its permission in one shot since the copy action is a asynchronous event and will not report back once copied. The only function that does so is “ssh2_scp_send“.

There are many things that can be of course optimized like: auth with ssh keys, plain ftp support, .. but for a first launch it works as expected. You can find the script here: https://github.com/setcooki/syncd


Copyright © 2012 setcooki
Proudly powered by WordPress, Free WordPress Themes