Class: Synco::Script
- Inherits:
-
Controller
- Object
- Controller
- Synco::Script
- Defined in:
- lib/synco/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(*paths, **options, &block) ⇒ Object
(also: #copy, #backup, #sync)
All directories which may be synchronised.
-
#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 instance of Synco::Method).
-
#servers ⇒ Object
All servers which are participating in the backup process.
Attributes inherited from Controller
Instance Method Summary collapse
- #current_server ⇒ Object
-
#find_current_server ⇒ Object
Find the server that matches the current machine.
-
#find_named_server(name) ⇒ Object
(also: #[])
Given a name, find out which server config matches it.
- #freeze ⇒ Object
-
#initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) ⇒ Script
constructor
A new instance of Script.
- #localhost?(name) ⇒ Boolean
-
#master_server ⇒ Object
The master server based on the name #master= specified.
- #resolve_name(name) ⇒ Object
- #running_on_master? ⇒ Boolean
-
#server(*arguments, **options, &block) ⇒ Object
Register a server with the backup script.
Methods inherited from Controller
#abort!, build, #fire, #on, #try
Constructor Details
#initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) ⇒ Script
Returns a new instance of Script.
35 36 37 38 39 40 41 42 |
# File 'lib/synco/script.rb', line 35 def initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) super() @method = method @servers = servers @directories = directories @master = master end |
Instance Attribute Details
#directories(*paths, **options, &block) ⇒ Object Also known as: copy, backup, sync
All directories which may be synchronised.
106 107 108 109 110 |
# File 'lib/synco/script.rb', line 106 def directories(*paths, **, &block) paths.each do |path| @directories << Directory.build(path, **, &block) end end |
#master ⇒ Object
The master server name (e.g. symbolic or host name)
117 118 119 |
# File 'lib/synco/script.rb', line 117 def master @master end |
#method ⇒ Object
A specific method which will perform the backup (e.g. an instance of Synco::Method)
120 121 122 |
# File 'lib/synco/script.rb', line 120 def method @method end |
#servers ⇒ Object
All servers which are participating in the backup process.
123 124 125 |
# File 'lib/synco/script.rb', line 123 def servers @servers end |
Instance Method Details
#current_server ⇒ Object
95 96 97 |
# File 'lib/synco/script.rb', line 95 def current_server @current_server ||= find_current_server end |
#find_current_server ⇒ Object
Find the server that matches the current machine
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/synco/script.rb', line 84 def find_current_server # There might be the case that the the local machine is both the master server and the backup server.. # thus we check first if the master server is the local machine: if master_server and localhost?(master_server.host) @master_server else # Find a server config that specifies the local host @servers.values.find{|server| localhost?(server.host)} end || Server.new('localhost') end |
#find_named_server(name) ⇒ Object Also known as: []
Given a name, find out which server config matches it.
67 68 69 70 71 72 73 74 |
# File 'lib/synco/script.rb', line 67 def find_named_server(name) if @servers.key? name @servers[name] else host = resolve_name(name) @servers.values.find{|server| server.host == host} end end |
#freeze ⇒ Object
44 45 46 47 48 |
# File 'lib/synco/script.rb', line 44 def freeze current_server; master_server super end |
#localhost?(name) ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/synco/script.rb', line 58 def localhost?(name) return true if name == "localhost" host = resolve_name(Socket.gethostname) return name == host end |
#master_server ⇒ Object
The master server based on the name #master= specified
79 80 81 |
# File 'lib/synco/script.rb', line 79 def master_server @master_server ||= find_named_server(@master) end |
#resolve_name(name) ⇒ Object
54 55 56 |
# File 'lib/synco/script.rb', line 54 def resolve_name(name) Socket.gethostbyname(name)[0] end |
#running_on_master? ⇒ Boolean
50 51 52 |
# File 'lib/synco/script.rb', line 50 def running_on_master? current_server.same_host?(master_server) end |