-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhyper
More file actions
executable file
·125 lines (99 loc) · 2.48 KB
/
hyper
File metadata and controls
executable file
·125 lines (99 loc) · 2.48 KB
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
119
120
121
122
123
124
125
#!/usr/bin/env ruby
require 'httparty'
require 'rufus-scheduler'
class Backup
include HTTParty
format :json
def initialize(dsm, username, password)
self.class.base_uri dsm
@sid = login(username, password)
end
def login(username, password)
params = {
api: 'SYNO.API.Auth',
version: 3,
method: 'login',
account: username,
passwd: password,
session: 'sessionName',
format: 'sid'
}
response = self.class.get('/webapi/auth.cgi', query: params)
return response.parsed_response['data']['sid']
end
def logout
params = {
api: 'SYNO.API.Auth',
version: 3,
method: 'logout',
session: 'sessionName',
}
self.class.get('/webapi/auth.cgi', query: params)
end
def tasks
params = {
api: 'SYNO.Backup.Task',
version: 1,
method: 'list',
sessionName: 'sessionName',
_sid: @sid,
}
response = self.class.get('/webapi/entry.cgi', query: params)
response.parsed_response['data']['task_list']
end
def suspend(task_id)
params = {
api: 'SYNO.Backup.Task',
version: 1,
method: 'suspend',
task_state: 'backupable',
sessionName: 'sessionName',
_sid: @sid,
task_id: task_id,
}
self.class.get('/webapi/entry.cgi', query: params).parsed_response
end
def resume(task_id)
params = {
api: 'SYNO.Backup.Task',
version: 1,
method: 'resume',
sessionName: 'sessionName',
_sid: @sid,
task_id: task_id,
}
self.class.get('/webapi/entry.cgi', query: params).parsed_response
end
end
dsm = ENV['DSM']
username = ENV['USERNAME']
password = ENV['PASSWORD']
start_hour = ENV['START_HOUR']
end_hour = ENV['END_HOUR']
start_cron = ENV['START_CRON'] || "0 #{start_hour} * * *"
end_cron = ENV['END_CRON'] || "0 #{end_hour} * * *"
puts "Connecting to #{dsm}"
puts "Start Cron: #{start_cron}"
puts "End Cron: #{end_cron}"
puts "Current time: #{Time.now}"
backup = Backup.new(dsm, username, password)
scheduler = Rufus::Scheduler.new
scheduler.cron(start_cron) do
puts 'Resuming backup tasks'
tasks = backup.tasks
tasks.each do |task|
puts "resuming #{task['task_name']}"
response = backup.resume task['task_id']
puts response
end
end
scheduler.cron(end_cron) do
puts 'Suspending backup tasks'
tasks = backup.tasks
tasks.each do |task|
puts "suspending #{task['task_name']}"
response = backup.suspend task['task_id']
puts response
end
end
scheduler.join