Tuesday, May 11th, 2010 / von Schorsch

Easy Subversion Repository Backup with Ruby

Ever had the problem to backup or dump a bunch of Subversion repositories? Here you have got a small ruby snippet which can be executed on your server.

Save this code in a ruby file e.g. backup.rb and make the changes on top of the script: adopt your subversion path, backup path and of course add you repository names(folder names).

require 'fileutils'
require 'date'

# set your repositories names here devided by space
svns = %w(my-repo-name1 my-repo-name2)

# Set your subversion repos base dir
svn_base = '/var/svn'
# set your backfolder here
backup_base = '/var/backups'

# remove existing backup dir
backup_dir = File.join(backup_base, 'svn')
FileUtils.rm_r(backup_dir)
FileUtils.mkdir(backup_dir)

# prefix each file with the current datetime
file_prefix = DateTime.now.strftime('%y%m%d%H%M')

svns.each do |svn|
  # dump each svn repo to the given backup dir
  svn_dir = File.join(svn_base, svn)
  backup_file = File.join(backup_dir, "#{file_prefix}_#{svn}.dump")
  # provide some status output
  puts "Starting dump of #{svn} to #{backup_file}"
  # go for it
  %x[svnadmin dump #{svn_dir}/ > #{backup_file} --quiet]
  puts "Finished dumping #{svn}"
end

# tar everything into one big backup file
tar_file = File.join(backup_base,"#{file_prefix}_svns.tar.gz")
puts "Compressing all dumps to # {tar_file}"
%x[tar cfz #{tar_file} #{backup_dir}]
puts "Thats it you can now move your backup to a save place"

Now run it with your ruby interpreter:

~$ ruby backup.rb

Tags:

,

Meta

Comments are closed.