<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -55,7 +55,7 @@ To disable this feature, comment out the following line from backup.inc.php:
     
 ### Hourly backups
 
-You can optionally run the backups every hour. For now it will store the full 24 backup sets per day as a group according to the above schedule. In the future I want to remove a lot of the hourly backups to save space.
+You can optionally run the backups every hour. Hourly backups will be available for the last 72 hours. Beyond that, Midnight (00) hour backups will be available according to the backup deletion schedule outlined above.
     
 ### Requirements
 
@@ -72,5 +72,4 @@ My name is [Ian](http://www.ianneubert.com/). I wrote this small program to help
 ## ToDo
 
 * Add configuration documentation
-* Comment code
-* Delete hourly backups after X days
\ No newline at end of file
+* Comment code
\ No newline at end of file</diff>
      <filename>README.markdown</filename>
    </modified>
    <modified>
      <diff>@@ -3,6 +3,7 @@
 define('awsAccessKey', '');
 define('awsSecretKey', '');
 define('awsBucket', '');
+define('debug',false);
 
 //Pass these options to mysqldump
 define('mysqlDumpOptions', '--quote-names --quick --add-drop-table --add-locks --allow-keywords --disable-keys --extended-insert --single-transaction --create-options --comments --net_buffer_length=16384');</diff>
      <filename>backup.dist.php</filename>
    </modified>
    <modified>
      <diff>@@ -9,7 +9,7 @@ require_once('S3.php');
 $s3 = new S3(awsAccessKey, awsSecretKey);
 
 //delete old backups
-deleteBackups(awsBucket);
+deleteBackups();
 
 //Setup variables
 $mysql_backup_options = mysqlDumpOptions;
@@ -19,7 +19,9 @@ $mysql_backup_options = mysqlDumpOptions;
 // Backup files and compress for storage
 function backupFiles($targets, $prefix = '') {
 	global $s3;
-		
+	
+	if (hourly) deleteHourlyBackups($prefix);
+	
 	foreach ($targets as $target) {
 		// compress local files
 		$cleanTarget = urlencode($target);
@@ -37,6 +39,8 @@ function backupFiles($targets, $prefix = '') {
 function backupDBs($hostname, $username, $password, $prefix, $post_backup_query = '') {
 	global $DATE, $s3, $mysql_backup_options;
 	
+	if (hourly) deleteHourlyBackups($prefix);
+	
 	// Connecting, selecting database
 	$link = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error());
 	
@@ -74,7 +78,21 @@ function backupDBs($hostname, $username, $password, $prefix, $post_backup_query
 	
 }
 
-function deleteBackups($bucket) {
+function deleteHourlyBackups($target_prefix) {
+  global $s3;
+  
+  //delete hourly backups, 72 hours before now, except the midnight (00) backup
+	$set_date = strtotime('-72 hours');
+	if (hourly) {
+	  for ($i = 1; $i &lt;= 23; $i++) {
+  	  $prefix = s3Path('','',$set_date,true).$target_prefix.&quot;/&quot;.str_pad((string)$i,2,&quot;0&quot;,STR_PAD_LEFT).&quot;/&quot;;
+  	  if (debug) echo $prefix.&quot;\n&quot;;
+  	  deletePrefix($prefix);
+	  }
+	}
+}
+
+function deleteBackups() {
 	global $s3;
 	
 	//delete the backup from 2 months ago
@@ -84,46 +102,42 @@ function deleteBackups($bucket) {
 	if ((int)date('j',$set_date) === 1) return true;
 	
 	//set s3 &quot;dir&quot; to delete
-	$prefix = s3Path('','',$set_date);
+	$prefix = s3Path('','',$set_date,false);
 	
-	//find files to delete
-	$keys = $s3-&gt;getBucket($bucket,$prefix);
-
-	//delete each key found
-	foreach ($keys as $key =&gt; $meta) {
-		$s3-&gt;deleteObject($bucket,$key);
-	}
+	if (debug) echo $prefix.&quot;\n&quot;;
 	
-	//echo $prefix.&quot;\n&quot;;
+	//delete each key found
+	deletePrefix($prefix);
 	
 	//delete the backup from 2 weeks ago
 	$set_date = strtotime('-2 weeks');
 	
 	//only if it wasn't a saturday or the 1st
 	if ((int)date('j',$set_date) === 1 || (string)date('l',$set_date) === &quot;Saturday&quot;) return true;
-	
-	//set s3 &quot;dir&quot; to delete
-	$prefix = s3Path('','',$set_date);
-	
-	//find files to delete
-	$keys = $s3-&gt;getBucket($bucket,$prefix);
+  $prefix = s3Path('','',$set_date,false);
+  if (debug) echo $prefix.&quot;\n&quot;;
+  
+	deletePrefix($prefix);
+}
 
-	//delete each key found
-	foreach ($keys as $key =&gt; $meta) {
-		$s3-&gt;deleteObject($bucket,$key);
+function deletePrefix($prefix) {
+  global $s3;
+  
+  //find files to delete
+	$keys = $s3-&gt;getBucket(awsBucket,$prefix);
+  
+  foreach ($keys as $key =&gt; $meta) {
+    if (debug) echo $key.&quot;\n&quot;;
+		$s3-&gt;deleteObject(awsBucket,$key);
 	}
-	
-	//debug
-	//print_r($keys);
-	//echo $prefix.&quot;\n&quot;;
 }
 
-function s3Path($prefix, $name, $timestamp = null) {
+function s3Path($prefix, $name, $timestamp = null, $force_hourly = null) {
   if (is_null($timestamp)) $timestamp = time();
   
   $date = date(&quot;Y/m/d/&quot;,$timestamp);
   
-  if (hourly) {
+  if (is_null($force_hourly) &amp;&amp; hourly) {
     return &quot;backups/&quot;.$date.$prefix.'/'.date('H',$timestamp).$name;
   } else{
     return &quot;backups/&quot;.$date.$prefix.$name;</diff>
      <filename>include/backup.inc.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>de9fecae408ebfd355ac9316be0f5ff63f8074b5</id>
    </parent>
  </parents>
  <author>
    <name>Ian Neubert</name>
    <email>ian@ianneubert.com</email>
  </author>
  <url>http://github.com/ianneub/php_backup_s3/commit/debbd48fd0097fea1b1b25b6cb6cf72eb7cb8936</url>
  <id>debbd48fd0097fea1b1b25b6cb6cf72eb7cb8936</id>
  <committed-date>2009-07-22T09:20:00-07:00</committed-date>
  <authored-date>2009-07-22T09:20:00-07:00</authored-date>
  <message>Hourly backups between 0100-2300 hours will now be deleted after 72 hours.
Hourly backups now correctly delete entire old backups according to the standard backup retention schedule.
Added new debug parameter to enable verbose output.
Updated README to reflect changes.</message>
  <tree>3323866917339f9cc16371847921d612661ac0d8</tree>
  <committer>
    <name>Ian Neubert</name>
    <email>ian@ianneubert.com</email>
  </committer>
</commit>
