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.



129
130
131
# File 'lib/lsync/script.rb', line 129

def directories
  @directories
end

#logObject (readonly)

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



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

def log
  @log
end

#loggerObject

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



117
118
119
# File 'lib/lsync/script.rb', line 117

def logger
  @logger
end

#masterObject

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



120
121
122
# File 'lib/lsync/script.rb', line 120

def master
  @master
end

#methodObject

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



123
124
125
# File 'lib/lsync/script.rb', line 123

def method
  @method
end

#serversObject (readonly)

All servers which are participating in the backup process.



126
127
128
# File 'lib/lsync/script.rb', line 126

def servers
  @servers
end

Instance Method Details

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

Backup a particular path (or paths).



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

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

		yield directory if block_given?

		@directories << directory
	end
end

#find_current_serverObject

Find out the config section for the current server



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lsync/script.rb', line 72

def find_current_server
	master = find_master_server
	server = nil

	# Find out if the master server is local...
	if master && master.local?
		server = master
	else
		# Find a server config that specifies the local host
		server = @servers.values.find { |s| s.local? }
	end

	return server
end

#find_master_serverObject

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



67
68
69
# File 'lib/lsync/script.rb', line 67

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



55
56
57
58
59
60
61
62
# 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
end

#run!(options = {}) ⇒ Object

Run the backup process for all servers and directories specified.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lsync/script.rb', line 135

def run!(options = {})
	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.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
			logger.info "Running backups for server #{current}..."

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

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

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

Register a server with the backup script.

Yields:



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

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