Class: Backuper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Backuper

Returns a new instance of Backuper.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/backuper.rb', line 6

def initialize(&block)
  instance_eval(&block)
  
  mysql_params['host']     ||= 'localhost'
  mysql_params['username'] ||= 'root'
  
  system "mkdir -p #{local_backup_base_path}"
  
  perform_files_backup
  perform_database_backup
  perform_remote_sync
end

Instance Attribute Details

#local_backup_base_pathObject

Returns the value of attribute local_backup_base_path.



3
4
5
# File 'lib/backuper.rb', line 3

def local_backup_base_path
  @local_backup_base_path
end

#max_kept_backupsObject

Returns the value of attribute max_kept_backups.



3
4
5
# File 'lib/backuper.rb', line 3

def max_kept_backups
  @max_kept_backups
end

#mysql_paramsObject

Returns the value of attribute mysql_params.



3
4
5
# File 'lib/backuper.rb', line 3

def mysql_params
  @mysql_params
end

#remote_backup_base_pathObject

Returns the value of attribute remote_backup_base_path.



3
4
5
# File 'lib/backuper.rb', line 3

def remote_backup_base_path
  @remote_backup_base_path
end

#remote_backup_ssh_infoObject

Returns the value of attribute remote_backup_ssh_info.



3
4
5
# File 'lib/backuper.rb', line 3

def remote_backup_ssh_info
  @remote_backup_ssh_info
end

#source_pathObject

Returns the value of attribute source_path.



3
4
5
# File 'lib/backuper.rb', line 3

def source_path
  @source_path
end

Instance Method Details

#perform_database_backupObject



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
# File 'lib/backuper.rb', line 48

def perform_database_backup
  if mysql_params == {} || mysql_params['database'] == '' || !(mysql_params['adapter'] =~ /mysql/)
    puts "Skipping MySQL backup as no configurtion given"
    return
  end
  
  timestamp = "#{Time.now.strftime('%Y-%-m-%d-%s')}"
  local_current_backup_path = "#{local_backup_base_path}/mysql/#{timestamp}.sql"
  local_latest_backup_path = Dir["#{local_backup_base_path}/mysql/*.sql"].last
  
  unless File.exist?("#{local_backup_base_path}/mysql")
    system "mkdir #{local_backup_base_path}/mysql"
  end
  
  # Local backup
  
  puts "Performing local backup of database '#{mysql_params['database']}' as user '#{mysql_params['username']}'"
  system "nice -n 10 mysqldump #{mysql_params['database']} --user=#{mysql_params['username']} --password=#{mysql_params['password']} > #{local_current_backup_path}"
  
  # Cleanup of old versions
  
  Dir["#{local_backup_base_path}/mysql/*"].reverse.each_with_index do |backup_version, index|
    if index >= max_kept_backups
      system "rm -rf #{backup_version}"
    end
  end
end

#perform_files_backupObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/backuper.rb', line 23

def perform_files_backup
  timestamp = "#{Time.now.strftime('%Y-%m-%d-%s')}"
  local_current_backup_path = "#{local_backup_base_path}/files/#{timestamp}"
  local_latest_backup_path = Dir["#{local_backup_base_path}/files/*"].last
  system "mkdir -p #{local_current_backup_path}"
  
  # Local backup
  
  if File.exist?("#{local_latest_backup_path}")
    puts "Performing local backup with hard links to pervious version"
    system "nice -n 10 rsync -az --delete --link-dest=#{local_latest_backup_path} #{source_path} #{local_current_backup_path}"
  else
    puts "Performing initial local backup"
    system "nice -n 10 rsync -az #{source_path} #{local_current_backup_path}"
  end
  
  # Cleanup of old versions
  
  Dir["#{local_backup_base_path}/files/*"].reverse.each_with_index do |backup_version, index|
    if index >= max_kept_backups
      system "rm -rf #{backup_version}"
    end
  end
end

#perform_remote_syncObject



76
77
78
79
80
# File 'lib/backuper.rb', line 76

def perform_remote_sync
  puts "Performing remote backup sync"
  system "nice -n 10 rsync -azH --delete #{local_backup_base_path} #{remote_backup_ssh_info}"
  puts "Done"
end

#set(attribute, value) ⇒ Object



19
20
21
# File 'lib/backuper.rb', line 19

def set(attribute, value)
  instance_variable_set("@#{attribute}", value)
end