Backing up your Blog, Part 2

In Part 1, I detailed how to set up an automatic backup for WordPress using a cron job calling mysqldump. This post has a slight twist on that method.

I recently had to set up a backup for a blog where this technique wouldn’t work.  The cron job would work and mysqldump worked, but whenever mysqldump was called from a cron, it resulted in an error: blocked because of many connection errors.

Normally, my first course would be to fix the underlying error, but in this case the blocking issue was only a problem for the cron backup and did not affect website performance.  Rather than figure out how to resolve the blocking issue, I knew that I could run the mysqldump from a PHP script and call that script from cron rather than trying to call the mysqldump from cron directly.

Here’s the PHP script I used:

    $version = 'live_12';
    $dbbackuppath = 'xxx';
    $dbuser = 'xxx';
    $dbpass = 'xxx';
    $dbhost = 'xxx';
    $dbname = 'xxx';

    $target_name = microtime();
    $target_name = str_replace(' ', '', $target_name);
    $target_name = str_replace('.', '', $target_name);
    $target_name = 'dbbackup_' . $version . '_' . $target_name . '.sql';
    $target_path = $dbbackuppath . $target_name;

    exec("mysqldump --user=$dbuser --password=$dbpass --host=$dbhost $dbname > $target_path", $out);

    echo("\n\ndone: $target_name");

For the real production code, all those variables at the top defining the file system path where the log will be stored and the database connection parameters are in an application config file.  I’ve included them inline here for completeness.

Basically this code creates a filename for the backup file which incorporates the application version number (to make it distinct from other backups saving to the same location) and the date/time the backup was run.  It then executes mysqldump using PHP’s exec command.

I’m not a big fan of using exec from script, but in this case it’s running in a non-public script and I’m using IP address filtering in htaccess to insure that this script can only be run by the server running the cron job.  It’s also saving the backup files in a location that is outside the web root path so these backup files cannot be retrieved from the server via HTTP.  Depending on what your site holds, it might be a very bad idea to make these backups publicly available even if they’re hidden.

With that script in place and after testing it to make sure it creates a backup file properly, you can automate the process using a cron job just like we did in Part 1.  The only difference is that in this case instead of running mysqldump, we use wget to hit the database backup PHP script.  To use wget, you just provide the URL of the script you want to load so your cron command looks like:

wget http://mydomain.com/dumpdata.php

While not quite as simple as using cron to call mysqldump directly, this method is easier to set up since you can test your dump script separately from cron.

Is there a better way?  Probably.  Give me feedback in the comments if you’ve got a more elegant way to handle automating backups of your site database.

 

 

 

 

3 Ways Cloud Services Can Help Your Business

Cloud services are a very hot topic right now, but many businesses don’t have a good understanding of what they are or how they could use them.

The cloud just refers to resources that are hosted on the internet.  If you use GMail or some other browser-based email, then your email is “in the cloud”.  If you use a hosting company to host your website instead of running your own webserver hardware, that’s in the cloud too.  As our internet connection speeds have increased, hosting files or accessing computers over the internet has become easier and more practical.  If you need a bigger hard drive or a new server, it may be cheaper and easier to use a cloud-based solution and let someone else worry about the hardware.

Here are three ways you can quickly start using the cloud to improve your business processes.

1.  Online Backups

You’re already backing up your work files, using a version control system, copying them to an external hard drive, burning to DVDs, right?  But all those backups are in the same place.  Having a second backup off-site is a critical part of having a reliable backup strategy.

One way to do this is to use one of the online file storage solutions that allow you to copy files to a cloud server just like you move files around your own computer.  Services like Dropbox and Box give you some free space to get started and allow you to pay for more if you need it.  Another option is Copingo which adds versioning and project management tools that help you share files and collaborate with others if you want to share your files among a team and not just back them up.

Another option for archiving your files to the cloud is to use a service specifically designed to make that easy.  Services like Carbonite will backup up your files automatically on a schedule and allow you to retrieve those files from any computer.

Carbonite is a fantastic service and is very easy to set up, but there is a monthly fee for not only the storage but the service.  It’s very easy and provides good support, but you pay for that convenience.  If you’re more inclined to work a bit to save the monthly fee, the open-source software Duplicati provides the same functionality as Carbonite and other commercial backup solutions.  It allows you to back up specific folders, include or exclude files by file type, and set schedules for both full and incremental storage.

