-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
216 lines (188 loc) · 6 KB
/
Copy pathbackup.py
File metadata and controls
216 lines (188 loc) · 6 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import argparse
from commands.add import add_handler
from commands.init import init_handler
from commands.remove import remove_handler
from commands.update import update_handler
from commands.default import default_handler
from commands.list import list_handler
parser = argparse.ArgumentParser(
description="Automate backups to Google Drive",
epilog="Also check and update backups in config.json",
)
main_commands_subparser = parser.add_subparsers(
title="commands", description="Main actions commands", required=True
)
# INIT COMMAND
init_command_parser = main_commands_subparser.add_parser(
"init",
help="Create a blank config.json file",
epilog="File is only created if it doesn't already exist",
)
init_command_parser.set_defaults(func=init_handler)
# ADD COMMAND
add_command_parser = main_commands_subparser.add_parser(
"add",
help="Add a new backup to config.json",
epilog="Also check and update backups in config.json. Schedule backups after adding them",
)
add_command_parser.add_argument(
"--local", help="Local path to backup", metavar="PATH", dest="local_path"
)
add_command_parser.add_argument(
"--drive",
help="Drive folder to backup to (ID available in URL)",
metavar="DRIVE_FOLDER_ID",
dest="drive_folder",
)
add_command_parser.add_argument(
"--account",
help="Google Drive account email",
metavar="EMAIL",
dest="drive_account",
)
add_command_parser.add_argument(
"--freq",
help="Backup frequency (e.g. 15m, 2h, 1d, 1w)",
metavar="FREQUENCY",
dest="frequency",
)
add_command_parser.add_argument("--time", help="Time backup is done", metavar="HH:MM")
add_command_parser.set_defaults(func=add_handler)
# REMOVE COMMAND
remove_command_parser = main_commands_subparser.add_parser(
"remove",
aliases=["rm"],
help="Remove backups from config.json (and unschedule it)",
)
remove_command_parser.add_argument(
"ids", nargs="+", type=int, help="IDs of backups in config.json"
)
remove_command_parser.set_defaults(func=remove_handler)
# UPDATE COMMAND
update_command_parser = main_commands_subparser.add_parser(
"update",
help="Update a backup in config.json",
argument_default=argparse.SUPPRESS,
)
update_command_parser.add_argument(
"ids", nargs="+", help="IDs of backups in config.json", type=int
)
update_command_parser.add_argument(
"--local", help="Local path to backup", metavar="PATH", dest="local_path"
)
update_command_parser.add_argument(
"--drive",
help="Drive folder to backup to (ID available in URL)",
metavar="DRIVE_FOLDER_ID",
dest="drive_folder",
)
update_command_parser.add_argument(
"--account",
help="Google Drive account email",
metavar="EMAIL",
dest="drive_account",
)
update_command_parser.add_argument(
"--freq",
help="Backup frequency (e.g. 15m, 2h, 1d, 1w)",
metavar="FREQUENCY",
dest="frequency",
)
update_command_parser.add_argument(
"--time", help="Time backup is done", metavar="HH:MM"
)
update_command_parser.set_defaults(func=update_handler)
# LIST COMMAND
list_command_parser = main_commands_subparser.add_parser(
"list",
aliases=["ls"],
help="List backups in config.json",
)
list_command_parser.add_argument(
"ids", nargs="*", help="IDs of backups in config.json", type=int
)
list_command_parser.add_argument(
"-d", "--defaults", action="store_true", help="Show default values in config.json"
)
list_command_parser.add_argument(
"-v", "--verbose", action="store_true", help="Show more info about each backup"
)
list_command_parser.set_defaults(func=list_handler)
# DEFAULT COMMAND
default_command_parser = main_commands_subparser.add_parser(
"default",
help="Set default values for backups",
argument_default=argparse.SUPPRESS,
)
default_command_parser.add_argument(
"--account",
help="Default Google Drive account email",
metavar="EMAIL",
dest="drive_account",
)
default_command_parser.add_argument(
"--freq",
help="Default frequency for backups (e.g. 15m, 2h, 1d, 1w)",
metavar="FREQUENCY",
dest="frequency",
)
default_command_parser.add_argument(
"--time", help="Default time for backups", metavar="HH:MM"
)
default_command_parser.set_defaults(func=default_handler)
# RUN COMMAND
run_command_parser = main_commands_subparser.add_parser(
"run", help="Manually run backups now"
)
run_command_parser.add_argument(
"ids", nargs="*", help="IDs of backups in config.json", type=int
)
run_command_parser.add_argument(
"--all", action="store_true", help="Run all backups now"
)
# SCHEDULE COMMAND
schedule_command_parser = main_commands_subparser.add_parser(
"schedule",
help="Schedule backups from config.json",
)
schedule_command_parser.add_argument(
"ids", nargs="*", help="IDs of backups in config.json", type=int
)
schedule_command_parser.add_argument(
"--all", action="store_true", help="Schedule all backups"
)
# UNSCHEDULE COMMAND
unschedule_command_parser = main_commands_subparser.add_parser(
"unschedule",
help="Unschedule backups from tasks",
)
unschedule_command_parser.add_argument(
"ids", nargs="*", help="IDs of backups in config.json", type=int
)
unschedule_command_parser.add_argument(
"--all", action="store_true", help="Unschedule all backups"
)
# AUTHENTICATE COMMAND
authenticate_command_parser = main_commands_subparser.add_parser(
"authenticate",
aliases=["auth"],
help="Authenticate Google Drive accounts emails",
)
authenticate_command_parser.add_argument(
"accounts", nargs="*", help="Google Drive accounts emails"
)
all_or_unauthenticated_group = (
authenticate_command_parser.add_mutually_exclusive_group()
)
all_or_unauthenticated_group.add_argument(
"--all", action="store_true", help="All Google Drive accounts in config.json"
)
all_or_unauthenticated_group.add_argument(
"--unauthenticated",
action="store_true",
help="All yet unauthenticated Google Drive accounts in config.json",
)
arguments = parser.parse_args()
command_arguments = vars(arguments).copy()
del command_arguments["func"]
arguments.func(command_arguments)