Using both a PC and a laptop, I have always had the problem of synchronizing my web developement environments. The obvious solution would be to work online, but for convenience I prefer a local XAMPP environment.
Therefore, Today’s Task: Find a way to synchronize XAMPP environments between my laptop and computer that works without any manual intervention and also keeps the MySQL databases in sync.
Now, I am aware that a database should not be “synchronized” but replicated normally; since setting that up to work reliably with servers that are not constantly connected to the internet is a pain in the ass, so I was looking for something simpler.
The solution I found is actually pretty straightforward, but it only works for a one-person-setup using one operating system on all devices; having multiple users work on the same project at the same time will break this setup, and having different OSs will most likely lead to (collation…) problems.
Since data security is also a concern for me, my solution relies fully on open source, self-hosted software – namely, OwnCloud. Here’s what you need to do:
- Set up XAMPP on both / all machines if you don’t have it already.
- Manually get all projects’ latest versions to one machine (this will be your primary machine for the inital sync process).
- Set up an instance of OwnCloud on your server. It’s easy to do, so I’ll just refer you to the manual. If you don’t want or can’t use your own server, you can rent an account from another server, see the OwnCloud homepage for hints where to go to.
- Install the OwnCloud client on your machines.
- Do NOT synchronize the whole xampp directory. Instead, add the following folder structure to your OwnCloud:
- DocSync – this will be the folder you set up your regular files to, if you’d like to use that feature. It took me a while to figure out that it’s not a good idea to use your ownCloud root folder as default sync folder.
- Now, create different sync targets on the first (primary) machine
- one for the files in each project you’d like to sync (i.e. xampp/htdocs/project1) as subfolders in httpdocs. That way, you can selectively synchronize the projects you’re currently working on without wasting resources on old stuff. It’s a great way to keep an archive of your projects.
- one for the MySQL database files pointing to the database folder. They’re locally kept in xampp/mysql/data.
- This time synchronize the whole folder, as leaving the metadata out of sync may cause problems. You may add exeptions for databases you don’t wish to sync, just except the folder corresponding to your database name from sync.
- Repeat step 5 for all other machines, but make sure your local target folders are empty to begin with. Having files in there may cause sync problems. That’s why you had to manually bring your primary machine up to speed in step 1.
- Optional: Turn on ownCloud’s “versions” and “deleted files” apps to have additional safeguard against data loss on your precious projects. This even allows you to restore old versions of a file should you ever fuck up badly. Not that I ever do. Ahem.
That’s it, you’re done. Depending on the size and number of files in your folders, first sync may take a while (though recent versions of ownCloud have become much better at handling large numbers of small files).
BUT: take care to only have one developement instance running at a time! Starting the MySQL server in XAMPP locks some of the files in the mysql/data directory so ownCloud won’t be able to sync them until you switch the server back off. Obviously, making changes in two locations at once will produce collisions. Since I can hack away at either my laptop or my PC, this tends not to be a problem for me.
Another word of caution: I have repeatedly experimented with ownCloud’s Encryption app. Besides the obvious concern of having encryption keys stored on the server, the current version is too badly broken to be of much use. I have twice had to wipe my ownCloud server to get it back to working normally after something in the encryption app fucked up, so I recommend you not to use it for now.