Class: IncrementalBackup::Task

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

Constant Summary collapse

LOG_FILENAME =
'log'
NUM_LOG_FILES =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|settings| ... } ⇒ Task

Returns a new instance of Task.

Yields:



12
13
14
15
# File 'lib/incremental_backup/task.rb', line 12

def initialize(&block)
  self.settings = TaskSettings.new
  yield settings
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



10
11
12
# File 'lib/incremental_backup/task.rb', line 10

def settings
  @settings
end

Instance Method Details

#loggerObject



73
74
75
# File 'lib/incremental_backup/task.rb', line 73

def logger
  @logger ||= Logger.new(File.join(settings.settings_path, LOG_FILENAME), NUM_LOG_FILES)
end

#runObject

Perform the backup



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
# File 'lib/incremental_backup/task.rb', line 18

def run

  # Validate - this will throw an exception if settings are not valid
  validate_settings

  # Run everything inside a lock, ensuring that only one instance of this
  # task is running.
  Lock.create(self) do

    # Find the schedule to run
    schedule = find_schedule
    unless schedule
      logger.info "No backup needed - exiting"
      return
    end

    logger.info "Starting #{schedule} backup to #{settings.remote_server}"

    # Paths and other options
    timestamp = Time.now.strftime('backup_%Y-%m-%d-T%H-%M-%S')
    current_path = File.join(settings.remote_path, 'current')
    progress_path = File.join(settings.remote_path, 'incomplete')
    complete_path = File.join(schedule_path(schedule), timestamp)
     = "#{settings.remote_user}@#{settings.remote_server}"
    rsync_path = "#{}:#{progress_path}"

    # Make schedule folder
    execute_ssh "mkdir --verbose --parents #{schedule_path schedule}"

    # Rsync
    Rsync.execute(logger, settings.local_path, rsync_path, {
      exclude_file: settings.exclude_file,
      link_dest: current_path,
      max_upload_speed: settings.max_upload_speed,
      max_download_speed: settings.max_download_speed
    })

    # shuffle backups around
    logger.info "Do the backup shuffle"
    execute_ssh [
      "mv --verbose            #{progress_path} #{complete_path}",
      "rm --verbose --force    #{current_path}",
      "ln --verbose --symbolic #{complete_path} #{current_path}",
    ]

    delete_old_backups schedule

    logger.info "#{schedule} backup done"
  end

rescue Exception => exception
  logger.error exception.message
  logger.error exception.backtrace
end