svn-backup2.php

Bruno Prado, 2010-01-12 17:02

Download (1.19 KB)

 
1
#!/usr/bin/php5 -q
2

    
3
<?php
4

    
5
# Requires PHP5 or the file_put_contents() function from PEAR::Compat
6

    
7
# this is a directory with my svn repositories in it
8
  $source_dir = "/var/svn";
9
# this is where the svn dumps will go
10
  $dest_dir = "/mnt/server/DS/SVN";
11
# we only back up repositories which have changed
12
  $hashfile = "/tmp/hash/hashes.txt";
13

    
14
  $date = system("date +%Y-%m-%d");
15

    
16
# read in the existing hash file if there is one
17
  $hashes = array();
18
  $hashlines = array();
19
  if (file_exists($hashfile)) {
20
    $hashlines = file($hashfile);                        
21
  }
22

    
23
# build this into an useful array
24
  foreach($hashlines as $hashline) {
25
    $key = substr($hashline,0,strpos($hashline,":"));
26
    $value = substr($hashline,strpos($hashline,":")+1);
27
    if ($key && $value) {
28
      $hashes[$key] = $value;                        
29
    }
30
  }
31

    
32
# Compress repositories folder
33
  system("tar -cf $dest_dir/$date.tar $source_dir/*");
34

    
35
# Remove oldest backup
36
#  system("find $dest_dir -amin +6000 -exec rm '{}'");
37
  system("find $dest_dir -amin +6000 -exec rm '{}' \ ");
38

    
39
  $hashelines = "";
40
  foreach($hashes as $key => $md5) {
41
    $hashelines .= "$key:$md5\n";
42
  }
43

    
44
  file_put_contents($hashfile, $hashelines);
45

    
46
?>
47