If you’re using Duplicati, you still need to set up a place to store your files in the cloud.  There are many options, and some of the best in both reliability and price are provided by Amazon.  Amazon has a new service called Glacier which is designed for “cold” storage, files you want to store in the cloud but not access frequently.  The fees for storage and bandwidth are incredibly cheap for storing files and somewhat higher for retrieval.  This service isn’t the best choice if you want to host files for regular access, but if you just want a secure online backup, it’s perfect.  There are some concerns that retrieval prices could be very high under certain circumstances as discussed in this article at Wired, so it’s important to compare the terms of the service with how you intend to use it.

2.  Online File Hosting

Online backups are intended to be stored away and only accessed in emergencies, but sometimes you have files you need to store and access on a regular basis.  These may be files you want to share among a team.  If so, services like Dropbox, Box, or Copingo mentioned above are perfect.  If you want to share files with the world, you could just put them on your website, but often the bandwidth and storage size limits on your website hosting may cause problems if you’re providing big images or videos.

If you want economical hosting for keeping big files online, Amazon Simple Storage Solution (S3) is a great option.  It’s a little more expensive for storing files than Amazon Glacier, but it’s designed for fast and reliable retrieval, making it a viable solution for hosting big files that you want to be able to display on or link to from your website or elsewhere.

3.  Online Computers

File storage is important, but sometimes you need processing power too.  If your business involves having lots of computers working on data, cloud-based computing can be much more economical than buying and maintaining hardware.  Some services like Rackspace are tailored toward providing web servers, and they allow you to spin up a virtual server matching whatever specs you need in terms of processors, hard drive space, operating system and pre-installed software like databases.

Another option is Amazon Elastic Compute Cloud (EC2).  Similar to Rackspace, EC2 lets you spin up servers to meet your requirements.  Their pricing plan is particularly attractive if your needs fluctuate.  For example, if you do video processing or crunch big data sets for a few days a month, the EC2 service lets pay for what you need.  Their fees are based on processing time, so you pay less when your instances are idle and you can turn them off entirely when not needed.

Trivia:  Why is it called the cloud? 
When we draw network diagrams to design how systems are going to work, we represent the internet as a cloud-shaped blob.  If I’m designing a system that will transfer files over the internet between two locations, I’ll draw the network details of both ends but just put a cloud-blob in the middle to represent that the traffic is moving through the internet.  When it became practical to move services from our desktop computers to computers hosted out on the internet, it was a natural extension of our network nomenclature to refer to these as services hosted in the cloud.  So “the cloud” just means something is hosted out there on the internet somewhere, you don’t need to know where the actual hardware is as long as you have the internet address.

Backing up your blog

Everyone has heard that backups are important, but are you sure you’re backing up the data you really need?

If you write a blog or run a website of any kind, there’s probably a database being used to generate the pages that users see.  If you’re not backing that data up regularly, everything you’ve created on the site could be lost.  A few simple steps can give you the security and peace of mind that all the content you’ve created is save and can be restored if needed.

The first step is logging into your hosting account.  If you have a blog set up on a host like GoDaddy, DreamHost or Bluehost, you might not have logged into this account since you did your initial setup, but you should be able to go to the hosting website and get to your account settings.

Many hosts will refer to this as the cPanel.  Others will just refer to it as “My Account” or “My Hosting Account”.  Browse through your hosting account website to find the screen that gives you access to your databases. Screen 1 shows the option as it appears on the control panel at BlueHost and similar services and the option is called “phpMyAdmin” which gives you administrative access to your databases.

Screen 1

Logging into your MySQL database with phpMyAdmin, supply the username and password you use to access the database on Screen 2.  This is NOT your main account username and password.  This is the username and password used by WordPress to access the database.

Screen 2

When you’ve logged in, you’ll see a general information screen shown on Screen 3.  On the left it will have two items, the general schema database which is called “information_schema” in this case and your main database.  On the right there are tabs for performing different actions on the selected database.

While you’re on this screen, make a note of the URL your browser is displaying (underlined in red on Screen 3).  You’ll use this later if you want to automate the backup process.

Screen 3

Select your database on the left (circled in red on Screen 3, but yours will not be named “my_database”) and you’ll see the tables within your database listed on the left as shown on Screen 4.  The right side will also show the tables in the Structure tab.

Screen 4

Click the Export tab at the top, circled in Screen 5.  Initially, the radio button at the top labeled “Quick” will be selected and the page will be very short.  If you select the “Custom” option below “Quick”, the options on the page will expand as shown on Screen 5.  You can leave all of these in the default state, no changes are necessary, but if you want to limit what data is saved or customize the format, you have the options available.

When you click the Go button at the bottom of Screen 5, you’ll see a Save-As dialog allowing you to save the export file on your own computer.

