Class: Keepitsafe

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BackupPlugins::Remove

#keep_all, #keep_first_each_day, #keep_first_each_month, #keep_first_each_week, #remove, #remove_old_backups

Methods included from BackupPlugins::Scp

#scp

Methods included from BackupPlugins::Files

#files

Methods included from BackupPlugins::Rsync

#rsync

Methods included from BackupPlugins::Tar

#tar, #tar_gz, #tar_gz_path, #tar_path

Methods included from BackupPlugins::Postgres

#all_postgres

Methods included from BackupPlugins::Mongo

#all_mongo

Methods included from Events

#events, included, #method_missing, #register_handler, #respond_to?, #trigger

Constructor Details

#initialize(server_domain = "localhost", options = {}, &block) ⇒ Keepitsafe

Returns a new instance of Keepitsafe.



31
32
33
34
35
36
37
38
39
40
# File 'lib/keepitsafe.rb', line 31

def initialize server_domain = "localhost", options = {}, &block
  
  @options = options
  @options[:username] ||= Etc.getlogin
  @options[:forward_agent] ||= true
  
  @server_domain = server_domain
  
  run &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Events

Instance Attribute Details

#backup_sizeObject

Returns the value of attribute backup_size.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def backup_size
  @backup_size
end

#end_timeObject

Returns the value of attribute end_time.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def end_time
  @end_time
end

#errorObject

Returns the value of attribute error.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def error
  @error
end

#free_afterObject

Returns the value of attribute free_after.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def free_after
  @free_after
end

#free_beforeObject

Returns the value of attribute free_before.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def free_before
  @free_before
end

#log_bufferObject

Returns the value of attribute log_buffer.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def log_buffer
  @log_buffer
end

#start_timeObject

Returns the value of attribute start_time.



171
172
173
# File 'lib/keepitsafe.rb', line 171

def start_time
  @start_time
end

Instance Method Details

#backup_target_dirObject



142
143
144
# File 'lib/keepitsafe.rb', line 142

def backup_target_dir
  "~/backups/#{@server_domain}/#{@backup_timastamp}/"
end

#check_disk_left(limit = 1000) ⇒ Object



158
159
160
161
162
163
# File 'lib/keepitsafe.rb', line 158

def check_disk_left limit = 1000
  
  free = free_disk_space
  
  raise "root disk limit reached limit:#{limit} disk_free_on_root:#{free}" if free < limit
end

#create_backup_target_dirObject



146
147
148
# File 'lib/keepitsafe.rb', line 146

def create_backup_target_dir
   puts run_cmd "mkdir -p #{backup_target_dir}"
end

#create_pending_fileObject



150
151
152
# File 'lib/keepitsafe.rb', line 150

def create_pending_file
  puts run_cmd "touch #{backup_target_dir}/pending"
end

#domainObject



42
43
44
# File 'lib/keepitsafe.rb', line 42

def domain 
  @server_domain
end

#download(src, target, create_target_dir = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/keepitsafe.rb', line 99

def download src,target,create_target_dir = false
  src = run_cmd("echo #{src}").strip # expand "~/"
  target = `echo #{target}`.strip # expand "~/"
  puts "remote:#{src} => local:#{target}"
  
  `mkdir -p #{target.end_with?("/") ? target : File.dirname(target)}` if create_target_dir
  
  if on_localhost?
    `cp -a #{src} #{target}`
  else
    @ssh.scp.download!(src,target)
  end
end

#download_backupObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/keepitsafe.rb', line 82

def download_backup
  
  path = if !run_cmd("ls #{tar_path}").match(/No such file or directory/)
     tar_path
   elsif !run_cmd("ls #{tar_gz_path}").match(/No such file or directory/)
     tar_gz_path
   elsif !run_cmd("ls #{backup_target_dir}").match(/No such file or directory/)
     backup_target_dir
   else  
     raise "This code sucks as we cant find a file to transfer =("
   end
   
   puts "\n\t backup.download_backup()\n\n"
   download(path,path,true)
  
end

#exist_on_server?(path) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/keepitsafe.rb', line 64

def exist_on_server? path
  (run_cmd("ls #{path}") || '').match(/No such file or directory/).nil?
end

#logObject



129
130
131
# File 'lib/keepitsafe.rb', line 129

def log
  @log_buffer.string
end

#on_localhost?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/keepitsafe.rb', line 167

def on_localhost?
  @server_domain == "localhost"
end

#remove_pending_fileObject



154
155
156
# File 'lib/keepitsafe.rb', line 154

def remove_pending_file
  puts run_cmd "rm #{backup_target_dir}/pending"
end

#run(&block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/keepitsafe.rb', line 46

def run &block
  
  puts "\n\n\t -- #{@server_domain} --\n\n"
  @backup_timastamp = Time.now.utc.strftime("%Y%m%d_%H%M.%S-UTC")
  
  
  if on_localhost?
    do_the_stuff &block if block_given?
  else
    Net::SSH.start( @server_domain , @options[:username] , :forward_agent => @options[:forward_agent]) do |ssh|
      @ssh = ssh
    
      do_the_stuff &block if block_given?
    
    end
  end 
end

#run_cmd(cmd) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/keepitsafe.rb', line 133

def run_cmd cmd
  puts "run_cmd(#{@server_domain}): #{cmd}"
  if on_localhost?
    `#{cmd} 2>&1`
  else
    @ssh.exec! cmd
  end
end

#set_backup_size(remote_path) ⇒ Object



173
174
175
176
177
# File 'lib/keepitsafe.rb', line 173

def set_backup_size remote_path
  
  raw = (run_cmd("du -hc #{remote_path}").gsub("\n",' ').gsub("\t",' ').strip.match(/([0-9kmgt.]{2,10})\s*total/i) || [0,"0"])[1]
  @backup_size = raw_to_meg(raw)
end

#upload(src, target, create_target_dir = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/keepitsafe.rb', line 68

def upload src,target,create_target_dir = false
  src = run_cmd("echo #{src}").strip # expand "~/"
  target = `echo #{target}`.strip # expand "~/"
  puts "local:#{src} => remote:#{target}"
  
  run_cmd("mkdir -p #{target.end_with?("/") ? target : File.dirname(target)}" ) if create_target_dir
  
  if on_localhost?
    `cp -a #{src} #{target}`
  else
    @ssh.scp.upload!(src,target)
  end
end

#upload_logObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/keepitsafe.rb', line 113

def upload_log    
  home_dir = run_cmd('echo ~')
  target_dir = "#{home_dir.strip}#{backup_target_dir[1..-1]}"
  if exist_on_server?(target_dir)
    puts "Uploading backup log"
    t = Tempfile.new('foo')
    t.write(log)
    t.close

    upload(t.path,"#{target_dir}"+"backup_log.txt")
    t.unlink
  else
    puts "Backup dir does not exist (any longer ?)"
  end
end