-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.rb
118 lines (100 loc) · 2.48 KB
/
backup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require "timeout"
require "socket"
require "date"
class Ping
def self.pingecho(host, timeout=5, service="echo")
begin
timeout(timeout) do
s = TCPSocket.new(host, service)
s.close
end
rescue Errno::ECONNREFUSED
return true
rescue Timeout::Error, StandardError
return false
end
return true
end
end
class Backup
def initialize(macAddress, serverIP, clientBroadcast, user, maxWaitForWakeupInSecs, debug)
@macAddress = macAddress
@serverIP = serverIP
@clientBroadcast = clientBroadcast
@user = user
@maxWaitForWakeupInSecs = maxWaitForWakeupInSecs
@debug = debug
STDOUT.sync = true
end
def startBackupProcess
if clientInHomeNetwork?
log "client is at home"
shutdownAfterBackup = serverOnline? == false
log "server already online - no shutdown after backup" if not shutdownAfterBackup
log "server is offline - server will shutdown after backup" if shutdownAfterBackup
if not serverOnline?
log "waking up the machine"
wakeupBackupMachine
end
if serverOnline? && afpOnline?
log_without_new_line "backing up data"
backupData
if shutdownAfterBackup
log "send shutdown command"
shutdownServer
end
else
log "server still unavailable - no backup possible"
end
else
log "client is not at home - no backup"
end
end
def log msg
puts DateTime.now.to_s + " " + msg if @debug
end
def log_without_new_line msg
print DateTime.now.to_s + " " + msg if @debug
end
def serverOnline?
return Ping.pingecho(@serverIP, 5)
end
def afpOnline?
return Ping.pingecho(@serverIP, 5, 548)
end
def clientInHomeNetwork?
execute_command("ifconfig | grep " + @clientBroadcast) != ""
end
def execute_command command
open("|" + command).read
end
def wakeupBackupMachine
sendWakeupCommand
connectionTries = 0
while connectionTries < @maxWaitForWakeupInSecs && serverOnline? == false
sleep 1
connectionTries = connectionTries + 1
end
end
def sendWakeupCommand
execute_command "/usr/local/Cellar/wol/HEAD/bin/wol " + @macAddress
end
def backupData
startTimeMachine
sleep 60
while backupInProcess?
print "."
sleep 30
end
puts ''
end
def backupInProcess?
execute_command("ps -ax | grep /CoreServices/[b]ackupd 2>&1") != ""
end
def startTimeMachine
execute_command "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper"
end
def shutdownServer
execute_command "ssh " + @user + "@" + @serverIP + " sudo shutdown -p now"
end
end