]> gitweb.michael.orlitzky.com - untangle-https-backup.git/blob - bin/untangle-https-backup
Add some source documentation.
[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 # Parse the configuration file...
33 config = configparser.ConfigParser()
34 config.read(args.config_file)
35
36 # And loop through each section.
37 for section in config.sections():
38 # For each section, we create an Untangle object based on that
39 # section's configuration.
40 u = Untangle(config[section])
41 try:
42 # And then try to log in and retrieve a backup.
43 u.login()
44 backup = u.get_backup()
45
46 # If that worked, we save the backup file and make it
47 # accessible only to its owner.
48 filename = u.name + '.backup'
49 with open(filename, 'wb') as f:
50 f.write(backup)
51 chmod(filename, 0o600)
52
53 # If it didn't work, but in a predictable way (some host is down),
54 # then we report that error and keep going.
55 except URLError as e:
56 msg = u.name + ': ' + str(e.reason) + ' from ' + u.host
57 print(msg, file=stderr)
58 status = EXIT_BACKUPS_FAILED
59 except HTTPError as e:
60 msg = u.name + ': ' + 'HTTP error ' + str(e.code) + ' from ' + u.host
61 print(msg, file=stderr)
62 status = EXIT_BACKUPS_FAILED
63
64 exit(status)