Repository: Naruno/Naruno (language: python)

Issue:
lib: Adding backup system
- https://docs.python.org/3/library/shutil.html#shutil.make_archive
- https://docs.python.org/3/library/shutil.html#shutil.unpack_archive


Discussion / hints:

Fix patch:
diff --git a/.gitignore b/.gitignore
index 66b0d980..2c490dc8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+#backups
+decentra_network/backups/*
+
 #manim exports
 media/*
 
diff --git a/decentra_network/config.py b/decentra_network/config.py
index a2e6ded3..873bfd2a 100644
--- a/decentra_network/config.py
+++ b/decentra_network/config.py
@@ -40,3 +40,4 @@
 EXTRACTED_PROOFS_PATH = "db/extracted_proofs/"
 
 QR_CODE_PATH = "db/qrs/"
+BACKUPS_PATH = "backups/"
diff --git a/decentra_network/lib/backup/decentra_export.py b/decentra_network/lib/backup/decentra_export.py
new file mode 100644
index 00000000..a9a5e782
--- /dev/null
+++ b/decentra_network/lib/backup/decentra_export.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+import os
+import shutil
+import sys
+import time
+
+sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
+from decentra_network.config import BACKUPS_PATH
+from decentra_network.lib.config_system import get_config
+from decentra_network.lib.log import get_logger
+
+logger = get_logger("LIB")
+
+
+def decentra_export():
+    logger.info("Export system is started")
+    main_folder = get_config()["main_folder"]
+    zip_dir = f"{main_folder}/db/"
+    result_file = f"{main_folder}/{BACKUPS_PATH}{int(time.time())}"
+    logger.debug(f"zip_dir: {zip_dir}")
+    logger.debug(f"result_file: {result_file}.zip")
+    shutil.make_archive(result_file, "zip", zip_dir)
+    logger.info("Export complated")
+
+    return f"{result_file}.zip"
diff --git a/decentra_network/lib/backup/decentra_import.py b/decentra_network/lib/backup/decentra_import.py
new file mode 100644
index 00000000..5bd95242
--- /dev/null
+++ b/decentra_network/lib/backup/decentra_import.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+import os
+import shutil
+import sys
+
+sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
+from decentra_network.lib.config_system import get_config
+from decentra_network.lib.log import get_logger
+
+logger = get_logger("LIB")
+
+
+def decentra_import(export_location):
+    logger.info("Import system is started")
+    main_folder = get_config()["main_folder"]
+    target_location = f"{main_folder}/db/"
+    logger.debug(f"export_location: {export_location}")
+    logger.debug(f"target_location: {target_location}")
+    shutil.unpack_archive(export_location, target_location)
+    logger.info("Import complated")


Test patch:
diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py
index 06d8e1c5..34ee4e3d 100644
--- a/tests/unit_tests/test_lib.py
+++ b/tests/unit_tests/test_lib.py
@@ -20,6 +20,8 @@
     MY_TRANSACTION_EXPORT_PATH, PENDING_TRANSACTIONS_PATH, TEMP_ACCOUNTS_PATH,
     TEMP_BLOCK_PATH, TEMP_BLOCKSHASH_PART_PATH, TEMP_BLOCKSHASH_PATH,
     UNL_NODES_PATH)
+from decentra_network.lib.backup.decentra_export import decentra_export
+from decentra_network.lib.backup.decentra_import import decentra_import
 from decentra_network.lib.clean_up import CleanUp_tests
 from decentra_network.lib.config_system import get_config
 from decentra_network.lib.export import export_the_transactions
@@ -666,5 +668,17 @@ def test_notification_mt_False(self):
 
         mt_settings(temp_settings["mute_notifications"])
 
+    def test_export_import(self):
+        backup = decentra_export()
+        temp_settings = the_settings()
+
+        changed_value = True if temp_settings["debug_mode"] is False else False
+        d_mode_settings(changed_value)
+        new_settings = the_settings()
+
+        self.assertEqual(new_settings["debug_mode"], changed_value)
+        decentra_import(backup)
+        self.assertNotEqual(the_settings()["debug_mode"], changed_value)
+
 
 unittest.main(exit=False)