Introduction
Okay, so too often this happens to everyone. Here’s the scenario - You’re writing your brand new post, you’ve just published it and you’ve put hours of work into it and you’re proud of your efforts. An hour later your host/provider has a server fart and your database and all your hard work goes down the drain. Your instant reaction is most likely rage. Your first thought is most likely: “there goes all my hard work! I can’t be motivated to write it again, so I’ll just sit back and wait until the urge builds up to rewrite the post.” And to think all of this grief could be saved with 5 minutes work. Here’s a few methods how:
Method One - cPanel
If you happen to be using a host that has cPanel, you can find the script I have written below to be quite useful for backing up your Wordpress database (or any other database for that matter, which is why I love it because it is not restricted to Wordpress). Either select the code, copy + paste it into a new file and save it as anything you want, as long as the extension of the file is .php. Otherwise, click the link below the code box to download the PHP source file. Simply change the file extension to .php and save it.
<?php #!/usr/local/bin/php -q function get_database_file($database_name, $domain, $username, $password) { //Construct the URL out of all our little pieces $url = 'http://' . $username . ':' . $password .'@'. $domain .'/getsqlbackup/'. $database_name .'.gz'; //Return the file's contents return(file_get_contents($url)); } function send_database_file($to, $from, $subject, $message) { global $database_name, $domain, $username, $password; $unique_sep = md5(uniqid(time())); $headers .= "From: $from\\n". "MIME-Version: 1.0\\nContent-Type: multipart/mixed;boundary=\\"$unique_sep\\";\\n". "charset=\\"iso-8859-1\\"\\nContent-Transfer-Encoding: 7bit\\n\\n" . "--$unique_sep\\n". "Content-Type: text/plain; charset=\\"iso-8859-1\\"\\n". "Content-Transfer-Encoding: 7bit\\n\\n". $message."\\n\\n". "--$unique_sep\\n". "Content-Type: gz; name=\\"wp.gz\\"\\n". "Content-Transfer-Encoding: base64\\n". "Content-Disposition: attachment\\n\\n"; $file = get_database_file($database_name, $domain, $username, $password); $headers .= chunk_split(base64_encode($file)) . "--$unique_sep--\\n"; if(mail($to, $subject, $message, $headers)) { return true; } else { return false; } } $database_name = "<Database name without cPanel username Prefix (eg: user_wrdp1 without user_)>"; $domain = "yourdomain:2082"; // Do not change the port number (:2082) $username = "cpanel_username"; $password = "cpanel_password"; $to = "your.email@domain.com"; $from = "backups@domain.com"; send_database_file($to, $from, $database_name ." Database Backup", "The backup of ". $database_name ." was performed at ". date("g:i A F j, Y")); ?>
Download this code: backup-sql-wp.phps
SECURITY WARNING
Now using your favourite FTP client, upload your newly created file In the directory under public_html/ or www/. I emphasised that because it is absolutely critical that this is where you upload it, otherwise you will be left open to someone being able to download your database information. And now we’re ready to roll onto the next step..
Now presuming you’ve either created a new php file or downloaded the file and renamed it, you need to login to cPanel and find the Cron Jobs icon. If you’re having trouble finding it, reference findcron.jpg for help. Note that the image you say may differ to your cPanel interface. Once you have found it, click it (duh) and youll be greeted with two buttons: Standard or Advaned. For now, choose standard and you’ll be greeted with a few boxes and inputs. See croninput.jpg for an idea of what you need to put in all these boxes. The settings I have selected in this image are that my backup will be emailed to me every day at 3am (Server Time). If you would like to change this, simply do so by using the boxes. If you have trouble understanding what to do, don’t hesitate to leave a comment or email me using the email page.
Method Two - Wordpress Plugin
So there’s many great plugins out there for Wordpress which can do everything from add ajax goodness on pages to backup your database, which is exactly what we want. For this tutorial, I have used WP-DB-Backup which was originally developed by skippy.net but was since abandoned and taken up by Austin Matzko and has since been rereleased for WP 2.1. Simply head on over to the WP-DP-Backup page, download the plugin and read the simple installation instructions. I would repost the incstructions here, but you can simply view them after you click the link. I myself have not used this plugin, as I am a faithful user of the above mentioned Method One.
Method Three - Wordpress Internal Backup
As far as I can tell from reading and watching a short video, this method makes the above mentioned Method Two of using a plugin obsolete (although having not used the plugin myself, it may offer extra functionality that the Wordpress Backup Tool does not). To save me explaining this method, a video on TubeTutorial which can be viewed via this link explains the method in great detail.
Conclusion
So there’s three methods to hopefully save you some grief in the long run if you do happen to lose your database(s). Just a quick shout out to the guys and girls at TubeTutorial for the use of their video in Method Three which is part of their series: 7 Essential Wordpress Hacks. It’s well worth a look.
If you feel I have missed anything in this guide drop me a line in the comments or use this revolutionary new technology called Email and get in contact, stranger.
Thanks for reading!
Technorati Tags: wordpress, database backup, wordpress tips and tricks




The very best method is to use phpMyAdmin if you can. Install and gain full access to not only back up but tool about with the database directly. You can even back up in a range of flavours there are also ports for platforms other than MySQL. Or if you can set additional hosts other than local host (as you can with with cPanel) then set your IP address and download a mySQL (or other flavour) free admin program which runs from the PC. “MySQL Control Center” or “MySQL Administrator” are good programs, I have both for looking after a local copy of MySQL I run on my dev PC.
I have used that method myself and may even consider revising this post an adding it, thanks for your input.
Good job, very useful information it will come in handy some day. THANKS!