3 Back up Untangle configurations via the web admin UI.
6 from argparse
import ArgumentDefaultsHelpFormatter
, ArgumentParser
8 from http
.client
import HTTPException
10 from urllib
.error
import HTTPError
, URLError
11 from socket
import timeout
12 from sys
import stderr
14 from untangle
.untangle
import Untangle
16 # Define a few exit codes.
18 EXIT_BACKUPS_FAILED
= 1
20 # Create an argument parser using our docsctring as its description.
21 parser
= ArgumentParser(description
= __doc__
,
22 formatter_class
= ArgumentDefaultsHelpFormatter
)
24 parser
.add_argument('-c',
26 default
='/etc/untangle-https-backup.ini',
27 help='path to configuration file')
29 args
= parser
.parse_args()
31 # Default to success, change it if anything fails.
34 # Parse the configuration file...
35 config
= configparser
.ConfigParser()
36 config
.read(args
.config_file
)
38 # And loop through each section.
39 for section
in config
.sections():
40 # For each section, we create an Untangle object based on that
41 # section's configuration.
42 u
= Untangle(config
[section
])
44 # And then try to log in and retrieve a backup.
46 backup
= u
.get_backup()
48 # If that worked, we save the backup file and make it
49 # accessible only to its owner.
50 filename
= u
.name
+ '.backup'
51 with open(filename
, 'wb') as f
:
53 chmod(filename
, 0o600)
55 # If it didn't work, but in a predictable way (some host is down),
56 # then we report that error and keep going.
58 tpl
= '{:s}: {:s} from {:s}'
59 msg
= tpl
.format(u
.name
, str(e
.reason
), u
.host
)
60 print(msg
, file=stderr
)
61 status
= EXIT_BACKUPS_FAILED
62 except HTTPError
as e
:
63 tpl
= '{:s}: HTTP error {:s} from {:s}'
64 msg
= tpl
.format(u
.name
, str(e
.code
), u
.host
)
65 print(msg
, file=stderr
)
66 status
= EXIT_BACKUPS_FAILED
67 except HTTPException
as e
:
68 # At least one sort of HTTPException (BadStatusLine) is not
69 # translated by urllib into an HTTPError, so we catch
71 tpl
= '{:s}: HTTP exception {:s} from {:s}'
72 msg
= tpl
.format(u
.name
, repr(e
), u
.host
)
73 print(msg
, file=stderr
)
74 status
= EXIT_BACKUPS_FAILED
76 # A socket.timeout exception occurs when something goes over
77 # the configured "timeout" limit.
78 tpl
= '{:s}: socket timeout ({:s}) from {:s}'
79 msg
= tpl
.format(u
.name
, str(e
), u
.host
)
80 print(msg
, file=stderr
)
81 status
= EXIT_BACKUPS_FAILED