We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
restore-files.py crashes when missing find_element returns None
## Description

When using restore-files to incrementally update a second instance of OpenCTI the process crashes whenever a referenced element is not in the data being restored (normally because it's already in the system). The return from find_element in the resolve_missing function (line 79 currently) is not checked for "None" (as is done in the restore_files function at line 121).

## Environment

1. OS (where OpenCTI server runs): Ubuntu 20.4
2. OpenCTI version: 5.3.11
3. OpenCTI client: python
4. Other environment details:

## Reproducible Steps

Steps to create the smallest reproducible scenario:
1. Run backup-files on system 1
2. copy output to system 2
3. Run restore-files on system 2

## Expected Output

Ingest of all elements.

## Actual Output

INFO:root:Restore run directory @ 20220314T200100Z
Traceback (most recent call last):
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 188, in <module>
    RestoreFilesInstance.start()
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 181, in start
    self.restore_files()
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 133, in restore_files
    self.resolve_missing(dir_date, ids, missing_element, acc)
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 87, in resolve_missing
    self.resolve_missing(dir_date, element_ids, missing_element, acc)
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 80, in resolve_missing
    refs = ref_extractors([data])
  File "/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py", line 17, in ref_extractors
    for key in data.keys():
AttributeError: 'NoneType' object has no attribute 'keys'
Killed


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `external-import/restore-files/src/restore-files.py`
Content:
```
1 ################################
2 # OpenCTI Restore Files         #
3 ################################
4 import datetime
5 import json
6 import os
7 import sys
8 from pathlib import Path
9 
10 import yaml
11 from pycti import OpenCTIConnectorHelper, OpenCTIStix2Splitter, get_config_variable
12 
13 
14 def ref_extractors(objects):
15     ids = []
16     for data in objects:
17         for key in data.keys():
18             if key.startswith("x_") is False:
19                 if key.endswith("_ref"):
20                     ids.append(data[key])
21                 if key.endswith("_refs"):
22                     ids.extend(data[key])
23     return set(ids)
24 
25 
26 def fetch_stix_data(file):
27     # Open a file: file
28     file = open(file, mode="r")
29     file_content = file.read()
30     file.close()
31     file_json = json.loads(file_content)
32     return file_json["objects"]
33 
34 
35 def date_convert(name):
36     return datetime.datetime.strptime(name, "%Y%m%dT%H%M%SZ")
37 
38 
39 class RestoreFilesConnector:
40     def __init__(self, conf_data):
41         config_file_path = os.path.dirname(os.path.abspath(__file__)) + "/config.yml"
42         config = (
43             yaml.load(open(config_file_path), Loader=yaml.FullLoader)
44             if os.path.isfile(config_file_path)
45             else conf_data
46         )
47         self.helper = OpenCTIConnectorHelper(config)
48         # Extra config
49         self.direct_creation = get_config_variable(
50             "DIRECT_CREATION",
51             ["backup", "direct_creation"],
52             config,
53             default=False,
54         )
55         self.backup_protocol = get_config_variable(
56             "BACKUP_PROTOCOL", ["backup", "protocol"], config
57         )
58         self.backup_path = get_config_variable(
59             "BACKUP_PATH", ["backup", "path"], config
60         )
61 
62     def find_element(self, dir_date, id):
63         name = id + ".json"
64         path = self.backup_path + "/opencti_data"
65         for root, dirs, files in os.walk(path):
66             if name in files:
67                 # If find dir is before, no need to process the element as missing
68                 path = os.path.basename(root)
69                 if date_convert(path) > dir_date:
70                     return fetch_stix_data(os.path.join(root, name))[0]
71         return None
72 
73     def resolve_missing(self, dir_date, element_ids, data, acc=[]):
74         refs = ref_extractors([data])
75         for ref in refs:
76             if ref not in element_ids:
77                 not_in = next((x for x in acc if x["id"] == ref), None)
78                 if not_in is None:
79                     missing_element = self.find_element(dir_date, ref)
80                     acc.insert(0, missing_element)
81                     self.resolve_missing(dir_date, element_ids, missing_element, acc)
82 
83     def restore_files(self):
84         stix2_splitter = OpenCTIStix2Splitter()
85         state = self.helper.get_state()
86         start_directory = (
87             state["current"] if state is not None and "current" in state else None
88         )
89         start_date = (
90             date_convert(start_directory) if start_directory is not None else None
91         )
92         path = self.backup_path + "/opencti_data"
93         dirs = sorted(Path(path).iterdir(), key=lambda d: date_convert(d.name))
94         for entry in dirs:
95             friendly_name = "Restore run directory @ " + entry.name
96             self.helper.log_info(friendly_name)
97             dir_date = date_convert(entry.name)
98             if start_date is not None and dir_date <= start_date:
99                 continue
100             # 00 - Create a bundle for the directory
101             files_data = []
102             element_ids = []
103             # 01 - build all _ref / _refs contained in the bundle
104             element_refs = []
105             for file in os.scandir(entry):
106                 if file.is_file():
107                     objects = fetch_stix_data(file)
108                     object_ids = set(map(lambda x: x["id"], objects))
109                     element_refs.extend(ref_extractors(objects))
110                     files_data.extend(objects)
111                     element_ids.extend(object_ids)
112             # Ensure the bundle is consistent (include meta elements)
113             # 02 - Scan bundle to detect missing elements
114             acc = []
115             ids = set(element_ids)
116             refs = set(element_refs)
117             for ref in refs:
118                 if ref not in ids:
119                     # 03 - If missing, scan the other dir/files to find the elements
120                     missing_element = self.find_element(dir_date, ref)
121                     if missing_element is not None:
122                         acc.insert(0, missing_element)
123                         # 04 - Restart the process to handle recursive resolution
124                         self.resolve_missing(dir_date, ids, missing_element, acc)
125             # 05 - Add elements to the bundle
126             objects_with_missing = acc + files_data
127             if len(objects_with_missing) > 0:
128                 # Create the work
129                 work_id = self.helper.api.work.initiate_work(
130                     self.helper.connect_id, friendly_name
131                 )
132                 # 06 - Send the bundle to the worker queue
133                 stix_bundle = {
134                     "type": "bundle",
135                     "objects": objects_with_missing,
136                 }
137                 if self.direct_creation:
138                     # Bundle must be split for reordering
139                     bundles = stix2_splitter.split_bundle(stix_bundle, False)
140                     self.helper.log_info(
141                         "restore dir "
142                         + entry.name
143                         + " with "
144                         + str(len(bundles))
145                         + " bundles (direct creation)"
146                     )
147                     for bundle in bundles:
148                         self.helper.api.stix2.import_bundle_from_json(
149                             json.dumps(bundle), True
150                         )
151                     # 06 - Save the state
152                     self.helper.set_state({"current": entry.name})
153                 else:
154                     self.helper.log_info("restore dir (worker bundles):" + entry.name)
155                     self.helper.send_stix2_bundle(
156                         json.dumps(stix_bundle), work_id=work_id
157                     )
158                     message = "Restore dir run, storing last_run as {0}".format(
159                         entry.name
160                     )
161                     self.helper.api.work.to_processed(work_id, message)
162                     # 06 - Save the state
163                     self.helper.set_state({"current": entry.name})
164         self.helper.log_info("restore run completed")
165 
166     def start(self):
167         # Check if the directory exists
168         if not os.path.exists(self.backup_path + "/opencti_data"):
169             raise ValueError(
170                 "Backup path does not exist - " + self.backup_path + "/opencti_data"
171             )
172         self.restore_files()
173 
174 
175 if __name__ == "__main__":
176     json_conf = sys.argv[1] if len(sys.argv) > 1 else None
177     conf = json.loads(json_conf) if json_conf is not None else {}
178     RestoreFilesInstance = RestoreFilesConnector(conf)
179     RestoreFilesInstance.start()
180
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/external-import/restore-files/src/restore-files.py b/external-import/restore-files/src/restore-files.py
--- a/external-import/restore-files/src/restore-files.py
+++ b/external-import/restore-files/src/restore-files.py
@@ -77,8 +77,9 @@
                 not_in = next((x for x in acc if x["id"] == ref), None)
                 if not_in is None:
                     missing_element = self.find_element(dir_date, ref)
-                    acc.insert(0, missing_element)
-                    self.resolve_missing(dir_date, element_ids, missing_element, acc)
+                    if missing_element is not None:
+                        acc.insert(0, missing_element)
+                        self.resolve_missing(dir_date, element_ids, missing_element, acc)
 
     def restore_files(self):
         stix2_splitter = OpenCTIStix2Splitter()

{"golden_diff": "diff --git a/external-import/restore-files/src/restore-files.py b/external-import/restore-files/src/restore-files.py\n--- a/external-import/restore-files/src/restore-files.py\n+++ b/external-import/restore-files/src/restore-files.py\n@@ -77,8 +77,9 @@\n                 not_in = next((x for x in acc if x[\"id\"] == ref), None)\n                 if not_in is None:\n                     missing_element = self.find_element(dir_date, ref)\n-                    acc.insert(0, missing_element)\n-                    self.resolve_missing(dir_date, element_ids, missing_element, acc)\n+                    if missing_element is not None:\n+                        acc.insert(0, missing_element)\n+                        self.resolve_missing(dir_date, element_ids, missing_element, acc)\n \n     def restore_files(self):\n         stix2_splitter = OpenCTIStix2Splitter()\n", "issue": "restore-files.py crashes when missing find_element returns None\n## Description\r\n\r\nWhen using restore-files to incrementally update a second instance of OpenCTI the process crashes whenever a referenced element is not in the data being restored (normally because it's already in the system). The return from find_element in the resolve_missing function (line 79 currently) is not checked for \"None\" (as is done in the restore_files function at line 121).\r\n\r\n## Environment\r\n\r\n1. OS (where OpenCTI server runs): Ubuntu 20.4\r\n2. OpenCTI version: 5.3.11\r\n3. OpenCTI client: python\r\n4. Other environment details:\r\n\r\n## Reproducible Steps\r\n\r\nSteps to create the smallest reproducible scenario:\r\n1. Run backup-files on system 1\r\n2. copy output to system 2\r\n3. Run restore-files on system 2\r\n\r\n## Expected Output\r\n\r\nIngest of all elements.\r\n\r\n## Actual Output\r\n\r\nINFO:root:Restore run directory @ 20220314T200100Z\r\nTraceback (most recent call last):\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 188, in <module>\r\n    RestoreFilesInstance.start()\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 181, in start\r\n    self.restore_files()\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 133, in restore_files\r\n    self.resolve_missing(dir_date, ids, missing_element, acc)\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 87, in resolve_missing\r\n    self.resolve_missing(dir_date, element_ids, missing_element, acc)\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 80, in resolve_missing\r\n    refs = ref_extractors([data])\r\n  File \"/opt/opencti-highside-sync/connectors-master/external-import/restore-files/src/restore-files.py\", line 17, in ref_extractors\r\n    for key in data.keys():\r\nAttributeError: 'NoneType' object has no attribute 'keys'\r\nKilled\r\n\n", "before_files": [{"content": "################################\n# OpenCTI Restore Files         #\n################################\nimport datetime\nimport json\nimport os\nimport sys\nfrom pathlib import Path\n\nimport yaml\nfrom pycti import OpenCTIConnectorHelper, OpenCTIStix2Splitter, get_config_variable\n\n\ndef ref_extractors(objects):\n    ids = []\n    for data in objects:\n        for key in data.keys():\n            if key.startswith(\"x_\") is False:\n                if key.endswith(\"_ref\"):\n                    ids.append(data[key])\n                if key.endswith(\"_refs\"):\n                    ids.extend(data[key])\n    return set(ids)\n\n\ndef fetch_stix_data(file):\n    # Open a file: file\n    file = open(file, mode=\"r\")\n    file_content = file.read()\n    file.close()\n    file_json = json.loads(file_content)\n    return file_json[\"objects\"]\n\n\ndef date_convert(name):\n    return datetime.datetime.strptime(name, \"%Y%m%dT%H%M%SZ\")\n\n\nclass RestoreFilesConnector:\n    def __init__(self, conf_data):\n        config_file_path = os.path.dirname(os.path.abspath(__file__)) + \"/config.yml\"\n        config = (\n            yaml.load(open(config_file_path), Loader=yaml.FullLoader)\n            if os.path.isfile(config_file_path)\n            else conf_data\n        )\n        self.helper = OpenCTIConnectorHelper(config)\n        # Extra config\n        self.direct_creation = get_config_variable(\n            \"DIRECT_CREATION\",\n            [\"backup\", \"direct_creation\"],\n            config,\n            default=False,\n        )\n        self.backup_protocol = get_config_variable(\n            \"BACKUP_PROTOCOL\", [\"backup\", \"protocol\"], config\n        )\n        self.backup_path = get_config_variable(\n            \"BACKUP_PATH\", [\"backup\", \"path\"], config\n        )\n\n    def find_element(self, dir_date, id):\n        name = id + \".json\"\n        path = self.backup_path + \"/opencti_data\"\n        for root, dirs, files in os.walk(path):\n            if name in files:\n                # If find dir is before, no need to process the element as missing\n                path = os.path.basename(root)\n                if date_convert(path) > dir_date:\n                    return fetch_stix_data(os.path.join(root, name))[0]\n        return None\n\n    def resolve_missing(self, dir_date, element_ids, data, acc=[]):\n        refs = ref_extractors([data])\n        for ref in refs:\n            if ref not in element_ids:\n                not_in = next((x for x in acc if x[\"id\"] == ref), None)\n                if not_in is None:\n                    missing_element = self.find_element(dir_date, ref)\n                    acc.insert(0, missing_element)\n                    self.resolve_missing(dir_date, element_ids, missing_element, acc)\n\n    def restore_files(self):\n        stix2_splitter = OpenCTIStix2Splitter()\n        state = self.helper.get_state()\n        start_directory = (\n            state[\"current\"] if state is not None and \"current\" in state else None\n        )\n        start_date = (\n            date_convert(start_directory) if start_directory is not None else None\n        )\n        path = self.backup_path + \"/opencti_data\"\n        dirs = sorted(Path(path).iterdir(), key=lambda d: date_convert(d.name))\n        for entry in dirs:\n            friendly_name = \"Restore run directory @ \" + entry.name\n            self.helper.log_info(friendly_name)\n            dir_date = date_convert(entry.name)\n            if start_date is not None and dir_date <= start_date:\n                continue\n            # 00 - Create a bundle for the directory\n            files_data = []\n            element_ids = []\n            # 01 - build all _ref / _refs contained in the bundle\n            element_refs = []\n            for file in os.scandir(entry):\n                if file.is_file():\n                    objects = fetch_stix_data(file)\n                    object_ids = set(map(lambda x: x[\"id\"], objects))\n                    element_refs.extend(ref_extractors(objects))\n                    files_data.extend(objects)\n                    element_ids.extend(object_ids)\n            # Ensure the bundle is consistent (include meta elements)\n            # 02 - Scan bundle to detect missing elements\n            acc = []\n            ids = set(element_ids)\n            refs = set(element_refs)\n            for ref in refs:\n                if ref not in ids:\n                    # 03 - If missing, scan the other dir/files to find the elements\n                    missing_element = self.find_element(dir_date, ref)\n                    if missing_element is not None:\n                        acc.insert(0, missing_element)\n                        # 04 - Restart the process to handle recursive resolution\n                        self.resolve_missing(dir_date, ids, missing_element, acc)\n            # 05 - Add elements to the bundle\n            objects_with_missing = acc + files_data\n            if len(objects_with_missing) > 0:\n                # Create the work\n                work_id = self.helper.api.work.initiate_work(\n                    self.helper.connect_id, friendly_name\n                )\n                # 06 - Send the bundle to the worker queue\n                stix_bundle = {\n                    \"type\": \"bundle\",\n                    \"objects\": objects_with_missing,\n                }\n                if self.direct_creation:\n                    # Bundle must be split for reordering\n                    bundles = stix2_splitter.split_bundle(stix_bundle, False)\n                    self.helper.log_info(\n                        \"restore dir \"\n                        + entry.name\n                        + \" with \"\n                        + str(len(bundles))\n                        + \" bundles (direct creation)\"\n                    )\n                    for bundle in bundles:\n                        self.helper.api.stix2.import_bundle_from_json(\n                            json.dumps(bundle), True\n                        )\n                    # 06 - Save the state\n                    self.helper.set_state({\"current\": entry.name})\n                else:\n                    self.helper.log_info(\"restore dir (worker bundles):\" + entry.name)\n                    self.helper.send_stix2_bundle(\n                        json.dumps(stix_bundle), work_id=work_id\n                    )\n                    message = \"Restore dir run, storing last_run as {0}\".format(\n                        entry.name\n                    )\n                    self.helper.api.work.to_processed(work_id, message)\n                    # 06 - Save the state\n                    self.helper.set_state({\"current\": entry.name})\n        self.helper.log_info(\"restore run completed\")\n\n    def start(self):\n        # Check if the directory exists\n        if not os.path.exists(self.backup_path + \"/opencti_data\"):\n            raise ValueError(\n                \"Backup path does not exist - \" + self.backup_path + \"/opencti_data\"\n            )\n        self.restore_files()\n\n\nif __name__ == \"__main__\":\n    json_conf = sys.argv[1] if len(sys.argv) > 1 else None\n    conf = json.loads(json_conf) if json_conf is not None else {}\n    RestoreFilesInstance = RestoreFilesConnector(conf)\n    RestoreFilesInstance.start()\n", "path": "external-import/restore-files/src/restore-files.py"}], "after_files": [{"content": "################################\n# OpenCTI Restore Files         #\n################################\nimport datetime\nimport json\nimport os\nimport sys\nfrom pathlib import Path\n\nimport yaml\nfrom pycti import OpenCTIConnectorHelper, OpenCTIStix2Splitter, get_config_variable\n\n\ndef ref_extractors(objects):\n    ids = []\n    for data in objects:\n        for key in data.keys():\n            if key.startswith(\"x_\") is False:\n                if key.endswith(\"_ref\"):\n                    ids.append(data[key])\n                if key.endswith(\"_refs\"):\n                    ids.extend(data[key])\n    return set(ids)\n\n\ndef fetch_stix_data(file):\n    # Open a file: file\n    file = open(file, mode=\"r\")\n    file_content = file.read()\n    file.close()\n    file_json = json.loads(file_content)\n    return file_json[\"objects\"]\n\n\ndef date_convert(name):\n    return datetime.datetime.strptime(name, \"%Y%m%dT%H%M%SZ\")\n\n\nclass RestoreFilesConnector:\n    def __init__(self, conf_data):\n        config_file_path = os.path.dirname(os.path.abspath(__file__)) + \"/config.yml\"\n        config = (\n            yaml.load(open(config_file_path), Loader=yaml.FullLoader)\n            if os.path.isfile(config_file_path)\n            else conf_data\n        )\n        self.helper = OpenCTIConnectorHelper(config)\n        # Extra config\n        self.direct_creation = get_config_variable(\n            \"DIRECT_CREATION\",\n            [\"backup\", \"direct_creation\"],\n            config,\n            default=False,\n        )\n        self.backup_protocol = get_config_variable(\n            \"BACKUP_PROTOCOL\", [\"backup\", \"protocol\"], config\n        )\n        self.backup_path = get_config_variable(\n            \"BACKUP_PATH\", [\"backup\", \"path\"], config\n        )\n\n    def find_element(self, dir_date, id):\n        name = id + \".json\"\n        path = self.backup_path + \"/opencti_data\"\n        for root, dirs, files in os.walk(path):\n            if name in files:\n                # If find dir is before, no need to process the element as missing\n                path = os.path.basename(root)\n                if date_convert(path) > dir_date:\n                    return fetch_stix_data(os.path.join(root, name))[0]\n        return None\n\n    def resolve_missing(self, dir_date, element_ids, data, acc=[]):\n        refs = ref_extractors([data])\n        for ref in refs:\n            if ref not in element_ids:\n                not_in = next((x for x in acc if x[\"id\"] == ref), None)\n                if not_in is None:\n                    missing_element = self.find_element(dir_date, ref)\n                    if missing_element is not None:\n                        acc.insert(0, missing_element)\n                        self.resolve_missing(dir_date, element_ids, missing_element, acc)\n\n    def restore_files(self):\n        stix2_splitter = OpenCTIStix2Splitter()\n        state = self.helper.get_state()\n        start_directory = (\n            state[\"current\"] if state is not None and \"current\" in state else None\n        )\n        start_date = (\n            date_convert(start_directory) if start_directory is not None else None\n        )\n        path = self.backup_path + \"/opencti_data\"\n        dirs = sorted(Path(path).iterdir(), key=lambda d: date_convert(d.name))\n        for entry in dirs:\n            friendly_name = \"Restore run directory @ \" + entry.name\n            self.helper.log_info(friendly_name)\n            dir_date = date_convert(entry.name)\n            if start_date is not None and dir_date <= start_date:\n                continue\n            # 00 - Create a bundle for the directory\n            files_data = []\n            element_ids = []\n            # 01 - build all _ref / _refs contained in the bundle\n            element_refs = []\n            for file in os.scandir(entry):\n                if file.is_file():\n                    objects = fetch_stix_data(file)\n                    object_ids = set(map(lambda x: x[\"id\"], objects))\n                    element_refs.extend(ref_extractors(objects))\n                    files_data.extend(objects)\n                    element_ids.extend(object_ids)\n            # Ensure the bundle is consistent (include meta elements)\n            # 02 - Scan bundle to detect missing elements\n            acc = []\n            ids = set(element_ids)\n            refs = set(element_refs)\n            for ref in refs:\n                if ref not in ids:\n                    # 03 - If missing, scan the other dir/files to find the elements\n                    missing_element = self.find_element(dir_date, ref)\n                    if missing_element is not None:\n                        acc.insert(0, missing_element)\n                        # 04 - Restart the process to handle recursive resolution\n                        self.resolve_missing(dir_date, ids, missing_element, acc)\n            # 05 - Add elements to the bundle\n            objects_with_missing = acc + files_data\n            if len(objects_with_missing) > 0:\n                # Create the work\n                work_id = self.helper.api.work.initiate_work(\n                    self.helper.connect_id, friendly_name\n                )\n                # 06 - Send the bundle to the worker queue\n                stix_bundle = {\n                    \"type\": \"bundle\",\n                    \"objects\": objects_with_missing,\n                }\n                if self.direct_creation:\n                    # Bundle must be split for reordering\n                    bundles = stix2_splitter.split_bundle(stix_bundle, False)\n                    self.helper.log_info(\n                        \"restore dir \"\n                        + entry.name\n                        + \" with \"\n                        + str(len(bundles))\n                        + \" bundles (direct creation)\"\n                    )\n                    for bundle in bundles:\n                        self.helper.api.stix2.import_bundle_from_json(\n                            json.dumps(bundle), True\n                        )\n                    # 06 - Save the state\n                    self.helper.set_state({\"current\": entry.name})\n                else:\n                    self.helper.log_info(\"restore dir (worker bundles):\" + entry.name)\n                    self.helper.send_stix2_bundle(\n                        json.dumps(stix_bundle), work_id=work_id\n                    )\n                    message = \"Restore dir run, storing last_run as {0}\".format(\n                        entry.name\n                    )\n                    self.helper.api.work.to_processed(work_id, message)\n                    # 06 - Save the state\n                    self.helper.set_state({\"current\": entry.name})\n        self.helper.log_info(\"restore run completed\")\n\n    def start(self):\n        # Check if the directory exists\n        if not os.path.exists(self.backup_path + \"/opencti_data\"):\n            raise ValueError(\n                \"Backup path does not exist - \" + self.backup_path + \"/opencti_data\"\n            )\n        self.restore_files()\n\n\nif __name__ == \"__main__\":\n    json_conf = sys.argv[1] if len(sys.argv) > 1 else None\n    conf = json.loads(json_conf) if json_conf is not None else {}\n    RestoreFilesInstance = RestoreFilesConnector(conf)\n    RestoreFilesInstance.start()\n", "path": "external-import/restore-files/src/restore-files.py"}]}