-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
283 lines (239 loc) · 7.77 KB
/
__main__.py
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
133
134
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
from subprocess import check_output, call
from time import sleep
import json
import os
import sys
import datetime
import logging
import argparse
import socket
# Default settings
settings = {
"cifs": {
"user": "",
"password": "",
"share": "//10.0.0.2/backup",
"target": "/media/backup",
"subDir": ""
},
"backup": {
"alternative_script_path": None,
"image_base_name": "sdimage",
"image_size": None,
"full_backup_interval": "monthly"
},
"mqtt" : {
"client_id": "rpi-backup",
"host": "127.0.0.1",
"port": 1883,
"keepalive": 60,
"bind_address": "",
"username": None,
"password": None,
"qos": 0,
"pub_topic_namespace": "pi/backup",
"retain": True
}
}
#
# FUNCTIONS
#
def mqtt_publish(topic, payload):
# Send out messages to the MQTT broker
log.debug('MQTT pub: {}: {}'.format(topic, payload))
mqtt_client.publish(
topic = namespace + '/' + topic,
payload = payload,
qos = settings['mqtt']['qos'],
retain = settings['mqtt']['retain'])
def fatal_error(message, error_code = 1, unmount = None):
# Publish error messages on MQTT and logs
mqtt_publish(
topic = "last_error/message",
payload = message)
mqtt_publish(
topic = "last_error/timestamp",
payload = str(datetime.datetime.now()))
log.fatal('{} [ERR{}]'.format(
message,
str(error_code)))
# Try to unmount cleanly
if unmount != None:
call('umount {}'.format(unmount), shell=True)
# Stop script
sys.exit(error_code)
#
# CONFIGURATION
#
# Parse arguments
parser = argparse.ArgumentParser(description="Python bkup_rpimage wrapper and MQTT bridge")
parser.add_argument("-c", "--config", default="config.json", help="Configuration file (default: %(default)s)")
parser.add_argument("-l", "--loglevel", default="INFO", help="Event level to log (default: %(default)s)")
args = parser.parse_args()
# Parse log level
num_level = getattr(logging, args.loglevel.upper(), None)
if not isinstance(num_level, int):
raise ValueError('Invalid log level: %s' % args.loglevel)
# Set up logging
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(level=num_level, format=log_format)
log = logging.getLogger(__name__)
log.debug('Loglevel is %s', logging.getLevelName(log.getEffectiveLevel()))
# Update default settings from the settings file
with open(args.config) as f:
overrides = json.load(f)
if 'backup' in overrides and isinstance(overrides['backup'], dict):
settings['backup'].update(overrides['backup'])
if 'cifs' in overrides and isinstance(overrides['cifs'], dict):
settings['cifs'].update(overrides['cifs'])
if 'mqtt' in overrides and isinstance(overrides['mqtt'], dict):
settings['mqtt'].update(overrides['mqtt'])
#
# MQTT CONNECTION
#
namespace = settings['mqtt']['pub_topic_namespace']
# Set up paho-mqtt
log.debug("Initializing MQTT...")
mqtt_client = mqtt.Client(
client_id = settings['mqtt']['client_id'])
if settings['mqtt']['username']:
mqtt_client.username_pw_set(
settings['mqtt']['username'],
settings['mqtt']['password'])
# The will makes sure the device registers as offline when the connection
# is lost
mqtt_client.will_set(
topic = namespace,
payload = "offline",
qos = settings['mqtt']['qos'],
retain = True)
mqtt_client.connect_async(
host = settings['mqtt']['host'],
port = settings['mqtt']['port'],
keepalive = settings['mqtt']['keepalive'],
bind_address = settings['mqtt']['bind_address'])
mqtt_client.loop_start()
# First message telling that the backup is in progress
sleep(3) # give mqtt some time to start
mqtt_client.publish(
topic = namespace,
payload = "busy",
qos = settings['mqtt']['qos'],
retain = True)
#
# PREPARING FOR BACKUP
#
mountpoint = settings['cifs']['target']
mountcmd = 'mount -t cifs -o user={},password={},rw,file_mode=0777,dir_mode=0777 {} {}'.format(
settings['cifs']['user'],
settings['cifs']['password'],
settings['cifs']['share'],
mountpoint)
backup_path = mountpoint + '/' + settings['cifs']['subDir']
# Check if mount point exists
log.debug("Checking mount point ({})...".format(mountpoint))
if not os.path.isdir(mountpoint):
fatal_error(
message = 'Invalid mount point {}'.format(mountpoint),
error_code = 1)
# Mount the cifs share
if not os.path.ismount(mountpoint):
log.debug("Mounting {}...".format(mountpoint))
call(mountcmd, shell=True)
# Check if the share is successfully mounted and the backup directory is present
log.debug("Checking mount point...")
if not os.path.ismount(mountpoint):
fatal_error(
message = 'Failed to mount backup volume {}'.format(mountpoint),
error_code = 2)
elif not os.path.isdir(backup_path):
fatal_error(
message = 'Invalid backup directory {}'.format(backup_path),
error_code = 3,
unmount = mountpoint)
# Device directory on backup volume
maccmd = "cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address"
device_directory = '{}_{}'.format(
socket.gethostname(), #device hostname
check_output(maccmd, shell=True).decode('utf-8').replace(":","").strip() #mac address
)
backup_path += '/{}'.format(device_directory)
if not os.path.isdir(backup_path):
log.info("Creating directory {}...".format(device_directory))
os.mkdir(backup_path)
if not os.path.isdir(backup_path):
fatal_error(
message = 'Failed to create {}'.format(backup_path),
error_code = 4,
unmount = mountpoint)
# Construct image name
image = settings['backup']['image_base_name']
interval = settings['backup']['full_backup_interval'].lower()[0]
now = datetime.datetime.now()
if interval == "d":
image += '_{:04d}-{:02d}-{:02d}.img'.format(
now.year,
now.month,
now.day)
elif interval == "w":
image += '_{:04d}-wk{:02d}.img'.format(
now.year,
datetime.date(now.year, now.month, now.day).isocalendar()[1])
elif interval == "m":
image += '_{:04d}-{:02d}.img'.format(
now.year,
now.month)
elif interval == "y":
image += '_{:04d}.img'.format(now.year)
# Check if image exists
if os.path.isfile('{}/{}'.format(backup_path, image)):
log.info("Updating {}".format(image))
else:
log.info("Full backup to {}".format(image))
#
# INVOKE BACKUP
#
# bkup_rpimage script path
if settings['backup']['alternative_script_path'] == None:
script_path = os.path.dirname(os.path.realpath(__file__)) + '/bkup_rpimage/bkup_rpimage.sh'
else:
script_path = settings['backup']['alternative_script_path']
# Check if script is found
if not os.path.isfile(script_path):
fatal_error(
message = 'File not found: {}'.format(script_path),
error_code = 5,
unmount = mountpoint)
# Additional parameters
if settings['backup']['image_size'] != None:
script_options = '-s {}'.format(str(settings['backup']['image_size']))
# Build backup command
backupcmd = 'sh {} start {} -c {}/{}'.format(
script_path,
script_options,
backup_path,
image)
# Call backup command
return_code = call(backupcmd, shell=True)
if return_code > 0:
fatal_error(
message = 'Backup stopped with exit code {}'.format(return_code),
error_code = 6,
unmount = mountpoint)
#
# FINALISE
#
# Unmount the backup volume
return_code = call('umount ' + mountpoint, shell=True)
if return_code > 0:
fatal_error(
message = 'Failed to unmount the backup volume',
error_code = 7)
# Report successful backup
now = str(datetime.datetime.now())
mqtt_publish(
topic = "last_success",
payload = now)
sys.exit(0)