Class: Backuper
- Inherits:
-
Object
- Object
- Backuper
- Defined in:
- lib/backuper.rb
Instance Attribute Summary collapse
-
#local_backup_base_path ⇒ Object
Returns the value of attribute local_backup_base_path.
-
#max_kept_backups ⇒ Object
Returns the value of attribute max_kept_backups.
-
#mysql_params ⇒ Object
Returns the value of attribute mysql_params.
-
#remote_backup_base_path ⇒ Object
Returns the value of attribute remote_backup_base_path.
-
#remote_backup_ssh_info ⇒ Object
Returns the value of attribute remote_backup_ssh_info.
-
#source_path ⇒ Object
Returns the value of attribute source_path.
Instance Method Summary collapse
-
#initialize(&block) ⇒ Backuper
constructor
A new instance of Backuper.
- #perform_database_backup ⇒ Object
- #perform_files_backup ⇒ Object
- #perform_remote_sync ⇒ Object
- #set(attribute, value) ⇒ Object
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_path ⇒ Object
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_backups ⇒ Object
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_params ⇒ Object
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_path ⇒ Object
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_info ⇒ Object
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_path ⇒ Object
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_backup ⇒ Object
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 = "#{Time.now.strftime('%Y-%-m-%d-%s')}" local_current_backup_path = "#{local_backup_base_path}/mysql/#{}.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_backup ⇒ Object
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 = "#{Time.now.strftime('%Y-%m-%d-%s')}" local_current_backup_path = "#{local_backup_base_path}/files/#{}" 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_sync ⇒ Object
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 |