class AnjeaBackup::Backup

Public Class Methods

new() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 10
def initialize
  read_system_conf
  setup_dirs
  if !lock!
    log_err "Aborting, anjea already running.  Delete #{@lock_file} if not."
    exit 2
  end
  read_backups_conf
end

Public Instance Methods

backup() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 20
def backup
  yyyymmdd = now_with_hour

  # TODO work in a tmp work dir
  @backup_items.each do |item|
    last_backup  = File.join(@last, item.name)
    today_backup = create_now_backup_path(yyyymmdd, item)
    work_dir     = File.join(@partial, item.name, yyyymmdd)
    FileUtils.mkdir_p work_dir
  
    source = item.ssh_url ? "-e \"ssh -i #{item.ssh_key}\" #{item.ssh_url}"
                          : item.src_dir
  
    rsync_cmd = "rsync -avz "\
      "--delete --relative --stats "\
      "--log-file #{log_file_for(yyyymmdd, item)} "\
      "--link-dest #{last_backup} "\
      "#{source} #{today_backup}"
  
    log item, "rsync start"
    if system(rsync_cmd)
      log item, "rsync finished"
      # 'finish', move partial to backup-dest
      link_last_backup today_backup, last_backup
      log item, "linked"
    else
      # TODO Move this one into quarantaine/incomplete!
      log_err item, "rsync failed?"
    end
  end
  self
end
cleanup() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 53
def cleanup
  now = DateTime.now
  @backup_items.each do |item|
    puts "[#{item.name}] backups:"
    puts "-- #{item.description} --"
    ages = Dir.glob("#{@destination}/[^c]*/#{item.name}").map do |dir|
      date_dir = Pathname.new(dir).parent.basename.to_s
      dtdiff = 0
      begin
        stamp = DateTime.strptime(date_dir, "%Y-%m-%d-%H")
        dtdiff = now - stamp
      rescue
        STDERR.puts "Do not understand timestamp in #{dir}"
      end
      [dtdiff, dir]
    end
    ages.sort.each do |age,dir|
      puts "(#{(age*24).to_i}) #{dir}"
    end
    puts
  end
end
to_vault() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 76
def to_vault
end

Private Instance Methods

create_now_backup_path(yyyymmdd, item) click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 127
def create_now_backup_path yyyymmdd, item
  today_backup = File.join(@destination, yyyymmdd, item.name)
  FileUtils.mkdir_p today_backup
  today_backup
end
lock!() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 96
def lock!
  File.new(@lock_file, 'w').flock(File::LOCK_NB | File::LOCK_EX)
end
log_err(item=nil, msg) click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 90
def log_err item=nil, msg
  log_msg = (!item.nil?) ? "[#{item.name}] #{now_with_minutes} - #{msg}"
    : "#{now_with_minutes} - #{msg}"
  STDERR.puts log_msg
end
log_file_for(yyyymmdd, item) click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 122
def log_file_for yyyymmdd, item
  FileUtils.mkdir_p File.join(@log, item.name)
  File.join(@log, item.name, "#{yyyymmdd}.log")
end
read_backups_conf() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 105
def read_backups_conf
  @backup_items = read_ini_file('backups.conf').map {|group| BackupItem.new group }
end
read_system_conf() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 109
def read_system_conf
  system_conf = read_ini_file 'anjea.conf'
  
  @destination = system_conf[0]['dst']
  @vault       = system_conf[0]['vault']
  @log         = system_conf[0]['log']
  @last        = File.join(@destination, 'current')
  @failed      = File.join(@destination, 'failed')
  @partial     = File.join(@destination, 'partial')
  @lock_file   = system_conf[0]['lock']
  # rescue from malformed config
end
setup_dirs() click to toggle source
# File lib/anjea_backup/anjea_backup.rb, line 81
def setup_dirs
  Dir.mkdir @destination if !File.directory? @destination
  Dir.mkdir @vault       if !File.directory? @vault
  Dir.mkdir @log         if !File.directory? @log
  Dir.mkdir @last        if !File.directory? @last
  Dir.mkdir @partial     if !File.directory? @partial
  Dir.mkdir @failed      if !File.directory? @failed
end