-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrotik_backup.py
222 lines (196 loc) · 8.75 KB
/
mikrotik_backup.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
from sys import exit
from time import sleep
from threading import Thread
from datetime import datetime
from netmiko import file_transfer
from argparse import ArgumentParser
from os import path, mkdir, environ, stat
from netmiko.exceptions import NetmikoTimeoutException
from related_utils import remove_old_files, generate_telegram_bot, markdownv2_converter
from related_utils import generate_connector, allowed_filename, print_output, size_converter
def args_parser():
parser = ArgumentParser(description='RouterOS backuper.')
parser.add_argument('-s', '--sshconf', type=str, help='Path to ssh_config.', required=False)
parser.add_argument('-n', '--hosts', type=str,
help='Comma separated hosts or single host (in ssh_config).', required=False)
parser.add_argument('-f', '--hostfile', type=str, help='Path to file with list of Hosts.',
required=False)
parser.add_argument('-p', '--path', type=str, help='Path to backups.', required=True)
parser.add_argument('-t', '--lifetime', type=int, help='Files (backup) lifetime (in days).',
required=False)
parser.add_argument('-b', '--bottoken', type=str, help='Telegram Bot token.', required=False)
parser.add_argument('-c', '--chatid', type=str, help='Telegram chat id.', required=False)
arguments = parser.parse_args().__dict__
return arguments
def hosts_to_devices(hosts):
devices = []
ssh_config_file = args_in['sshconf'] if args_in['sshconf'] else path.join(environ.get('HOME'), '.ssh/config')
for hostname in hosts:
hostname = hostname.strip()
if hostname:
try:
host_device = Backuper(
ssh_config_file=ssh_config_file,
host=hostname,
path_to_backups=args_in['path'],
lifetime=args_in['lifetime']
)
except (NetmikoTimeoutException, ValueError) as exc:
text = exc.__str__().replace('\n', ' ').replace(' ', ' ')
host_device = Failakuper(
host=hostname,
exc_text=text,
)
devices.append(host_device)
return devices
def summary_report(reports, lifetime):
many_hosts = len(reports) > 1
ending = {
True: 'ах',
False: 'е',
}
emoji_dead = '\U0001F480' # 💀
message_header = f'Отчёт о проведении бэкапа настроек на Микротик{ending[many_hosts]}.\n\n'
message_body = ''
message_footer = ''
if lifetime:
message_footer += f'{emoji_dead}Также были удалены ранее сохранённые бэкапы старше {lifetime} дн.'
for report in reports:
message_body += f'{report}\n'
message = markdownv2_converter(message_header) + message_body + markdownv2_converter(message_footer)
return message
class Backuper(Thread):
def __init__(self, host, path_to_backups, ssh_config_file, lifetime, *args, **kwargs):
super().__init__(*args, **kwargs)
self.path_to_backups = path_to_backups
self.connect = generate_connector(
args={'sshconf': ssh_config_file, 'host': host},
)
self.lifetime = lifetime
self.subdir = 'backup'
self.delay = 10
self.report = ''
self.emoji = {
'device': '\U0001F4F6', # 📶
'dir': '\U0001F4C2', # 📂
'ok': '\U00002705', # ✅
'not ok': '\U0000274C', # ❌
}
def run(self):
self.connect.enable()
identity = self.generate_identity()
path_to_backup = path.join(self.path_to_backups, identity)
backup_name = f'{identity}_{datetime.now().strftime("%Y.%m.%d_%H.%M.%S")}'
self.make_dirs(path_to_backup)
self.create_backup(backup_name)
sleep(self.delay)
self.add_to_report(f'В каталоге {self.emoji["dir"]}`{markdownv2_converter(path_to_backup)}/` сохранены файлы:')
for backup_type in ['rsc', 'backup']:
self.download_backup(backup_type, backup_name, path_to_backup)
self.remove_backup_from_device(backup_type, backup_name)
sleep(self.delay)
self.connect.disconnect()
if self.lifetime:
remove_old_files(path_to_backup, self.lifetime)
def add_to_report(self, text, paragraph=False):
self.report += '\n' * paragraph + f'{text}\n'
def generate_identity(self):
command = '/system identity print'
identity = print_output(self.connect, command)
identity_name = re.match(r'^name: (.*)$', identity).group(1)
self.add_to_report(f'{self.emoji["device"]}*{markdownv2_converter(identity_name)}*')
allowed_identity_name = allowed_filename(identity_name)
return allowed_identity_name
def make_dirs(self, path_to_backup):
try:
mkdir(path_to_backup)
except FileExistsError:
pass
command = f'/file print detail where name={self.subdir}'
backup_dir = print_output(self.connect, command)
if not backup_dir:
# Crutch for create directory ROS6
self.connect.send_command(f'/ip smb shares add directory={self.subdir} name=crutch_for_dir')
self.connect.send_command('/ip smb shares remove [/ip smb shares find where name=crutch_for_dir]')
# Create directory ROS7
try:
self.connect.send_command(f'/file add name={self.subdir} type=directory')
except Exception:
pass
def create_backup(self, backup_name):
file_path_name = f'{self.subdir}/{backup_name}'
self.connect.send_command(
f'/export file={file_path_name}.rsc', read_timeout=240, cmd_verify=False, expect_string=""
)
self.connect.send_command(
f'/system backup save dont-encrypt=yes name={file_path_name}.backup', read_timeout=240, cmd_verify=False, expect_string=""
)
def download_backup(self, backup_type, backup_name, path_to_backup):
src_file = f'{backup_name}.{backup_type}'
dst_file = f'{path_to_backup}/{backup_name}.{backup_type}'
direction = 'get'
transfer_dict = file_transfer(
self.connect,
source_file=src_file,
dest_file=dst_file,
file_system=self.subdir,
direction=direction,
overwrite_file=True,
disable_md5=True,
socket_timeout=60.0,
)
file_name = markdownv2_converter(src_file)
try:
file_stats = stat(dst_file)
except FileNotFoundError:
file_info = f'{self.emoji["not ok"]}`{file_name}`'
else:
file_size = markdownv2_converter(size_converter(file_stats.st_size))
file_name = markdownv2_converter(src_file)
file_info = f'{self.emoji["ok"]}`{file_name}` ➜ {file_size}'
self.add_to_report(file_info)
def remove_backup_from_device(self, backup_type, backup_name):
self.connect.send_command(f'/file remove {self.subdir}/{backup_name}.{backup_type}')
class Failakuper(Thread):
def __init__(self, host, exc_text, *args, **kwargs):
super().__init__(*args, **kwargs)
self.host = markdownv2_converter(host)
self.exc_text = markdownv2_converter(exc_text)
self.report = ''
self.emoji = {
'device': '\U0001F4F6', # 📶
'not ok': '\U0000274C', # ❌
}
def run(self):
self.add_to_report(f'{self.emoji["device"]}*{self.host}*')
self.add_to_report(f'{self.emoji["not ok"]}`{self.exc_text}`')
def add_to_report(self, text, paragraph=False):
self.report += '\n' * paragraph + f'{text}\n'
def main():
hosts = []
match args_in['hostfile'], args_in['hosts']:
case str() as path_to_file, None:
with open(path_to_file) as file:
hosts = file.read().splitlines()
case None, str() as host:
hosts = host.split(',')
case file, host:
exit(f'What needs to be used: {file} or {host}?')
telegram_bot = generate_telegram_bot(args_in['bottoken'], args_in['chatid'])
devices_backup = hosts_to_devices(hosts)
for device in devices_backup:
device.start()
for device in devices_backup:
device.join()
if telegram_bot and telegram_bot.alive():
devices_reports = []
for device in devices_backup:
devices_reports.append(device.report)
report = summary_report(devices_reports, args_in['lifetime'])
telegram_bot.send_text_message(report)
if __name__ == '__main__':
args_in = args_parser()
main()