Support for adding extra stuff to tarball when we create backup with PostgreSQL engine
@@ -17,13 +17,16 @@ import time
import os
import uuid
import asyncio
+import json
+import tarfile
-from fledge.services.core import server
-
-from fledge.common.storage_client import payload_builder
-from fledge.common.process import FledgeProcess
from fledge.common import logger
from fledge.common.audit_logger import AuditLogger
+from fledge.common.process import FledgeProcess
+from fledge.common.plugin_discovery import PluginDiscovery
+from fledge.common.storage_client import payload_builder
+from fledge.services.core import server
+from fledge.services.core.api.service import get_service_installed
import fledge.plugins.storage.postgres.backup_restore.lib as lib
import fledge.plugins.storage.postgres.backup_restore.exceptions as exceptions
@@ -353,23 +356,47 @@ class BackupProcess(FledgeProcess):
"""
self._logger.debug("{func}".format(func="execute_backup"))
-
self._purge_old_backups()
-
backup_file = self._generate_file_name()
-
- self._backup_lib.sl_backup_status_create(backup_file, lib.BackupType.FULL, lib.BackupStatus.RUNNING)
-
+ backup_file_tar_base, dummy = os.path.splitext(backup_file)
+ backup_file_tar = backup_file_tar_base + ".tar.gz"
+ self._logger.debug("execute_backup - backup_file :{}: backup_file_tar :{}: -".format(backup_file,
+ backup_file_tar))
+ self._backup_lib.sl_backup_status_create(backup_file_tar, lib.BackupType.FULL, lib.BackupStatus.RUNNING)
+ # Run backup
status, exit_code = self._run_backup_command(backup_file)
- backup_information = self._backup_lib.sl_get_backup_details_from_file_name(backup_file)
-
+ # Create tar file
+ t = tarfile.open(backup_file_tar, "w:gz")
+ t.add(backup_file, arcname=os.path.basename(backup_file))
+ # Add external scripts if any
+ backup_path = self._backup_lib.dir_fledge_data + "/scripts"
+ if os.path.isdir(backup_path):
+ t.add(backup_path, arcname=os.path.basename(backup_path))
+ # Add data/etc directory
+ t.add(self._backup_lib.dir_fledge_data_etc, arcname=os.path.basename(self._backup_lib.dir_fledge_data_etc))
+ # Add software both plugins & services
+ data = {
+ "plugins": PluginDiscovery.get_plugins_installed(),
+ "services": get_service_installed()
+ }
+ temp_software_file = "{}/software.json".format(self._backup_lib.dir_backups)
+ with open(temp_software_file, 'w') as outfile:
+ json.dump(data, outfile, indent=4)
+ t.add(temp_software_file, arcname=os.path.basename(temp_software_file))
+ t.close()
+ # Delete the temporary files
+ os.remove(backup_file)
+ os.remove(temp_software_file)
+
+ # Get backup details
+ backup_information = self._backup_lib.sl_get_backup_details_from_file_name(backup_file_tar)
+ # Update backup id, status, exit code
self._backup_lib.sl_backup_status_update(backup_information['id'], status, exit_code)
-
+ # Audit trail entry
audit = AuditLogger(self._storage_async)
loop = asyncio.get_event_loop()
if status != lib.BackupStatus.COMPLETED:
-
self._logger.error(self._MESSAGES_LIST["e000007"])
loop.run_until_complete(audit.information('BKEXC', {'status': 'failed'}))
raise exceptions.BackupFailed
