-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub_list_backups.py
executable file
·94 lines (73 loc) · 2.39 KB
/
hub_list_backups.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
#!/usr/bin/python3
#
# Copyright (c) 2011 Alon Swartz <[email protected]>
# Copyright (c) 2022 TUrnKey GNU/Linux <[email protected]>
#
# This file is part of HubTools.
#
# HubTools is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
"""
List backup records
By default uses a built-in format, unless a user-specified format is specified.
Format variables:
%backup_id Backup ID
%label Descriptive label
%turnkey_version Appliance version code
%server_id Associated server id
%created Date the backup record was created
%updated Date the backup record was last updated
%size Aggregate size of backup, in bytes
%address Backup target address
%skpp Secret Key Passphrase Protection (bool)
Examples:
hub-list-backups
hub-list-backups "backup_id=%backup_id label=%label size=%{size}bytes"
Environment variables:
HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hublib import Hub
from hublib.formatter import Formatter, fmt_backup_header, fmt_backup
from hublib.utils import fatal
def usage(e=None):
if e:
print("error: " + str(e), file=sys.stderr)
print("Syntax: %s [ <format> ]" % (sys.argv[0]), file=sys.stderr)
print(__doc__, file=sys.stderr)
sys.exit(1)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError as e:
usage(e)
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
if args:
if len(args) != 1:
usage("incorrect number of arguments")
format = args[0]
else:
format = None
apikey = os.getenv('HUB_APIKEY', None)
if not apikey:
fatal("HUB_APIKEY not specified in environment")
hub = Hub(apikey)
backups = hub.backups.get()
if backups:
if format:
format = Formatter(format)
for backup in backups:
print(format(backup))
else:
print(fmt_backup_header())
for backup in backups:
print(fmt_backup(backup))
if __name__ == "__main__":
main()