#!/usr/bin/php5 -q

<?php

# Requires PHP5 or the file_put_contents() function from PEAR::Compat

# this is a directory with my svn repositories in it
  $source_dir = "/var/svn";
# this is where the svn dumps will go
  $dest_dir = "/mnt/server/DS/SVN";
# we only back up repositories which have changed
  $hashfile = "/tmp/hash/hashes.txt";

  $date = system("date +%Y-%m-%d");

# read in the existing hash file if there is one
  $hashes = array();
  $hashlines = array();
  if (file_exists($hashfile)) {
    $hashlines = file($hashfile);			
  }

# build this into an useful array
  foreach($hashlines as $hashline) {
    $key = substr($hashline,0,strpos($hashline,":"));
    $value = substr($hashline,strpos($hashline,":")+1);
    if ($key && $value) {
      $hashes[$key] = $value;			
    }
  }

# Compress repositories folder
  system("tar -cf $dest_dir/$date.tar $source_dir/*");

# Remove oldest backup
#  system("find $dest_dir -amin +6000 -exec rm '{}'");
  system("find $dest_dir -amin +6000 -exec rm '{}' \ ");

  $hashelines = "";
  foreach($hashes as $key => $md5) {
    $hashelines .= "$key:$md5\n";
  }

  file_put_contents($hashfile, $hashelines);

?>