Screen 5

After you’ve saved the file, you can log out of your MySQL database using the logout button at the top of the left panel or just close the browser window.

Automating the Backup

If you want to automate this process and save backups on schedule, go back to the control panel screen we used in the first step, find the option for “Cron Jobs”.  Cron is a time-based scheduler that lets you set up tasks that will be executed at specific times in the future.

Screen 6

At the top of the cron-job setup screen, there is an block to set up an email address which will be notified each time the task executes.  This email address, underlined in red on Screen 7, will probably be set to your main account’s email.  If it’s not set or you want to change it, use the “Update Email” input and button below it to set it to the email address you want notified.  You may be inclined to leave this blank because you just want this taks to run and not keep telling you about it, but in the beginning you need to get the notifications to make sure the task you create runs successfully (and receive helpful error messages if it doesn’t).

Screen 7

The next step is to set how often the task should run.  You need to make a decision based on how often your site is updated whether the backups should run weekly, daily or even hourly.  If you update every day and there’s a lot of activity on the site, you should choose daily.  You might be safe with weekly if your site is less active.  The “Common Settings” dropdown box circled on Screen 8 provides a shortcut for filling in the boxes below it.  For example, if you select “Once a day” as shown on Screen 8, the minute and hour boxes below it are filled in with zeros, indicating that it will run at midnight (0:00) and the day, month, and weekday are filled in with asterisks indicating that the task will run every day, every month and every weekday.  You can change the numbers in the hour and minute boxes if you want the task to run at a different time every day.  You can also change the other inputs to customize exactly when your task repeats.

Screen 8

The last step is to fill in the Command at the bottom of Screen 8.  This is the task that will be executed every time your schedule job runs.  This could be almost any task that will run from a Linux command line.  For our needs in backing up a database, we want a command like:

mysqldump --opt -Q -h myserver.myhost.com --user=my_db_username 
--password='my_db_password' my_database_name | gzip -v9 - > 
/home/myaccount/public_html/archive/_db_backup-
`/bin/date '+\%Y-\%m-\%d_\%H-\%M'`.sql.gz

Let’s go through that step by step:

mysqldump – this is the command-line program that comes with the MySQL database which will create an export file with all the table schema and data just like we did manually in the first few steps of this article (Screens 1-5).

–opt – this is an arguement for mysqldump that specifies certain outputs.  It’s shorthand for a collection of individual arguments and sets the most common outputs:  add-drop-table, add-locks, create-options, disable-keys, extended-insert, lock-tables, quick, set-charset.

-Q – this sets the quote-names parameter that will cause table and field names to be surrounded by quote marks in the output file.  This may not be necessary for your database, but it’s necessary in some situations and doesn’t hurt to leave it in.

-h – indicates that the next value will be the hostname of the server where the database is located

myserver.myhost.com – this is the server name of the server hosting your database.  We made a note of it earlier when looking at Screen 3.

my_db_username and my_db_password – this is your database username and password, the same ones you used on Screen 2.

my_database_name – this is the name of your database, the name that’s circled in red on Screen 3.

/home/myaccount/public_html/archive/ – this is the path to a directory where you want the files to be saved.  You may need to check your site’s FTP settings to find the root path (the /home/myaccount part).  You’ll probably want to create a new directory like “archive” or “db_backup” to store your export files in.  You’ll need to make this directory writable using your FTP tool.

The command up to this point, mysqldump –opt -Q -h myserver.myhost.com –user=my_db_username –password=’my_db_password’ my_database_name, creates the MySQL export file.  This is followed by the pipe character, |, that will send the output of the export to the next part of the command line that will save the file.

gzip -v9 will compress the output file into a GZ file, sort of like a zip.

-> is like the pipe command, it will send the output of the gzip command to the filename specified next.

The remainder of the command line specifies the file where the export will be saved.

/home/myaccount/public_html/archive/ – this is the path to a directory where you want the files to be saved.  You may need to check your site’s FTP settings to find the root path (the /home/myaccount part).  You’ll probably want to create a new directory like “archive” or “db_backup” to store your export files in.  You’ll need to make this directory writable using your FTP tool.

The next part is the filename you want to use within that directory:  _db_backup-`/bin/date ‘+\%Y-\%m-\%d_\%H-\%M’`.sql.gz

The file name will start with “_db_backup-” followed by the date formatted in YYYYMMDD_HHMM format followed by the .sql.gz extension.  Using the date command will provide a unique filename for each file that’s created by the scheduled task.

Fill in the command line with all these parameters set with the values for your database and click the Add New Cron Job button shown on Screen 8.

