Class: LSync::Script

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from EventHandler

#abort!, #fire, #on, #try

Constructor Details

#initialize(options = {}, &block) ⇒ Script

Returns a new instance of Script.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lsync/script.rb', line 33

def initialize(options = {}, &block)
	if options[:logger]
		@logger = options[:logger]
	else
		@logger = Logger.new($stdout)
		@logger.formatter = LSync::MinimalLogFormat.new
	end
	
	@method = nil

	@servers = {}
	@directories = []
	@master = "localhost"

	@log = nil

	if block_given?
		instance_eval &block
	end
end

Instance Attribute Details

#directoriesObject (readonly)

All directories which may be synchronised.


145
146
147
# File 'lib/lsync/script.rb', line 145

def directories
  @directories
end

#logObject (readonly)

Log data (an IO) specific to the current script.


148
149
150
# File 'lib/lsync/script.rb', line 148

def log
  @log
end

#loggerObject

The script logger which will be provided all events when the script is run.


133
134
135
# File 'lib/lsync/script.rb', line 133

def logger
  @logger
end

#masterObject

The master server name (e.g. symbolic or host name)


136
137
138
# File 'lib/lsync/script.rb', line 136

def master
  @master
end

#methodObject

A specific method which will perform the backup (e.g. an isntance of LSync::Method)


139
140
141
# File 'lib/lsync/script.rb', line 139

def method
  @method
end

#serversObject (readonly)

All servers which are participating in the backup process.


142
143
144
# File 'lib/lsync/script.rb', line 142

def servers
  @servers
end

Instance Method Details

#copy(*paths) ⇒ Object Also known as: backup

Backup a particular path (or paths).


120
121
122
123
124
125
126
127
128
# File 'lib/lsync/script.rb', line 120

def copy(*paths)
	paths.each do |path|
		directory = Directory.new(path)

		yield directory if block_given?

		@directories << directory
	end
end

#find_current_serverObject

Find the server that matches the current machine


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lsync/script.rb', line 82

def find_current_server
	master = find_master_server
	server = nil

	# 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.local?
		server = master
	else
		# Find a server config that specifies the local host
		server = @servers.values.find { |s| s.local? }
	end

	# At this point we must know the current server or we can't continue
	if server == nil
		raise ScriptError.new("Could not determine current server!", :script => self)
	end

	return server
end

#find_master_serverObject

Find the master server based on the name #master= specified


70
71
72
73
74
75
76
77
78
79
# File 'lib/lsync/script.rb', line 70

def find_master_server
	server = find_named_server(@master)
	
	# At this point we must know the current server or we can't continue
	if server == nil
		raise ScriptError.new("Could not determine master server!", :script => self, :name => @master)
	end
	
	return server
end

#find_named_server(name) ⇒ Object Also known as: []

Given a name, find out which server config matches it


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lsync/script.rb', line 55

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
	
	# No server was found for this name
	return nil
end

#run!(options = {}) ⇒ Object

Run the backup process for all servers and directories specified.


151
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
# File 'lib/lsync/script.rb', line 151

def run!(options = {})
	start_time = Time.now

	logger.info "===== Starting backup at #{start_time} ====="

	# 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

	if master.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(master_controller) do
		# This allows events to run on the master server if specified, before running any backups.
		master.try(master_controller) do
			method.try(master_controller) do
				logger.info "Running backups for server #{current}..."

				run_backups!(master, current, logger, options)
			end
		end
	end

	end_time = Time.now
	logger.info "[Time]: (#{end_time - start_time}s)."
	logger.info "===== Finished backup at #{end_time} ====="
end

#server(name) {|server| ... } ⇒ Object

Register a server with the backup script.

Yields:


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lsync/script.rb', line 104

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