Class: Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/backup.rb

Overview

A class that has individual backups as instances.

Instance Method Summary collapse

Constructor Details

#initialize(path, rsync_arguments, verbose) ⇒ Backup

Initializes the a backup, takes as arguments a Configfile, some rsync_arguments and the information if it should be verbose.



10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/backup.rb', line 10

def initialize(path, rsync_arguments, verbose)
  @rsync_arguments = rsync_arguments
  @verbose = verbose
  @logger = nil
  @configfile = nil
  
  begin 
    @configfile = ConfigFile.new(path)

    logfile = @configfile.logfile
    if logfile == ""
      if @configfile.logdir == ""
        logfile = "/tmp/" + Time.now.strftime("%Y-%m-%d.%Hh%M")
      else
        logfile = @configfile.logdir + "/" + Time.now.strftime("%Y-%m-%d.%Hh%M")+"-"+@configfile.filename.to_s+".log"
      end
    else
      logfile = @configfile.logfile
    end

    @logger = Log.new(logfile, @configfile.advancedlog, @configfile.syslog)

    @configfile.write
    
    @logger.p("-" * 20, 98)
    @logger.p("Working on " + @configfile.filename, 2)
    @logger.p("-" * 20, 98)
    @logger.p("\nChecking configfile...")
    
    if @configfile.sendmail
      @logger.initialize_email(@configfile.email, @configfile.smtp,
        @configfile.port, @configfile.uname, @configfile.passwd,
        @configfile.tls, @configfile.testm, path.split("/")[-1])
      if @configfile.testm
        @logger.p("The test e-mail has been successfully sent to " + @configfile.email + ",", 2, 1)
        @configfile.testm=false
        @configfile.write
        @logger.p("testm has been automatically set to 'no'.", 2, 1)            
      end  
    end
    
    raise "No 'rsync' installed!" unless command_exists?("rsync")
    raise "No 'du' installed!" unless command_exists?("du")      
    raise "No 'df' installed!" unless command_exists?("df")
    readcf
    
    if not @configfile.execute_before.empty? then
      output = `#{@configfile.execute_before} 2>&1`
      raise "execute_before command '" + @configfile.execute_before + "' was not successful, aborting!\nThis was the output generated:\n" + output unless $?.exitstatus == 0
    end
    
    @backup_dir = SmarbsBackupDir.new(@configfile.dest + "/" + "smarbsbackup", @configfile.ignore_leftovers)
    
    backup
    
    if not @configfile.execute_after.empty? then
      output = `#{@configfile.execute_after} 2>&1`
      raise(AfterException, "execute_after command '" + @configfile.execute_after + "' was not successful, aborting!\nThis was the output generated:\n" + output) unless $?.exitstatus == 0
    end
  
  rescue AfterException
    message =  $!.to_s + "\nCheck your configfile " + path + "."
    if @logger.nil?
      puts "Error: " + message
    else
      @logger.p(message, 4)
    end
  rescue NoConfigException
    @configfile.execute_after = "" if @configfile.execute_after.nil?
    message =  $!.to_s
    if not @configfile.execute_after.empty? then
      output = `#{@configfile.execute_after} 2>&1`
      if $?.exitstatus != 0 then
        message = "execute_after command '" + @configfile.execute_after + "' was not successful, aborting!\nThis was the output generated:\n" + output + "\nAlso, your backup was aborted: " + message
      end
    end
    if @logger.nil?
      puts "Error: " + message
    else
      @logger.p(message, 4)
    end
  rescue
    message =  $!.to_s + "\nCheck your configfile " + path + "."
    @configfile.execute_after = "" if @configfile.execute_after.nil? unless @configfile.nil?
    if not @configfile.execute_after.empty? then
      output = `#{@configfile.execute_after} 2>&1`
      if $?.exitstatus != 0 then
        message = "execute_after command '" + @configfile.execute_after + "' was not successful, aborting!\nThis was the output generated:\n" + output + "\nAlso, your backup was aborted: " + message
      end
    end unless @configfile.nil?
    if @logger.nil?
      puts "Error: " + message
    else
      @logger.p(message, 4)
    end
  end
end