You might want to set the time for a few minutes from the current time (accounting for the timezone change between you and the server) so the cron job will run soon and you can check the output.  You’ll get an email indicating the success or failure of the task.  Then you can come back and edit the time to have it run sometime in the middle of the night or whenever traffic is lowest.

If everything has been set correctly, you’ll start to get backup files in the directory you created.  Now you just have to remember to download them and clear out that directory every so often so it doesn’t fill up and use all the space in your hosting account.

Many hosts will have a slightly different setup.  If your screens don’t look like the ones I’ve shown above, post a comment with info about your host and I’ll try to add details that will help you.

Why You Need Version Control – Especially if You Don’t Know What That Means

Have you ever been editing a document and realized you just completely messed up the whole thing?  Maybe you accidentally deleted a major section.  Maybe you made a lot of changes and realize you’re going off track and would rather start over with an earlier version?  Sometimes you can use the undo feature to reverse the changes or you haven’t saved yet and you can just quit and re-open the good version.  But what if you’ve saved and quit and realize you’ve just made a huge mistake.  Many people keep backup copies of their work, and that can prevent a lot of problems.  Some people copy files and rename them with something like “_bak1″ or a date like “_20120607″ appended to the filename.  Maybe you copied a version to a thumb drive or emailed a copy to yourself to keep a backup.  Whatever your backup plan, rolling back the clock might save you a lot of additional work.

Version Control software is a system you use to keep track of the things you work on that takes periodic backups to another level. Version control provides a lot of features, but the most important one for most people is the ability to roll back to a previous version of whatever you’re working on.  Version control software is often used by big teams writing software because it provides features for checking out files from a central storage location, merging changes when multiple people edit the same file, and tracking not just what was changed but comments about why the changes were made over time.  This is invaluable when you have a lot of people working on the same set of files.

While many people think version control is exclusively used by large teams, it is invaluable even if you’re one person working alone.  Whether you’re writing software, editing images, or working on your novel, using a version control system not only gives you regular backups of your work, but it allows you to easily roll back and view a previous version if necessary.

There are many different version control systems available, and some of the best are available for free.  Personally, I use Mercurial with the TortoiseHG plugin which gives me great content-menu support in Windows.  I’ve also used Subversion, and it’s widely used for open-source software projects.  There are other systems, even commercial ones like Microsoft’s SourceSafe, but it’s hard to find any reason to use them over either Mercurial or Subversion.  Users of Mercurial and Subversion will argue continually over which is better, but unless you’re a real power user it’s best to just pick one and get started.  I’d recommend skimming a couple of tutorials on each and picking the one that seems to make the most sense to you so you can get up and running quickly.

Mercurial Tutorials:

Subversion Tutorials:

A Case Study of Building an Internet Presence

Steven Harris is an expert in alternative energy and has a broad and deep internet presence.  He has a personal website where he has a short bio and a list of books and courses he’s authored.  He runs Knowledge Publications where he publishes books on alternative energy systems authored by himself and others.  He maintains Solar1234 where he lists relevant books and other materials as well as links to his podcast appearances.  He sells both equipment and guides for generating your own alcohol-based fuels at IMakeMyGas.  He provides a free preparedness course at BeforeTheStormHits that has been viewed by over 100,000 people.  He also maintains a mailing list of several tens of thousands, which makes him an influential voice in the alternative energy marketplace.

I first ran across Harris about a year ago in the context of easy home solar projects.  Looking at the breadth of the material he had available, I assumed he had spent his whole career in consumer-oriented alternative energies.  Recently, I was listening to his guest appearance on an older episode of The Survival Podcast (TSP) where he discussed his easy home solar projects.  He mentions that he spent a good deal of his career working in very high-temperature commercial solar power systems.  Late in the podcast (about the 1:00 mark), he discusses his career arc and how the recession “knocked him flat” and left him without a clue about what to do next.  I don’t know the details about what happened, and it’s really none of my business.  What’s important for this blog is that with some encouragement from friends and a lot of hard work, he pulled himself up by his bootstraps and created the internet presence noted above.  I’m interested in his work in solar power, but I’m absolutely fascinated by him as a case study for running an internet-based business.

