| 
      1
     | 
    
      #!/usr/bin/php5 -q
 
     | 
  
  
    | 
      2
     | 
    
      
 
     | 
  
  
    | 
      3
     | 
    
      <?php
 
     | 
  
  
    | 
      4
     | 
    
      
 
     | 
  
  
    | 
      5
     | 
    
      // Requires PHP5 or the file_put_contents() function from PEAR::Compat
 
     | 
  
  
    | 
      6
     | 
    
      
 
     | 
  
  
    | 
      7
     | 
    
      $source_dir = "/var/svn";	   // this is a directory with my svn repositories in it
 
     | 
  
  
    | 
      8
     | 
    
      $dest_dir = "/mnt/server/DS/SVN"; // this is where the svn dumps will go
 
     | 
  
  
    | 
      9
     | 
    
      $tmp_dir = "/tmp";		   // temp dir
 
     | 
  
  
    | 
      10
     | 
    
      $hashfile = "/tmp/hash/hashes.txt";// we only back up repositories which have changed
 
     | 
  
  
    | 
      11
     | 
    
      $date = system("date +%Y-%m-%d");
     | 
  
  
    | 
      12
     | 
    
      
 
     | 
  
  
    | 
      13
     | 
    
      // read in the existing hash file if there is one
 
     | 
  
  
    | 
      14
     | 
    
      $hashes = array();
 
     | 
  
  
    | 
      15
     | 
    
      $hashlines = array();
 
     | 
  
  
    | 
      16
     | 
    
      if (file_exists($hashfile)) {
     | 
  
  
    | 
      17
     | 
    
        $hashlines = file($hashfile);			
 
     | 
  
  
    | 
      18
     | 
    
      }
 
     | 
  
  
    | 
      19
     | 
    
      
 
     | 
  
  
    | 
      20
     | 
    
      // build this into an useful array
 
     | 
  
  
    | 
      21
     | 
    
      foreach($hashlines as $hashline) {
     | 
  
  
    | 
      22
     | 
    
        $key = substr($hashline,0,strpos($hashline,":"));
 
     | 
  
  
    | 
      23
     | 
    
        $value = substr($hashline,strpos($hashline,":")+1);
 
     | 
  
  
    | 
      24
     | 
    
        if ($key && $value) {
     | 
  
  
    | 
      25
     | 
    
          $hashes[$key] = $value;			
 
     | 
  
  
    | 
      26
     | 
    
        }
 
     | 
  
  
    | 
      27
     | 
    
      }
 
     | 
  
  
    | 
      28
     | 
    
      
 
     | 
  
  
    | 
      29
     | 
    
      system("mkdir $dest_dir/backup");
     | 
  
  
    | 
      30
     | 
    
      system("chmod 755 $dest_dir/backup");
     | 
  
  
    | 
      31
     | 
    
      
 
     | 
  
  
    | 
      32
     | 
    
      // loop through the repositories in the source directory
 
     | 
  
  
    | 
      33
     | 
    
      if ($handle = opendir($source_dir)) {
     | 
  
  
    | 
      34
     | 
    
        while (false !== ($file = readdir($handle))) {
     | 
  
  
    | 
      35
     | 
    
          if (is_dir($source_dir."/".$file) && $file != "." && $file != "..") {
     | 
  
  
    | 
      36
     | 
    
            // generate a md5 hash for the contents of the repository
 
     | 
  
  
    | 
      37
     | 
    
            $command = "svnlook info '$source_dir/$file'";
 
     | 
  
  
    | 
      38
     | 
    
            // echo $command."\n";
 
     | 
  
  
    | 
      39
     | 
    
            exec($command,$output);
 
     | 
  
  
    | 
      40
     | 
    
            $md5 = trim(md5(trim(str_replace("\n","-",implode("\n",$output)))));
     | 
  
  
    | 
      41
     | 
    
            if (!isset($hashes[$file]) || $hashes[$file] !== $md5) {
     | 
  
  
    | 
      42
     | 
    
              // echo "dumping $file\n";
 
     | 
  
  
    | 
      43
     | 
    
              $return = system("mkdir -p $tmp_dir/svn_backup/$file");
     | 
  
  
    | 
      44
     | 
    
              $return = system("svnadmin hotcopy $source_dir/$file $tmp_dir/svn_backup/$file");
     | 
  
  
    | 
      45
     | 
    
              $return = system("svnadmin dump -q $tmp_dir/svn_backup/$file | bzip2 -c > $dest_dir/backup/$file.bz2");
     | 
  
  
    | 
      46
     | 
    
              $return = system("rm -Rf $tmp_dir/svn_backup/$file");
     | 
  
  
    | 
      47
     | 
    
              $hashes[$file] = $md5;
 
     | 
  
  
    | 
      48
     | 
    
            }
 
     | 
  
  
    | 
      49
     | 
    
          }
 
     | 
  
  
    | 
      50
     | 
    
        }
 
     | 
  
  
    | 
      51
     | 
    
        //Compress repositories dumped
 
     | 
  
  
    | 
      52
     | 
    
        system("tar -cf $dest_dir/$date.tar $dest_dir/backup");
     | 
  
  
    | 
      53
     | 
    
        system("rm -r $dest_dir/backup*");
     | 
  
  
    | 
      54
     | 
    
      
 
     | 
  
  
    | 
      55
     | 
    
        //Remove oldest backup
 
     | 
  
  
    | 
      56
     | 
    
        system("find $dest_dir -amin +6000 -exec rm '{}'");
     | 
  
  
    | 
      57
     | 
    
      
 
     | 
  
  
    | 
      58
     | 
    
       closedir($handle);
 
     | 
  
  
    | 
      59
     | 
    
      }
 
     | 
  
  
    | 
      60
     | 
    
      
 
     | 
  
  
    | 
      61
     | 
    
      $hashelines = "";
 
     | 
  
  
    | 
      62
     | 
    
      foreach($hashes as $key => $md5) {
     | 
  
  
    | 
      63
     | 
    
        $hashelines .= "$key:$md5\n";
 
     | 
  
  
    | 
      64
     | 
    
      }
 
     | 
  
  
    | 
      65
     | 
    
      
 
     | 
  
  
    | 
      66
     | 
    
      file_put_contents($hashfile, $hashelines);
 
     | 
  
  
    | 
      67
     | 
    
      
 
     | 
  
  
    | 
      68
     | 
    
      ?>
 
     |