Skip to content

Commit 52b070e

Browse files
foutoucourpedrobaeza
authored andcommitted
auto_backup: allow to change the format of backup (#1333)
1 parent 2602f59 commit 52b070e

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

auto_backup/models/db_backup.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ class DbBackup(models.Model):
8383
"read permissions for that file.",
8484
)
8585

86+
backup_format = fields.Selection(
87+
[
88+
("zip", "zip (includes filestore)"),
89+
("dump", "pg_dump custom format (without filestore)")
90+
],
91+
default='zip',
92+
help="Choose the format for this backup."
93+
)
94+
8695
@api.model
8796
def _default_folder(self):
8897
"""Default to ``backups`` folder inside current server datadir."""
@@ -131,11 +140,11 @@ def action_sftp_test_connection(self):
131140
def action_backup(self):
132141
"""Run selected backups."""
133142
backup = None
134-
filename = self.filename(datetime.now())
135143
successful = self.browse()
136144

137145
# Start with local storage
138146
for rec in self.filtered(lambda r: r.method == "local"):
147+
filename = self.filename(datetime.now(), ext=rec.backup_format)
139148
with rec.backup_log():
140149
# Directory must exist
141150
try:
@@ -151,21 +160,28 @@ def action_backup(self):
151160
shutil.copyfileobj(cached, destiny)
152161
# Generate new backup
153162
else:
154-
db.dump_db(self.env.cr.dbname, destiny)
163+
db.dump_db(
164+
self.env.cr.dbname,
165+
destiny,
166+
backup_format=rec.backup_format
167+
)
155168
backup = backup or destiny.name
156169
successful |= rec
157170

158171
# Ensure a local backup exists if we are going to write it remotely
159172
sftp = self.filtered(lambda r: r.method == "sftp")
160173
if sftp:
161-
if backup:
162-
cached = open(backup)
163-
else:
164-
cached = db.dump_db(self.env.cr.dbname, None)
174+
for rec in sftp:
175+
filename = self.filename(datetime.now(), ext=rec.backup_format)
176+
with rec.backup_log():
165177

166-
with cached:
167-
for rec in sftp:
168-
with rec.backup_log():
178+
cached = db.dump_db(
179+
self.env.cr.dbname,
180+
None,
181+
backup_format=rec.backup_format
182+
)
183+
184+
with cached:
169185
with rec.sftp_connection() as remote:
170186
# Directory must exist
171187
try:
@@ -255,13 +271,16 @@ def cleanup_log(self):
255271
self.name)
256272

257273
@staticmethod
258-
def filename(when):
274+
def filename(when, ext='zip'):
259275
"""Generate a file name for a backup.
260276
261277
:param datetime.datetime when:
262278
Use this datetime instead of :meth:`datetime.datetime.now`.
279+
:param str ext: Extension of the file. Default: dump.zip
263280
"""
264-
return "{:%Y_%m_%d_%H_%M_%S}.dump.zip".format(when)
281+
return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format(
282+
when, ext='dump.zip' if ext == 'zip' else ext
283+
)
265284

266285
@api.multi
267286
def sftp_connection(self):

auto_backup/tests/test_db_backup.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,6 @@ def test_action_backup_sftp_remote_open(self):
175175
'wb'
176176
)
177177

178-
def test_action_backup_sftp_remote_open(self):
179-
""" It should open remote file w/ proper args """
180-
rec_id = self.new_record()
181-
with self.mock_assets() as assets:
182-
with self.patch_filtered_sftp(rec_id):
183-
conn = rec_id.sftp_connection().__enter__()
184-
rec_id.action_backup()
185-
conn.open.assert_called_once_with(
186-
assets['os'].path.join(),
187-
'wb'
188-
)
189-
190178
def test_action_backup_all_search(self):
191179
""" It should search all records """
192180
rec_id = self.new_record()
@@ -241,8 +229,20 @@ def test_sftp_connection_return(self, pysftp):
241229
pysftp.Connection(), res,
242230
)
243231

244-
def test_filename(self):
232+
def test_filename_default(self):
245233
""" It should not error and should return a .dump.zip file str """
246234
now = datetime.now()
247235
res = self.Model.filename(now)
248236
self.assertTrue(res.endswith(".dump.zip"))
237+
238+
def test_filename_zip(self):
239+
""" It should return a dump.zip filename"""
240+
now = datetime.now()
241+
res = self.Model.filename(now, ext='zip')
242+
self.assertTrue(res.endswith(".dump.zip"))
243+
244+
def test_filename_dump(self):
245+
""" It should return a dump filename"""
246+
now = datetime.now()
247+
res = self.Model.filename(now, ext='dump')
248+
self.assertTrue(res.endswith(".dump"))

auto_backup/view/db_backup_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<field name="folder"/>
1616
<field name="days_to_keep"/>
1717
<field name="method"/>
18+
<field name="backup_format"/>
1819
</group>
1920
<div attrs="{'invisible': [('method', '!=', 'sftp')]}">
2021
<div class="bg-warning text-warning">

0 commit comments

Comments
 (0)