Super-simple Ruby Subversion Command Line Wrapper

Like Dan did recently, I was setting up a Subversion repository one year ago. Of course, I also wanted to have regular backups of my Subversion repository. As I was tired of writing bash scripts for such a task, I looked into writing a simple Ruby script for backing up my Subversion repositories.

Full and Incremental Backups


One of my main requirements was that I needed to do incremental backups. One of our Subversion repositories contains a couple of gigabytes of pictures. This is simply too much to backup every day.

In addition to backing up my data, I wanted an easy way to restore a Subversion repository from the backup. All those commands aren’t very complicated yet I wanted to use pure Ruby within my backup script.

Ruby-Subversion Bindings

As described in Using the Subversion Ruby Bindings there are official Ruby-Subversion bindings. They’re huge and for my simple Subversion backup script simply overkill to learn. Therefore, I wrote my own super-simple Ruby-Subversion command line wrapper.

Ruby Subversion Command Line Wrapper

This is nearly too primitive to share, but maybe you can make use of it:

require 'yaml'

class Svn
  
    attr :repository_base
    attr :svnadmin
    attr :svn
  
    def initialize(repository_base = "/var/svn")
      @repository_base = repository_base
      @svn = `which svn`.strip
      @svnadmin = `which svnadmin`.strip
    end
  
    def commit(working_copy, message)
      `cd #{working_copy} && #{@svn} commit -m "#{message}"`
    end
  
    def add(working_copy, file)
      `cd #{working_copy} && #{@svn} add #{file}`
    end
  
    def update(working_copy)
      `cd #{working_copy} && #{@svn} up`
    end
  
    def dump(repository, dump_file_name)
      `#{@svnadmin} dump #{@repository_base}/#{repository} > #{dump_file_name}`
      $?.success?
    end
  
    def incremental_dump(repository, dump_file_name, from, to)
      `#{@svnadmin} dump #{@repository_base}/#{repository} --incremental -r#{from}:#{to} > #{dump_file_name}`
      $?.success?
    end
  
    def load(repository, dump_file_name)
      `#{@svnadmin} load #{@repository_base}/#{repository} < #{dump_file_name}`
      $?.success?
    end
  
    def checkout(repository, working_copy_path)
      result = `cd #{working_copy_path} && #{@svn} co file:///#{@repository_base}/#{repository}`
      result[/d+/]
    end
  
    def create(repository)
      `#{@svnadmin} create #{@repository_base}/#{repository}`
      $?.success?
    end
  
    def info(working_copy)
      result = `#{@svn} info #{working_copy}`
      yaml = YAML.load(result)
      yaml['Revision'].to_s
    end
  end

Putting this code into a file called svn.rb and requiring it from your Ruby script you should be able to

  • create a new Subversion repository
  • add and commit new files to it
  • checkout or update a working copy
  • dump your repository to a file (which you can then e.g. push to Amazon S3 or your own FTP server
  • restore your Subversion repository from a dump

Good luck and have fun!

2 thoughts on “Super-simple Ruby Subversion Command Line Wrapper

  1. Thanks, this has been really useful to me , specially when I was finding lack of time for building SVN bindings and then look for something a readymade SVN commandline wrapper in ruby… sounds like it can it make its way to ruby gems.. why not?!

    Thanks again! 🙂

    Regards,
    Nikhil

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.