Monday, 2 September 2013

Using Google Drive for Server Backup

Automated Backup Jobs

Recently we reviewed our server backup to make sure everything is working correctly and we also looked into improving our solution as always.

Our current solution uses a bash script to create some tar archives which are then compressed with bzip2 which gives us some significant file size reduction especially for text files which our database dumps are.

When the backup archives are created the bash script uses mutt to email the archives to a Google gmail account this solution provides us with a nice easy to access backup files.

Recently we have been running into trouble with the size of our backup files so one constraint we had was using a different storage method we decided upon using Google Drive for the job.


Image of some clock work cogs
Automation


What to Backup

The first task we had was to review our backup and check we are saving the correct files and directories and we are not missing anything important.

For us we needed to backup our subversion repositories which contain all of our code base and revision history so this is an important asset to us.

Next we have our bug tracking software which is a Bugzilla install so we have to backup the database tables for that.

We have a few other databases that need to be backed up along with the bugzilla database so that can all be handle in one swoop.

Finally we need to backup the web root of all our website that we host the reason we backup the source tree here and not just use SVN is due to images being uploaded and other files which are created dynamically.


Backing Up Databases

Here we use passthru to execute a system command to dump the databases then create a md5 hash of the dump file then archive and compress them. We should be able to make some significant space savings using compression.

The reason we create a checksum of the file is to help with determining the archive is the same as the day it was created. We check them manually a few times a year to verify it is working correctly and the checksums match, more on this later.

For dumping all databases you can use the --all-databases switch for the mysqldump program the following will backup our databases.
mysqldump --user={SQL_USER} --password={SQL_PASS} --all-databases

Using the command above as the first parameter for the passthru(); function will dump all the data we need.


Backing Up Subversion

Our server has multiple subversion repositories so we need to backup each one and generate a file checksum using the md5_file(); function. For our solution we create a DirectoryIterator() object and iterate through the directories under our SVN root directory.

By testing if the item is a directory and not a dot we can use the svnadmin program which we can call using passthru again.
svnadmin dump /path/to/repos > dump_file_name.svn

Once we have iterated through the array we can create a tar archive of the dump and md5 checksum files which we then compress.


Backing Up Website

Finally we backup our website root directories so we have a copy of any files that have been generated automatically such as news cover images.

We decided not to backup the whole of the source code files under some of our web roots because we use subversion for revision control we have our tagged releases and the HEAD of the trunk.


If we need to restore it will be restored from the subversion backup this makes more sense to us as we can use stable.
tar -pcf webroot_backup_file_name.tar /path/to/web/root

Using tar simplifies the creation of an archive of files we will be compressing this tar achive to try and make it smaller in size.

Compressing Data Files

When we have got all our data in a tar archived we like to compress the archive to try and reduce file size and reduce bandwidth usage. Although bzip2 is slower than gzip we can achieve a greater reduction in size.

The program bzip2 has a few switches one of them is to set the block sized used with -9 offering the most efficient but slowest compression speed this is the option we use as time taken does not matter and all tasks are run with a nice level of 10.

Using Google google-api-php-client Library

To assist in using Google Drive as the back end storage for our backup solution we downloaded the API library from Google to help us speed up development.


Using the library allows us to interface with Google Drive you can also use curl to upload files by sending POST requests but we chose to use the PHP library.

Object orientated design allows us to reuse our upload code for each type of backup (database, subversion, website, system) by having a base class and extending this class for each concrete implementation.

If you download the library from here and extract it to a directory you have access to you can use it to access all of Google's API's using one set of libraries not just Google Drive.


Upload Code