As I discussed in Three Reasons Your Business Needs a Blog, a blog provides a way to leverage your expertise in your field and build a reputation that can contribute to your business.  Harris has gone way beyond this by creating his network of sites.  Some key points that Harris nailed in creating his internet business:

  • re-focused his expertise to aim squarely at the consumer market.  The market for big commercial systems may involve big dollars, but it’s vanishingly small.  The market of people who can afford to pay for home systems is a little larger.  But by aiming at the DIY market, Harris has tapped a huge group of people who want to save money, be a little greener, and are willing to put in the work.
  • gives away basic content.  His sites linked above have a wealth of free videos, podcast appearances, and other resources to get you started for free.  This brings people in when they’re searching for info on these topics.  It gives them a taste of the quality of content he provides.  In the TSP podcast linked above, he says a fundamental part of his strategy is to give away the first-level info so people will come back and be willing to pay for the second- and third-level info.
  • markets himself relentlessly.  Harris has built a wide network of websites to cross-market his content, and he makes the most of guest appearances on podcasts to both provide valuable content for free and market his commercial resources.
  • creates discounts and bundles of products to increase exposure to his materials.  He gave a generous discount to supporters of TSP where he has guested several times, and he provides discounted bundles of books on different topics which helps to upsell potential buyers.

Not every business owner is going to build a network like Steven Harris, but every business can benefit by looking at the example he has set and seeing how even a small part of that could apply to their own business.  Can contributing to a blog or appearing as a guest on podcast help build your reputation and drive traffic to your website?  Can you create discounts or bundles of products aimed at specific internet audiences?  Can you give away guides or other resources that will help demonstrate your expertise and bring people back for more?

Building a profitable internet presence is hard work, but it’s not rocket science.  You don’t have to be an internet guru.  It’s mostly a matter of taking the expertise you already have in your field and using it to provide value to your audience and build your reputation.  Whatever business you’re in, with a little effort you can leverage what you already know to build a remarkable internet audience.

 

Three Reasons Your Business Needs a Blog

For many businesses, getting their website off the ground can be a major endeavor.  Whether you want a fully functional e-commerce site or a simple brochure page pointing visitors to your business, it can be be difficult or expensive simply because it’s outside your main expertise.  But regardless of what kind of business you have, having a website is virtually essential in today’s world.

Once they have their website up and running, many business owners balk at the thought of adding a blog.  Not only is this a new separate site, but it requires almost daily, at very least weekly, updates to really have any value.  The good news is that once it’s set up, running a blog is easy and is directly on topic for your main expertise.  It’s just a way for you to talk about the things you know best.

Here’s three reasons your business should have a blog:

1.  Provide your customers with information

It doesn’t matter what kind of business you have, the first thing your customers are looking for is more information to make an informed purchase.  Whether you’re selling widgets to industry, running an artsy boutique, or selling consulting or training, your customers can use more info on your business and general industry niche.  By providing this info, you demonstrate your expertise and provide value to potential customers that will draw them back to your site.

If you just use your blog to reiterate your catalog and advertise yourself or, worse, trash-talk your competitors, you’re providing more spam than value.  But if you talk about the bigger picture, trends in your industry, implications of new technologies or regulations, then you’re giving customers and potential leads real information they can use.

2.  Build your own reputation

Your blog is no different than any other networking opportunity, a way to get your name in front of potential leads.  By using your name and photo on your blog, you’re associating your personal reputation with the things you write.  If you follow the first point above and write content that really contributes to your industry, then your reputation will grow accordingly.

Building your personal reputation will improve your business, leading directly to more customers.  In addition, it will lead to more exposure, the opportunity to guest-blog on more prominent blogs, contribute to industry journals, and to speak at conferences or other industry events.  All of this exposure builds your reputation further, continuing the cycle.

3.  Traffic, traffic, traffic

On the internet, traffic is key.  How do people find your site?  You can buy ads and trade links with other sites, but in most cases the majority of visitors to your site will find you using a search engine like Google.  And how does Google rank your site?  They calculate how you rank for different keywords by measuring how other sites link to you for those keywords.  By having a blog, you’re providing a little bump to your main site by linking from your blog to your site, but the main benefit comes from people linking to your blog entries.  As you become more involved in writing about and discussing your industry, more people will link to you as they do the same.  This is closely linked to the value of your content and your personal reputation discussed above.  As you create content that people find meaningful and build your reputation as a voice in your industry, more people will link to you and that means the search engines like Google will rank you as more relevant for the keywords involved.

 

Many business owners are reluctant to get involved with blogging.  They see it as a distraction and not a good use of their time.  But if these same people were to ask any reputable internet marketing expert how to increase traffic to their website and build their business, the number one answer by far would be to start a blog and write good, relevant content for their industry.  Many business people will spend a few hours a week going to networking events in their city, but by spending the same amount of time generating quality content for a blog, they stand to reach exponentially more people, build their own reputation, and improve their main business in measurable ways.