forked from johnf/synobackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyper
executable file
·132 lines (106 loc) · 2.79 KB
/
hyper
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
126
127
128
129
130
131
132
#!/usr/bin/env ruby
require 'httparty'
require 'rufus-scheduler'
class Backup
include HTTParty
format :json
def initialize(dsm, username, password, verify_ssl = true)
self.class.base_uri dsm
if !verify_ssl
self.class.default_options.update(verify: false)
end
@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 = {
additional: '["last_bkp_result"]',
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} * * *"
verify_ssl = !(ENV['VERIFY_SSL'] == 'false')
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, verify_ssl)
scheduler = Rufus::Scheduler.new
scheduler.cron(start_cron) do
puts 'Resuming backup tasks'
tasks = backup.tasks
tasks = tasks.select {|t| t['last_bkp_result'] == 'suspend' }
tasks.each do |task|
puts "resuming #{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 = tasks.select {|t| t['last_bkp_result'] == 'backingup' }
tasks.each do |task|
puts "suspending #{task['name']}"
response = backup.suspend task['task_id']
puts response
end
end
scheduler.join