All of the code needed to use Google Drive as a back end for database backups is included below all you need to modify is the {FILE_TO_UPLOAD_NAME} this should be the path of the file to upload and change the mime type if you are uploading something other than a bzip2 file there is a list of mime types here.
require_once('{PATH_TO_GOOGLE_LIB}/src/Google_Client.php');
require_once('{PATH_TO_GOOGLE_LIB}/src/contrib/Google_DriveService.php');
require_once('{PATH_TO_GOOGLE_LIB}/src/contrib/Google_Oauth2Service.php');
$File = new Google_DriveFile();
$File->setTitle({FILE_TO_UPLOAD_TITLE});
$File->setMimeType('application/bzip2');
try {
    $data = file_get_contents({FILE_TO_UPLOAD_NAME});
    $createdFile = $this->Service->files->insert($File, array(
        'data' => $data,
        'mimeType' => 'application/bzip2'
        )
    );
    echo "File ID: " . $createdFile->getId() . "\n";
}
catch (Exception $e) {
    echo "There was an exception, message was: " . $e->getMessage() . "\n";
}


PHP Client Shell

We have also implemented a command shell which can be used as a client interface to the backup system allowing all the functions to be carried out by issuing commands such as list to display the contents of the Google Drive.

So far we have implemented the following commands and we will be adding some more in the future if we feel they are needed. There are also shorthand versions of the commands available but they are not listed below (eg.. dl for the download command).

  • list, List contents of current Google Drive directory
  • upload {FILE_NAME}, Upload the file identified by {FILE_NAME} to the Google Drive
  • download {FILE_ID}, Download a file identified by its Google Drive file id
  • backup {TYPE}, Perform backup action of {TYPE}. {TYPE} can be database|subversion|website|system
  • remove {FILE_ID}, Remove the file identified by {FILE_ID} from the Google Drive
  • system {ACTION}, Run a system command action

Conclusion

Our new implementation of our backup solution is quite heavy for the task at hand and anything we have done here could be done using a bash script. If you take this approach you could use curl from the command line to post the files to the Google Drive.

In a future article we will be covering the command shell part of our backup system and looking at our system restore scripts which we use to help automate building and configuring a clean Debian system install.

Monday, 26 August 2013

New Blog License

Creative Commons

We have decided to release our company blog posts under a creative commons non commercial share alike license.

You can view a copy of the license by following the links in the footer of our blog posts above the copyright statement.

In the end we decided to use a non commercial because this is a company blog so we would prefer it not be used commercially.

The creative commons is a very simple license to read which also contributed to our choice and we are considering and hope to use this license for our other authorship works.


Creative Commons Licence
Web development blog by Chris Elsen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Based on a work at http://chriselsen.blogspot.com/.

Saturday, 24 August 2013

New Feature Release: Mobile and Tablet News

Care and Nursing Related News

Our news system has now been ported to our mobile and tablet specific website so you can now read the latest news headlines from http://m.carehomes.net/news/ you can also visit any news categories from the headlines page.

With the release of our news section we feel value has been added to our website and we have noticed an increase of visitors which corresponds with our release. Our returning visitor percentage has risen along side so now our visitors are becoming repeat visitors while the main directory seems to be more a single visit session.

By taking advantage of Google Analytics we have been able to monitor our visitor usage which in turn helps us improve the most commonly used pages and invest our time on worth while pages.

Please remember we do have RSS and Atom feeds available for our latest headlines if you are a web master please feel free to use our feeds if you would also link back to our news headlines page it would be appreciated.

Screen shot of our mobile news headline page.
Mobile and Tablet Version

Friday, 9 August 2013

Local Amenities Listings Surpass 70000

More Business Listings

Our local amenities database has now surpassed 70,000 entries and is still growing we have also found it is a popular feature with our visitors.

We will be adding support for creating your own business listings in the near future as we hope to build a comprehensive database.


Enhanced Listing

Saturday, 3 August 2013

Care and Nursing Home Android App

Native Android Application

To complement the new mobile and tablet release of our care and nursing home website we have now developed an Android app for your download.


Mobile App Interface


You can download it using Google play by clicking the button below, there is an app package file avaliable from here (apk) or you can use Google play to install by just search for the word Care Home or net.carehomes.carehome.


We will be writing a new article on the development of our mobile device strategy soon, as you may know we have been very busy developing new features to help improve our user experience.

Also we have nearly completed our first draft of an article discussing debugging in PHP with xDebug and profiling scripts with the help of kcachegrind.