From d4f3e744de88071e95917e829abdf82698f2a084 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Thu, 27 May 2021 20:16:40 +1000 Subject: [PATCH] Improvements for backup (#70) * Remove previous backup files * Backup - Add a text file containing only the URLs, with Windows+UNIX line-endings, for better portability. * Fix filename on backup not being correct --- backend/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/__init__.py b/backend/__init__.py index 25e06049..c2e77b4b 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -633,6 +633,10 @@ def changedetection_app(conig=None, datastore_o=None): import zipfile from pathlib import Path + # Remove any existing backup file, for now we just keep one file + for previous_backup_filename in Path(app.config['datastore_path']).rglob('changedetection-backup-*.zip'): + os.unlink(previous_backup_filename) + # create a ZipFile object backupname = "changedetection-backup-{}.zip".format(int(time.time())) @@ -662,7 +666,20 @@ def changedetection_app(conig=None, datastore_o=None): compress_type=zipfile.ZIP_DEFLATED, compresslevel=8) - return send_from_directory(app.config['datastore_path'], backupname) + # Create a list file with just the URLs, so it's easier to port somewhere else in the future + list_file = os.path.join(app.config['datastore_path'], "url-list.txt") + with open(list_file, "w") as f: + for uuid in datastore.data['watching']: + url = datastore.data['watching'][uuid]['url'] + f.write("{}\r\n".format(url)) + + # Add it to the Zip + zipObj.write(list_file, + arcname="url-list.txt", + compress_type=zipfile.ZIP_DEFLATED, + compresslevel=8) + + return send_from_directory(app.config['datastore_path'], backupname, as_attachment=True) @app.route("/static//", methods=['GET']) def static_content(group, filename):