Automate MySQL Database Backup With WebCron – No Cron Jobs? No Problem – Part 1

Clock

What To Do When You Can’t Cron

One pet peeve that I have with 1and1 hosting was their lack of Cron Jobs. Cron Jobs are basically the Unix method of scheduling stuff, much like the Windows Task Scheduler. If you’re on a Unix/Linux host, then they use cron jobs to schedule their internal activity. But not every host is kind enough to give you the same power to do so. Now that I’m on DreamHost, I don’t have this problem anymore, but a lot of shared hosts still lock up the cron jobs. And since hosts like 1and1 aren’t too reliable with their backups, doing your own backups becomes, more of less, essential. There are, of course, alternatives if your host doesn’t provide you with cron jobs, such as phpJobScheduler (used this a lot back in the day) and pseudo-cron.

How The Other Guys Work

The way these work is that, they keep track of tasks and times every time the cron job is called, but you need to include them in one of your highly accessed pages (e.g. Home page). With phpJobScheduler, you can include the file in your PHP code, or you can include it using the img tag. Using the second (2nd) method spits back a transparent GIF of 1 pixel, so nothing will be displayed on your page and you could even use it within plain HTML pages if you needed to. When this is triggered, it checks the database for time of each task, compares that to the current time, them if x > y it runs the task. Simple really. Since they need to be triggered via a user visit, it’s never going to be pin point accurate; this is why you need to include it on a page getting high traffic. Some of the scripts even have an option to build in to help with this. It’s a sort of buffer time. This says that even if the specific start time of the task hasn’t arrived yet, run it anyway if it’s within the buffer amount. You would increase or decrease the buffer based on your anticipated traffic.

It’s Just Not As Good As The Real Thing

Not Time Specific

We’ve already addressed the specific time thing. Yes, that sucks, but who really cares right? I mean we’re using this for database backups, which should be running daily at most. So it happens at 2:30 AM instead of 2:00 AM, no biggie. Yes, your backup relies on the hope that some insomniac is going to be surfing your site at 2:00 AM, when you scheduled it. If not, still not a big deal. The visitor at 8:00 AM will trigger it. Again, time doesn’t really bother you, you just want to have a backup that’s all.

Eats Up Page Load Time

Web BroswerThere is one further problem, and this one I couldn’t live with. Since these are being embedded in your page, when a task is triggered, it actually runs at that time. There is no way of telling PHP “Hey, would you mind running in the background for a while?”. So what does this mean? This means that whatever tasks are running are going to be executed as part of your page load (or image if you used the img tag). This isn’t too bad for smaller tasks. Who’s going to notice a one time (2) second delay? But remember our purpose for this: Database Backup. My backups are small right now are small, but still run an average of 25 seconds. Who’s going to be dedicated visitor that sits there for 25 seconds waiting for your page to load? One solution is to put this at the end of your page, so that it loads first. But still, the browser is going to be active. And if you’re visitors are anything like me, they’re thinking:

“Wait, I’m on a 5 MB connection, what’s taking so long? Are my torrents downloading now? No. Oh crap, Firefox crashed didn’t it? But no, it still scrolls. Let me try going to Google.com in another tab. There it is, it comes up fine. I’m just sitting on the stupid blog; I guess the guy’s server is having issues, because 25 seconds is way too long.”

There’d better damn well be something worthwhile after those 25 seconds, because you’ve just slightly annoyed a visitor. A visitor who can find content very similar to yours somewhere with an eight (8) second wait. Even though this only happens once a day, I don’t feel that it’s fair to subject anyone to that sort of wait time. It may very well be one of your dedicated, loyal visitors that stumbles upon this and you just piss him off enough to make him unsubscribe to your feed.

The Real Alternative

WebCron. This has been a tried and true service for me. First of all, it’s free. You can’t beat that. If you’re on a shared host that doesn’t offer cron jobs, I’m thinking that money is already tight enough. It’s in French, but it’s free. Now, as with every free service, you need to finesse it a little bit to get it to suit your purposes. From what I gather, they allow you cron tasks on a credit system, where you can buy credits and then you’d be able to schedule more tasks and at greater frequencies, but we’re not interested in spending money here. The free service shuts off your task if the history has been allowed to build up past a certain amount. You can purchase credits that automatically clean out the history (I think) or you can get off your lazy butt and once a month, log into the service and dump the history on your daily backup task. Now of course, if you use this to schedule something every hour, you’re going to have a little more foot work to do. But for our purposes here, this service is great. They take that 25 second hit on their system so your visitors don’t have to. Now of course, since this is coming from a remote server, you can’t use it run Perl scripts and the like. Anything you schedule has to be accessible through a browser window. So off you go, have some fun!

Part Two

Stay tuned for Part two, where we actually get into the backup.

Trackbacks

  1. […] Web Cron is a great method of automating tasks on your server if you’re not blessed with cron jobs. However, after writing this I stumbled onto something that may fit your needs a bit better; Remote Cron is that service. It’s everything Web Cron is and more: […]

  2. […] I can’t remember where exactly I heard about this web host, but InsaneGB.com sounded like a great deal. You can read their website for more details, but in a nutshell they got PHP 5, MySQL 4, and everything (Databases, Add-on domains, etc) is unlimited except disk space and bandwidth. They give you 5GB for disk space and 20GB on bandwidth per month. Which is great for a free host, if you ask me; heck, they even offer cron jobs. […]