-
Notifications
You must be signed in to change notification settings - Fork 0
/
mikrotik_ssh_backup.py
executable file
·157 lines (123 loc) · 4.18 KB
/
mikrotik_ssh_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
#!/usr/bin/env python3
import argparse
import subprocess
import datetime
import os
import sys
import shutil
import yaml
from pathlib import Path
from dataclasses import dataclass
from pprint import pprint
@dataclass
class Router:
ssh_host: str
filename_prefix: str
# default for RouterOS 7
export_command: str = "/export show-sensitive"
backup_command: str = "/system/backup save name=autobak dont-encrypt=yes"
def list_routers(config):
pprint(config["routers"])
def print_config(config):
print(yaml.dump(config))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-C", "--config-file",
help="path to configuration file (default: %(default)s)",
type=argparse.FileType("r"),
default="./mikrotik.yaml"
)
subparsers = parser.add_subparsers(dest="subparser_name", required=True)
subparsers.add_parser(
"list_routers",
help="list available routers from config file and exit"
)
subparsers.add_parser(
"print_config",
help="dump current config (incl. defaults) as yaml to stdout and exit"
)
parser_backup = subparsers.add_parser(
"backup",
help="perform backup"
)
parser_backup.add_argument(
"-d", "--diff",
help="Do not backup, just diff exports",
action="store_true"
)
parser_backup.add_argument(
"-e", "--export",
help="Make export (text script) backup",
action="store_true"
)
parser_backup.add_argument(
"-b", "--backup",
help="Make binary backup",
action="store_true"
)
parser_backup.add_argument(
"router",
help="router name (see list_routers)"
)
args = parser.parse_args()
with args.config_file as f:
config = yaml.safe_load(f)
config["tracked_dir"] = Path(config.get("tracked_dir", "tracked"))
config["repo_dir"] = Path(config.get("repo_dir", "."))
for router_name, cnf in config["routers"].items():
if cnf is None:
cnf = {}
router = Router(
ssh_host=cnf.get("ssh_host", router_name),
filename_prefix=cnf.get("filename_prefix", router_name),
)
if "backup_command" in cnf:
router.backup_command = cnf["backup_command"]
if "export_command" in cnf:
router.export_command = cnf["export_command"]
config["routers"][router_name] = router
if args.subparser_name == "list_routers":
list_routers(config)
sys.exit(0)
if args.subparser_name == "print_config":
print_config(config)
sys.exit(0)
if args.router not in config["routers"]:
parser_backup.error(f"unknown router: {args.router}")
router = config["routers"][args.router]
if args.diff:
subprocess.run(
f'ssh {router.ssh_host} "{router.export_command}" | '
f'diff --color -u "{router.filename_prefix}.rsc" -',
shell=True
)
sys.exit(0)
time_suffix = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
config["tracked_dir"].mkdir(parents=True, exist_ok=True)
filename_rsc = config["tracked_dir"] / f"{router.filename_prefix}_{time_suffix}.rsc"
filename_binary = config["tracked_dir"] / f"{router.filename_prefix}_{time_suffix}.backup"
if args.export:
print("exporting")
with open(filename_rsc, 'w') as f:
subprocess.run(
["ssh", router.ssh_host, router.export_command],
stdout=f
)
print("export written to", filename_rsc)
print("copying to repo")
shutil.copyfile(filename_rsc, config["repo_dir"] / (router.filename_prefix + ".rsc"))
if args.backup:
print("making binary backup")
subprocess.run(
["ssh", router.ssh_host, router.backup_command]
)
print("downloading")
subprocess.run(
["scp", router.ssh_host+":/autobak.backup", filename_binary]
)
print("binary backup written to", filename_binary,
"size:", os.stat(filename_binary).st_size)
print("done")
if __name__ == "__main__":
main()