This is the script I use to copy my Subversion repositories to a backup server. Each repository is checked for changes before doing the export.
SVN Backup Script
How to Use
- Download the script below
- Make the script execuatable: chmod +x svn-backup.php
- Set the 4 configuration variables at the top: $source_dir, $dest_dir, $tmp_dir & $hashfile
- Run the script: ./svn-backup.php
Download
svn-backup.php.zip (1 KB)
#!/usr/bin/php5 -q
<?php
// Requires PHP5 or the file_put_contents() function from PEAR::Compat
$source_dir = "/usr/local/svn"; // this is a directory with my svn repositories in it
$dest_dir = "/mnt/backup/svn"; // this is where the svn dumps will go
$tmp_dir = "/usr/local/tmp"; // temp dir
$hashfile = "/mnt/backup/svn/hashes.txt"; // we only back up repositories which have changed
// read in the existing hash file if there is one
$hashes = array();
$hashlines = array();
if (file_exists($hashfile)) {
$hashlines = file($hashfile);
}
// build this into a useful array
foreach($hashlines as $hashline) {
$key = substr($hashline,0,strpos($hashline,":"));
$value = substr($hashline,strpos($hashline,":")+1);
if ($key && $value) {
$hashes[$key] = $value;
}
}
// loop thorugh the repositories in the source directory
if ($handle = opendir($source_dir)) {
while (false !== ($file = readdir($handle))) {
if (is_dir($source_dir."/".$file) && $file != "." && $file != "..") {
// generate an md5 hash for the contents of the repository
$command = "svnlook info '$source_dir/$file'";
// echo $command."\n";
exec($command,$output);
$md5 = trim(md5(trim(str_replace("\n","-",implode("\n",$output)))));
if (!isset($hashes[$file]) || $hashes[$file] !== $md5) {
// echo "dumping $file\n";
$return = system("mkdir -p $tmp_dir/svn_backup/$file");
$return = system("svnadmin hotcopy $source_dir/$file $tmp_dir/svn_backup/$file");
$return = system("svnadmin dump -q $tmp_dir/svn_backup/$file | bzip2 -c > $dest_dir/$file.bz2");
$return = system("rm -Rf $tmp_dir/svn_backup/$file");
$hashes[$file] = $md5;
}
}
}
closedir($handle);
}
$hashelines = "";
foreach($hashes as $key => $md5) {
$hashelines .= "$key:$md5\n";
}
file_put_contents($hashfile,$hashelines);
?>
