Class: AnjeaBackup::Backup

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/anjea_backup/anjea_backup.rb

Instance Method Summary collapse

Methods included from Logger

#log, #now_with_hour, #now_with_minutes

Constructor Details

#initializeBackup

Returns a new instance of Backup.



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#backupObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#cleanupObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_vaultObject



76
77
# File 'lib/anjea_backup/anjea_backup.rb', line 76

def to_vault
end