Automatic upgrade script

Hi Guys- I have a commercial application written in PHP and I would like to allow my members to automatically upgrade to the latest release from within their admin section.

I have seen this kind of thing done within Wordpress, but I am just wondering what is the best way to create such an upgrade script? I guess what I am asking is, what are the steps I need to take in order to create a successful automatic upgrade script?

Thank you in advance.

I think the best way to think about it is to visualise the steps that a person would take when manually upgrading and simply automate that.

So they would:

  1. Check what upgrades exist
  2. Back up their database (and verify the backup)
  3. Backup all the files that will be upgraded
  4. Download the new version
  5. Unpack it into the correct locations
  6. Check all file permissions are accurate
  7. Run any database upgrade scripts that are necessary
  8. Verify everything is in the right place

When you break it down into steps, I guess it sounds pretty straight forward. I think the important thing is to keep everything backed up and verified at every stage so if anything goes wrong you can revert back to the original version.

Isn’t wordpress an open source project? Does it mean that you can download the source and find how they do this sort of thing?

Easier said than done. It’s better to understand the basics before attempting to pick apart the code of an existing code base.

I’ve wondered about this question as to how it’s actually done. My best guess is that you can use the fopen() function on an RSS feed or PHP file on your server, then another fopen() to actually perform the download of the zip/gzip file. After that, I’m not quite sure.

AFAIK WordPress doesn’t back anything up, I could be wrong though.

There is a plugin for WordPress which automates the entire process including backups I think.

That plugin became part of the base code in version 2.7. The steps it follows are similar to what corbyboy described.

for me,

svn update

I’ve developed similar in the past and it turned out to be a real pain in the rear. corbyboy has the right idea, think it through and code to match the human process. You may find this of some help

Best Regards George

If you want a really good auto upgrade system, then you have to use some sort of package manager like pear installer

The way these installers work is that a package usually comes it a .tar or .tar.gz file and this archive file has an xml file that contains information about every file in the package like where to install it, what file permissions to set (if it’s necessary), the checksum of each file and of cause the version number.

Then the installer on your server downloads the package (archive) from package website, unpacks it, examines the xml file and installs all files according the instructions in the xml file, if necessary it will also set writable or executable permissions on some folders and scripts. Installer may also change the schema of existing database tables or add a new table. All these instructions and new schemas are also in xml format in the package.

If something goes wrong, you can always revert back to the previously installed package, except that you should still backup the database before each upgrade, just in case.

This is difficult for package developers to maintain and may require the site owner to use sudo to run the installation with root privileges because when you run upgrade from browser, all scripts run under the same user as a web server and usually this account does not own the directories where files going to be installed.

This solution is great for enterprise system where site is hosted on a dedicated server and admin has root access and can setup sudoers file.

pear is not the only installer out there. The new pyrus installer that pear will be using in the near future is said to be much faster than the old installer.

The other way upgrade may work is that you supply the ftp login and password to remote installer. It then ftp to your server and uploads all necessary files, overriding the old versions of the files.

I’ve pondered this as well, specifically in regards to a custom cms, and how would I make it so clients can automatically or manually update application once I make overall enhancements to the system.

I’m sure this is a backwards way approach to it, and perhaps svn is worth looking into (which I haven’t really explored yet). But I know from the server-side, you can use fopen to write the contents of a php file.

see as follows, the script will open a file called multiply.php and write the php code to it…


$file = "multiply.php";
$handle = fopen($file, 'w') or die("error dude");
$message = "<?php\
$message .= "\\$result = (3 * 3);\
$message .= "echo \\$result;\
$message .= "?>";
fwrite($handle, $message);
echo "File ".$file." successfully created! View it <a href=\\"".$file."\\">here</a>.";

I wonder if you can have the updated file available for download from a central database (say on your server), so when the client goes to update a script, it reads the updated script from your database, then writes it to a file on their server.

I’m really a noob in this regard, and like I said, this method is probably backwards, but is something worth looking into. Say I made an interface that lets the client browse photos, but 1 yr later I make like the ultimate interface with lots more options for sorting, ordering etc. Having a way for the client to update their cms would be nice.

On a side note, maybe something with cURL is another option (I haven’t messed around with this yet either but I think it has to do with downloading url’s from other sites (your server to theirs)).

Ok, I have started to build my own script that will automatically upgrade the users software. I have managed to backup the database. Now I need to create a function that will backup the files on the site and create a gzip or zip file.

How can I read the contents of a directory and compress the contents on the fly?

Open Source can be nightmare to pick through

I’d be interested to see how you backed up the database on the fly. Where do you store the backups - on the server, or on the machine from which you launch it?

Generally backups are created on the server and you then need to provide a separate option for downloading the backup to the local computer. This can’t be easily integrated since it is necessary to ask where on the local computer to save the copy of the backup (since each local computer has its own arrangement for what is where).