mcdruid.co.uk - mysql http://mcdruid.co.uk.php53/taxonomy/term/42/0 en comparison of compression methods for database dumps http://mcdruid.co.uk.php53/content/comparison-of-compression-methods-for-database-dumps <p> I like to have automated daily database backups set up on my servers - for those hosting relatively small sites, the database dumps can usually be compressed down small enough to be sent as an e-mail attachment by the cron script which does the database dump. This is a really simple and cost-effective way of having offsite backups which provide daily snapshots of sites which are easy to access if anything goes wrong. </p> <p> Initially I just used gzip to compress the sql files down, but as dump files inevitably grew, I decided to look into some alternatives, and in this post I'll share the results of my research. As I'm often sending these backups as e-mail attachments, the size of the compressed file is the main thing I care about, but this isn't the only factor in a comparison. Some of the methods of compression use more resources than others in terms of CPU and memory. I'm not too concerned about this as my dumps are only being done by cron once a day, but I've included a measurement of resource use; the time taken in seconds (as measured by the <a href="http://linuxmanpages.com/man1/time.1.php" rel="nofollow">time</a> command). </p> <p> The compression methods I tried out included gzip and bzip2 (which should be available on most *nix servers), <a href="http://rzip.samba.org/" rel="nofollow">rzip</a> (written by Andrew Tridgell of samba and rsync fame) and finally <a href="http://www.7-zip.org/" rel="nofollow">7zip</a>. In all cases, <em>fast</em> meant using the <em>-1</em> switch, and <em>best</em> meant using <em>-9</em> (n.b. rzip also has a -0 setting, but this didn't yield useful results). </p> <p> For my tests, I used 3 different SQL files (uncompressed they are 16mb, 48mb and 83mb approx) - they are mostly made up of dumps of drupal sites, although there are a few other databases in there (each one is a mysqldump of all databases on a mysql 5 server). Here are the results: </p> <table class="tochart typebvg size700x250"> <caption>Compression comparison - sizes in mb</caption> <thead> <tr> <th> &nbsp; </th> <th scope="col"> 16mb </th> <th scope="col"> 48mb </th> <th scope="col"> 83mb </th> </tr> </thead> <tbody> <tr> <td> gzip (fast) </td> <td> 2.19 </td> <td> 8.27 </td> <td> 19.94 </td> </tr> <tr> <td> gzip (best) </td> <td> 1.62 </td> <td> 6.65 </td> <td> 17.36 </td> </tr> <tr> <td> bzip2 (fast) </td> <td> 1.72 </td> <td> 6.36 </td> <td> 16.93 </td> </tr> <tr> <td> bzip2 (best) </td> <td> 1.17 </td> <td> 5.14 </td> <td> 13.07 </td> </tr> <tr> <td> rzip (fast) </td> <td> 0.95 </td> <td> 5.01 </td> <td> 12.00 </td> </tr> <tr> <td> rzip (best) </td> <td> 0.89 </td> <td> 4.65 </td> <td> 10.39 </td> </tr> <tr> <td> 7zip </td> <td> 0.82 </td> <td> 4.34 </td> <td> 9.09 </td> </tr> </tbody> </table> <table class="tochart typebvg size700x250"> <caption> Compression comparison - time in seconds<br /> </caption> <thead> <tr> <th> <p> &nbsp; </p> </th> <th scope="col"> 16mb </th> <th scope="col"> 48mb </th> <th scope="col"> 83mb </th> </tr> </thead> <tbody> <tr> <td> gzip (fast) </td> <td> 0.309 </td> <td> 1.020 </td> <td> 2.072 </td> </tr> <tr> <td> gzip (best) </td> <td> 1.356 </td> <td> 4.873 </td> <td> 7.343 </td> </tr> <tr> <td> bzip2 (fast) </td> <td> 5.141 </td> <td> 14.918 </td> <td> 25.858 </td> </tr> <tr> <td> bzip2 (best) </td> <td> 7.732 </td> <td> 22.026 </td> <td> 44.834 </td> </tr> <tr> <td> rzip (fast) </td> <td> 2.400 </td> <td> 5.931 </td> <td> 11.582 </td> </tr> <tr> <td> rzip (best) </td> <td> 2.476 </td> <td> 34.277 </td> <td> 52.385 </td> </tr> <tr> <td> 7zip </td> <td> 4.284 </td> <td> 15.097 </td> <td> 25.325 </td> </tr> </tbody> </table> <p> As I'm mainly interested in small compressed files, and don't care about performance or resource usage so much, my conclusion is that 7zip is the best fit for my needs. It's the best of the bunch in terms of producing small files, but still outperforms many of the others in terms of how fast it compresses the SQL files. </p> <p>7zip also has other advantages; the <em>cross platform port</em> <a href="http://en.wikipedia.org/wiki/P7zip" rel="nofollow">p7zip</a> works great on most linux distros (and Mac OSX, I believe), and the original 7zip application is a windows app - so I can have my backup scripts send copies of database dumps to website owners and be confident they can access the dumps if the need ever arises. (p)7zip can also be pipe-lined (that is, it can take input from standard in and send its output to standard out), so it's pretty flexible in how it can be used in scripts. </p> <p> A few reflections on the other methods / apps I tested; if performance is more important to you than it is to me, then rzip (at less than <em>best</em> compression ratio) may be worth a look - on its fast setting it did pretty well at making smaller files than gzip or bzip2 could and was not much slower than gzip (and faster than bzip2). Where 7zip does well with cross-platform availability and pipe-lining, however, rzip's not so good. </p> <p> I should point out that my conclusions here are based on my own criteria (I want small compressed files, and don't care so much what it takes to get them). If your priorities are different, my conclusion may not work for you. In particular my use of wall time to measure the performance of these compression methods is very woolly. For example, I made no measurement of memory consumption, and this could be a significant differentiator if it's a factor that matters to you. </p> <p> <br /><em><br /> P.S. this post uses a new drupal module I put together which allows you to easily generate google charts from HTML tables. I plan to document this (and possibly release the module) soon. It's based on work by <a href="http://www.rsc-ne-scotland.org.uk/mashe/2008/06/generating-charts-from-accessible-data-tables-using-the-google-charts-api" rel="nofollow">Martin Hawksey</a> which was based on work by <a href="http://www.wait-till-i.com/2008/01/08/generating-charts-from-accessible-data-tables-using-the-google-charts-api" rel="nofollow">Chris Heilmann</a>.</em> </p> 7zip backups compression database drupal drupal-planet linux mysql server sysadmin Wed, 14 Apr 2010 19:11:20 +0000 drew 21 at http://mcdruid.co.uk.php53