Class: LSync::Script
- Inherits:
-
Object
- Object
- LSync::Script
- Includes:
- EventHandler
- Defined in:
- lib/lsync/script.rb
Overview
The main backup/synchronisation mechanism is the backup script. It specifies all servers and directories, and these are then combined specifically to produce the desired data replication behaviour.
Instance Attribute Summary collapse
-
#directories ⇒ Object
readonly
All directories which may be synchronised.
-
#log ⇒ Object
readonly
Log data (an
IO
) specific to the current script. -
#logger ⇒ Object
The script logger which will be provided all events when the script is run.
-
#master ⇒ Object
The master server name (e.g. symbolic or host name).
-
#method ⇒ Object
A specific method which will perform the backup (e.g. an isntance of LSync::Method).
-
#servers ⇒ Object
readonly
All servers which are participating in the backup process.
Instance Method Summary collapse
-
#copy(*paths) ⇒ Object
(also: #backup)
Backup a particular path (or paths).
-
#find_current_server ⇒ Object
Find out the config section for the current server.
-
#find_master_server ⇒ Object
Find the master server based on the name #master= specified.
-
#find_named_server(name) ⇒ Object
(also: #[])
Given a name, find out which server config matches it.
-
#initialize(options = {}, &block) ⇒ Script
constructor
A new instance of Script.
-
#run! ⇒ Object
Run the backup process for all servers and directories specified.
-
#server(name) {|server| ... } ⇒ Object
Register a server with the backup script.
Methods included from EventHandler
Constructor Details
#initialize(options = {}, &block) ⇒ Script
Returns a new instance of Script.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lsync/script.rb', line 17 def initialize( = {}, &block) @logger = [:logger] || Logger.new($stdout) @method = nil @servers = {} @directories = [] @log = nil if block_given? instance_eval &block end end |
Instance Attribute Details
#directories ⇒ Object (readonly)
All directories which may be synchronised.
106 107 108 |
# File 'lib/lsync/script.rb', line 106 def directories @directories end |
#log ⇒ Object (readonly)
Log data (an IO
) specific to the current script.
109 110 111 |
# File 'lib/lsync/script.rb', line 109 def log @log end |
#logger ⇒ Object
The script logger which will be provided all events when the script is run.
94 95 96 |
# File 'lib/lsync/script.rb', line 94 def logger @logger end |
#master ⇒ Object
The master server name (e.g. symbolic or host name)
97 98 99 |
# File 'lib/lsync/script.rb', line 97 def master @master end |
#method ⇒ Object
A specific method which will perform the backup (e.g. an isntance of LSync::Method)
100 101 102 |
# File 'lib/lsync/script.rb', line 100 def method @method end |
#servers ⇒ Object (readonly)
All servers which are participating in the backup process.
103 104 105 |
# File 'lib/lsync/script.rb', line 103 def servers @servers end |
Instance Method Details
#copy(*paths) ⇒ Object Also known as: backup
Backup a particular path (or paths).
81 82 83 84 85 86 87 88 89 |
# File 'lib/lsync/script.rb', line 81 def copy(*paths) paths.each do |path| directory = Directory.new(path) yield directory if block_given? @directories << directory end end |
#find_current_server ⇒ Object
Find out the config section for the current server
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lsync/script.rb', line 49 def find_current_server master = find_master_server server = nil # Find out if the master server is local... if master.is_local? server = master else # Find a server config that specifies the local host server = @servers.values.find { |s| s.is_local? } end return server end |
#find_master_server ⇒ Object
Find the master server based on the name #master= specified
44 45 46 |
# File 'lib/lsync/script.rb', line 44 def find_master_server find_named_server(@master) end |
#find_named_server(name) ⇒ Object Also known as: []
Given a name, find out which server config matches it
32 33 34 35 36 37 38 39 |
# File 'lib/lsync/script.rb', line 32 def find_named_server name if @servers.key? name return @servers[name] else hostname = Socket.gethostbyname(name)[0] rescue name return @servers.values.find { |s| s["host"] == hostname } end end |
#run! ⇒ Object
Run the backup process for all servers and directories specified.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/lsync/script.rb', line 112 def run! start_time = Time.now # We buffer the log data so that if there is an error it is available to the notification sub-system @log = StringIO.new local_logger = Logger.new(@log) local_logger.formatter = MinimalLogFormat.new logger = @logger.tee(local_logger) master = find_master_server current = find_current_server # At this point we must know the current server or we can't continue if current == nil raise ScriptError.new("Could not determine current server!", :script => self, :master => @master) end if master.is_local? logger.info "We are the master server..." else logger.info "We are not the master server..." logger.info "Master server is #{@master}..." end master_controller = ServerController.new(self, logger, master) self.try do method.try do master.try(master_controller) do logger.info "Running backups for server #{current}..." run_backups!(master, current, logger) end end end end_time = Time.now logger.info "Backup Completed (#{end_time - start_time}s)." end |
#server(name) {|server| ... } ⇒ Object
Register a server with the backup script.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lsync/script.rb', line 65 def server(name) case name when Symbol host = "localhost" else host = name.to_s end server = Server.new(host) yield server if block_given? @servers[name] = server end |