-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmodels.py
270 lines (204 loc) · 8.28 KB
/
models.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
This module provides the app's data store using Peewee with SQLite.
At the bottom there is a simple schema migration system.
"""
import json
import logging
from datetime import datetime
from enum import Enum
import peewee as pw
from playhouse import signals
from vorta.utils import slugify
from vorta.views.utils import get_exclusion_presets
DB = pw.Proxy()
logger = logging.getLogger(__name__)
class JSONField(pw.TextField):
"""
Class to "fake" a JSON field with a text field. Not efficient but works nicely.
From: https://gist.github.com/rosscdh/f4f26758b0228f475b132c688f15af2b
"""
def db_value(self, value):
"""Convert the python value for storage in the database."""
return value if value is None else json.dumps(value)
def python_value(self, value):
"""Convert the database value to a pythonic value."""
return value if value is None else json.loads(value)
class BaseModel(signals.Model):
"""Common model superclass."""
class RepoModel(BaseModel):
"""A single remote repo with unique URL."""
url = pw.CharField(unique=True)
name = pw.CharField(default='')
added_at = pw.DateTimeField(default=datetime.now)
encryption = pw.CharField(null=True)
unique_size = pw.IntegerField(null=True)
unique_csize = pw.IntegerField(null=True)
total_size = pw.IntegerField(null=True)
total_unique_chunks = pw.IntegerField(null=True)
create_backup_cmd = pw.CharField(default='')
extra_borg_arguments = pw.CharField(default='')
def is_remote_repo(self):
return not self.url.startswith('/')
class Meta:
database = DB
class RepoPassword(BaseModel):
"""Fallback to save repo passwords. Only used if no Keyring available."""
url = pw.CharField(unique=True)
password = pw.CharField()
class Meta:
database = DB
class BackupProfileModel(BaseModel):
"""Allows the user to switch between different configurations."""
name = pw.CharField()
added_at = pw.DateTimeField(default=datetime.now)
repo = pw.ForeignKeyField(RepoModel, default=None, null=True)
ssh_key = pw.CharField(default=None, null=True)
compression = pw.CharField(default='lz4')
exclude_patterns = pw.TextField(null=True)
exclude_if_present = pw.TextField(null=True)
schedule_mode = pw.CharField(default='off')
schedule_interval_count = pw.IntegerField(default=3)
schedule_interval_unit = pw.CharField(default='hours')
schedule_fixed_hour = pw.IntegerField(default=3)
schedule_fixed_minute = pw.IntegerField(default=42)
schedule_interval_hours = pw.IntegerField(default=3) # no longer used
schedule_interval_minutes = pw.IntegerField(default=42) # no longer used
schedule_make_up_missed = pw.BooleanField(default=True)
validation_on = pw.BooleanField(default=True)
validation_weeks = pw.IntegerField(default=3)
prune_on = pw.BooleanField(default=False)
prune_hour = pw.IntegerField(default=2)
prune_day = pw.IntegerField(default=7)
prune_week = pw.IntegerField(default=4)
prune_month = pw.IntegerField(default=6)
prune_year = pw.IntegerField(default=2)
prune_keep_within = pw.CharField(default='10H', null=True)
new_archive_name = pw.CharField(default="{hostname}-{now:%Y-%m-%d-%H%M%S}")
prune_prefix = pw.CharField(default="{hostname}-")
pre_backup_cmd = pw.CharField(default='')
post_backup_cmd = pw.CharField(default='')
dont_run_on_metered_networks = pw.BooleanField(default=True)
def refresh(self):
return type(self).get(self._pk_expr())
def slug(self):
return slugify(self.name)
def get_combined_exclusion_string(self):
allPresets = get_exclusion_presets()
excludes = ""
if (
ExclusionModel.select()
.where(
ExclusionModel.profile == self,
ExclusionModel.enabled,
ExclusionModel.source == ExclusionModel.SourceFieldOptions.CUSTOM.value,
)
.count()
> 0
):
excludes = "# custom added rules\n"
for exclude in ExclusionModel.select().where(
ExclusionModel.profile == self,
ExclusionModel.enabled,
ExclusionModel.source == ExclusionModel.SourceFieldOptions.CUSTOM.value,
):
excludes += f"{exclude.name}\n"
raw_excludes = self.exclude_patterns
if raw_excludes:
excludes += "\n# raw exclusions\n"
excludes += raw_excludes
excludes += "\n"
# go through all source=='preset' exclusions, find the name in the allPresets dict, and add the patterns
for exclude in ExclusionModel.select().where(
ExclusionModel.profile == self,
ExclusionModel.enabled,
ExclusionModel.source == ExclusionModel.SourceFieldOptions.PRESET.value,
):
if exclude.name not in allPresets:
logger.warning("Exclusion preset %s not found in built-in presets.", exclude.name)
continue
excludes += f"\n# {exclude.name}\n"
for pattern in allPresets[exclude.name]['patterns']:
excludes += f"{pattern}\n"
return excludes
class Meta:
database = DB
class ExclusionModel(BaseModel):
"""
If this is a user created exclusion, the name will be the same as the pattern added. For exclusions added from
presets, the name will be the same as the preset name. Duplicate patterns are already handled by Borg.
"""
class SourceFieldOptions(Enum):
CUSTOM = 'custom'
PRESET = 'preset'
profile = pw.ForeignKeyField(BackupProfileModel, backref='exclusions')
name = pw.CharField()
enabled = pw.BooleanField(default=True)
source = pw.CharField(default=SourceFieldOptions.CUSTOM.value)
class Meta:
database = DB
class SourceFileModel(BaseModel):
"""A folder to be backed up, related to a Backup Configuration."""
dir = pw.CharField()
dir_size = pw.BigIntegerField(default=-1)
dir_files_count = pw.BigIntegerField(default=-1)
path_isdir = pw.BooleanField(default=False)
profile = pw.ForeignKeyField(BackupProfileModel, default=1)
added_at = pw.DateTimeField(default=datetime.utcnow)
class Meta:
database = DB
table_name = 'sourcedirmodel'
class ArchiveModel(BaseModel):
"""An archive in a remote repository."""
snapshot_id = pw.CharField()
name = pw.CharField()
repo = pw.ForeignKeyField(RepoModel, backref='archives')
time = pw.DateTimeField()
duration = pw.FloatField(null=True)
size = pw.IntegerField(null=True)
trigger = pw.CharField(null=True)
def formatted_time(self):
return
class Meta:
database = DB
class WifiSettingModel(BaseModel):
"""Save Wifi Settings"""
ssid = pw.CharField()
last_connected = pw.DateTimeField(null=True)
allowed = pw.BooleanField(default=True)
profile = pw.ForeignKeyField(BackupProfileModel, default=1)
class Meta:
database = DB
class EventLogModel(BaseModel):
"""Keep a log of background jobs."""
start_time = pw.DateTimeField(default=datetime.now)
end_time = pw.DateTimeField(default=datetime.now)
category = pw.CharField()
subcommand = pw.CharField(null=True)
message = pw.CharField(null=True)
returncode = pw.IntegerField(default=-1)
params = JSONField(null=True)
profile = pw.CharField(null=True)
repo_url = pw.CharField(null=True)
class Meta:
database = DB
class SchemaVersion(BaseModel):
"""Keep DB version to apply the correct migrations."""
version = pw.IntegerField()
changed_at = pw.DateTimeField(default=datetime.now)
class Meta:
database = DB
class SettingsModel(BaseModel):
"""App settings unrelated to a single profile or repo"""
key = pw.CharField(unique=True)
value = pw.BooleanField(default=False)
str_value = pw.CharField(default='')
label = pw.CharField()
group = pw.CharField(default='') # Settings group name and label
tooltip = pw.CharField(default='') # optional tooltip for `checkbox` type
type = pw.CharField()
class Meta:
database = DB
class BackupProfileMixin:
"""Extend to support multiple profiles later."""
def profile(self):
return BackupProfileModel.get(id=self.window().current_profile.id)