]> gitweb.michael.orlitzky.com - untangle-https-backup.git/blob - bin/untangle-https-backup
Reorganize the source code into a package.
[untangle-https-backup.git] / bin / untangle-https-backup
1 #!/usr/bin/python3
2 """
3 Back up Untangle configurations via the web admin UI.
4 """
5
6 from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
7 import configparser
8 from os import chmod
9 from urllib.error import HTTPError, URLError
10 from sys import stderr
11
12 from untangle.untangle import Untangle
13
14 # Define a few exit codes.
15 EXIT_OK = 0
16 EXIT_BACKUPS_FAILED = 1
17
18 # Create an argument parser using our docsctring as its description.
19 parser = ArgumentParser(description = __doc__,
20 formatter_class = ArgumentDefaultsHelpFormatter)
21
22 parser.add_argument('-c',
23 '--config-file',
24 default='/etc/untangle-https-backup.ini',
25 help='path to configuration file')
26
27 args = parser.parse_args()
28
29 # Default to success, change it if anything fails.
30 status = EXIT_OK
31
32 config = configparser.ConfigParser()
33 config.read(args.config_file)
34
35 for section in config.sections():
36 u = Untangle(config[section])
37 try:
38 u.login()
39 backup = u.get_backup()
40 filename = u.name + '.backup'
41 with open(filename, 'wb') as f:
42 f.write(backup)
43 chmod(filename, 0o600)
44
45 except URLError as e:
46 msg = u.name + ': ' + str(e.reason) + ' from ' + u.host
47 print(msg, file=stderr)
48 status = EXIT_BACKUPS_FAILED
49 except HTTPError as e:
50 msg = u.name + ': ' + 'HTTP error ' + str(e.code) + ' from ' + u.host
51 print(msg, file=stderr)
52 status = EXIT_BACKUPS_FAILED
53
54 exit(status)