3 Back up Untangle configurations via the web admin UI.
6 from argparse
import ArgumentDefaultsHelpFormatter
, ArgumentParser
9 from urllib
.error
import HTTPError
, URLError
10 from sys
import stderr
12 from untangle
.untangle
import Untangle
14 # Define a few exit codes.
16 EXIT_BACKUPS_FAILED
= 1
18 # Create an argument parser using our docsctring as its description.
19 parser
= ArgumentParser(description
= __doc__
,
20 formatter_class
= ArgumentDefaultsHelpFormatter
)
22 parser
.add_argument('-c',
24 default
='/etc/untangle-https-backup.ini',
25 help='path to configuration file')
27 args
= parser
.parse_args()
29 # Default to success, change it if anything fails.
32 # Parse the configuration file...
33 config
= configparser
.ConfigParser()
34 config
.read(args
.config_file
)
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
])
42 # And then try to log in and retrieve a backup.
44 backup
= u
.get_backup()
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
:
51 chmod(filename
, 0o600)
53 # If it didn't work, but in a predictable way (some host is down),
54 # then we report that error and keep going.
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