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
-
#backup(*paths, &block) ⇒ Object
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.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lsync/script.rb', line 59 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.
146 147 148 |
# File 'lib/lsync/script.rb', line 146 def directories @directories end |
#log ⇒ Object (readonly)
Log data (an IO) specific to the current script.
149 150 151 |
# File 'lib/lsync/script.rb', line 149 def log @log end |
#logger ⇒ Object
The script logger which will be provided all events when the script is run.
134 135 136 |
# File 'lib/lsync/script.rb', line 134 def logger @logger end |
#master ⇒ Object
The master server name (e.g. symbolic or host name)
137 138 139 |
# File 'lib/lsync/script.rb', line 137 def master @master end |
#method ⇒ Object
A specific method which will perform the backup (e.g. an isntance of LSync::Method)
140 141 142 |
# File 'lib/lsync/script.rb', line 140 def method @method end |
#servers ⇒ Object (readonly)
All servers which are participating in the backup process.
143 144 145 |
# File 'lib/lsync/script.rb', line 143 def servers @servers end |
Instance Method Details
#backup(*paths, &block) ⇒ Object
Backup a particular path (or paths).
123 124 125 126 127 128 129 130 131 |
# File 'lib/lsync/script.rb', line 123 def backup(*paths, &block) 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lsync/script.rb', line 91 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
86 87 88 |
# File 'lib/lsync/script.rb', line 86 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
74 75 76 77 78 79 80 81 |
# File 'lib/lsync/script.rb', line 74 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/lsync/script.rb', line 152 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, master, logger) 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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/lsync/script.rb', line 107 def server(name, &block) 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 |